Specific use of Linux which command

  • 2021-07-24 12:11:33
  • OfStack

We often look for a file in linux, but we don't know where we put it. We can use the following commands to search:

which View the location of the executable file. whereis View the location of the file. locate works with the database to see the file location. find actually searches the hard disk for file names.

01. Command Overview

Find files in environment variables

The which command is used to find and display the absolute path of a given command, and the environment variable PATH holds the directory that needs to be traversed when finding the command. The which directive looks for qualified files in the directory set by the environment variable $PATH. That is to say, using which command, you can see whether a certain system command exists and where the command is executed.

02. Command format

which [选项] 执行文件名 […]

03. Common Options


 Find files in environment variables 

-a   Find everything, not the first 1 Files 
-n  < File name length >  Specifies the length of the filename, which must be greater than or equal to the longest filename of all files.  
-p  < File name length >  And -n Parameters are the same, but the < File name length > Includes the path of the file.  
-w  Specifies the width of the field at output.  
-V  Displays version information. 

--version, -[vV]  Display version information and exit 
--help  Display help information and exit 
--skip-dot  Skip  PATH  Directories beginning with dots in 
--skip-tilde  Skip  PATH  Directory beginning with waveform symbol in 
--show-dot  Do not extend the point to the current directory in the output 
--show-tilde  For  HOME  Directory (non-root) output waveform 
--tty-only  If not  tty  Stop the processing option on the right 
--all, -a  Print  PATH  All matches in, not just the first 1 A 
--read-alias, -i  From  stdin  Read the alias list in 
--skip-alias  Ignore option  --read-alias ; Don't read  stdin
--read-functions  From  stdin  Read  shell  Function 
--skip-functions  Ignore option  --read-functions ; Don't read  stdin

04. Reference example

4.1 Display the path to the command


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

which is according to the user configured PATH variables within the directory to search for runnable files! Therefore, different PATH configurations may find different commands.

4.2 Display aliases for commands


[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 Common users and root users have different lookup paths

The situation of ordinary users looking up


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

root User Lookup Scenarios


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

4.4 Built-in command not found


[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 ~]#

Related articles: