Specific use of Linux which command

Specific use of Linux which command

We often want to find a file in Linux, but we don't know where it is. We can use the following commands to search:

  • which View the location of the executable file.
  • whereis checks the location of the file.
  • locate cooperates with the database to view the file location.
  • find actually searches the hard disk for the file name.

01. Command Overview

Find files in environment variables

The which command is used to find and display the absolute path of a given command. The environment variable PATH stores the directories that need to be traversed when searching for commands. The which command searches for matching files in the directories set by the environment variable $PATH. In other words, by using the which command, you can see whether a system command exists and where the command is executed.

02. Command format

which [選項] 執行文件名[…]

03. Common options

Search for files in environment variables -a Search all contents instead of the first file -n <file name length> Specify the file name length. The specified length must be greater than or equal to the longest file name among all files. 
-p <filename length> is the same as the -n parameter, but the <filename length> here includes the file path. 
-w specifies the width of the output columns. 
-V Display version information.

--version, -[vV] show version information and exit --help show this help message and exit --skip-dot skip directories in PATH that begin with a dot --skip-tilde skip directories in PATH that begin with a tilde --show-dot do not expand dot to current directory in output --show-tilde print tilde for HOME directory (non-root) --tty-only if not on a tty, stop processing options to the right --all, -a print all matches in PATH, not just the first --read-alias, -i read alias list from stdin --skip-alias ignore option --read-alias; do not read stdin
--read-functions Read shell functions from stdin --skip-functions Ignore option --read-functions; do not read stdin

04. Reference examples

4.1 Display command path

[deng@localhost test]$ which bash
/usr/bin/bash

Description: which searches for executable files based on the directories in the PATH variable configured by the user! Therefore, different PATH configuration contents may find different commands.

4.2 Display command aliases

[deng@localhost test]$ which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /usr/bin/alias
    /usr/bin/which
[deng@localhost test]$ 

4.3 Ordinary users and root users have different search paths

Common user search scenarios

[deng@localhost test]$ which pwd
/usr/bin/pwd

Root user search scenario

[root@localhost ~]# which pwd
/bin/pwd
[root@localhost ~]#

4.4 Cannot find built-in commands

[root@localhost ~]# which type
/usr/bin/which: no type in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]#

This is the end of this article about the specific usage of the Linux which command. For more relevant Linux which content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The difference between Linux command search commands whereis and which

<<:  Echarts tutorial on how to implement tree charts

>>:  Detailed explanation of common commands in MySQL 8.0+

Recommend

MySQL series 6 users and authorization

Table of contents Tutorial Series 1. User Managem...

jQuery plugin to implement search history

A jQuery plugin every day - to make search histor...

Using react-virtualized to implement a long list of images with dynamic height

Table of contents Problems encountered during dev...

JS implements simple example code to control video playback speed

introduction I discovered a problem before: somet...

Implementation of debugging code through nginx reverse proxy

background Now the company's projects are dev...

A detailed analysis and processing of MySQL alarms

Recently, a service has an alarm, which has made ...

Graphic tutorial on installing CentOS7 on VMware 15.5

1. Create a new virtual machine in VMware 15.5 1....

Introduction to JavaScript conditional access attributes and arrow functions

Table of contents 1. Conditional access attribute...

Delete the image operation of none in docker images

Since I usually use the docker build command to g...

MySQL 5.7.18 installation tutorial under Windows

This article explains how to install MySQL from a...

Linux system dual network card binding configuration implementation

System version [root@ ~]# cat /etc/redhat-release...

Write a simple calculator using JavaScript

The effect is as follows:Reference Program: <!...

Vue data two-way binding implementation method

Table of contents 1. Introduction 2. Code Impleme...

In-depth explanation of the locking mechanism in MySQL InnoDB

Written in front A database is essentially a shar...