A Linux command a day (19): which command

post thumb
Devops
by Admin/ on 01 Oct 2021

A Linux command a day (19): which command


We often have to look for a file in linux, but we don’t know where it is, we can use some of the following commands to search for it.

  • which to see the location of the executable file.

  • whereis to see the location of the file.

  • locate works with the database to see the location of the file.

  • find to actually search the hard disk for the file name.

The purpose of the which command is to search for the location of a system command in the path specified by the PATH variable and return the first search result. In other words, using the which command, you can see if a system command exists and exactly where it is executed.

1. Command format.

which executable file name

2. Command function.

The which command will search for the location of a system command in the path specified by the PATH variable and return the first search result.

3. Command parameters.

-n specifies the file name length, the specified length must be greater than or equal to the longest file name among all files.

-p is the same as the -n parameter, but here the path to the file is included.

-w Specifies the width of the field for output.

-V Display version information

4. Examples of use.

Example 1: Find files, display command paths

command.

which lsmod

Output: `which lsmod

[root@localhost ~]# which pwd

/bin/pwd

[root@localhost ~]# which adduser

/usr/sbin/adduser

[root@localhost ~]#

Description.

which searches for a runnable file based on the directory in the PATH variable configured by the user! So of course, different PATH configurations will find different commands!

Example 2: Use which to find which

command.

which which

Output.

[root@localhost ~]# which which

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

   /usr/bin/which

[root@localhost ~]#

Description.

There are two whichs, one of which is alias. This is called a “command alias”, meaning that typing which is equivalent to the command that follows it!

Example 3: Find the cd command

Command.

which cd

Description.

cd This commonly used command could not find ah! Why? This is because cd is a built-in command in bash! But which defaults to the directory specified in the PATH, so of course it can’t be found!

Reference:

www.cnblogs.com/peida/archive/2012/11/08/2759805.html

comments powered by Disqus