How to use the which command in linux (find files)

Source: Internet
Author: User
We often search for a file in linux, but do not know where it is stored. you can use the following commands to search

We often search for a file in linux, but do not know where it is stored. you can use the following commands to search:

Which: view the location of the executable file.
Whereis view the file location.
Use locate with the database to view the file location.
Find actual search hard disk query file name.

The which command searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result. That is to say, by using the which command, you can see whether a system command exists and where the command is executed.

1. command format:

Which executable file name

2. command functions:

The which command searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result.

3. command parameters:

-N indicates the length of the file name. the specified length must be greater than or equal to the longest file name in all files.
The-p and-n parameters are the same, but the file path is included here.
-W specifies the width of the field when the output is made.
-V: Display version information

4. example:

Instance 1: search for files and display command paths

Command:

Which lsmod

Output:


Copy codeThe code is as follows:
[Root @ localhost ~] # Which pwd
/Bin/pwd
[Root @ localhost ~] # Which adduser
/Usr/sbin/adduser
[Root @ localhost ~] #

Note:

Which searches for executable files based on the directory in the PATH variable configured by the user! Therefore, the commands found for different PATH configurations are certainly different!

Instance 2: Use which to locate which

Command:

Which

Output:


Copy codeThe code is as follows:
[Root @ localhost ~] # Which
Alias which = 'Alias |/usr/bin/which -- tty-only -- read-alias -- show-dot -- show-tilde'
/Usr/bin/which
[Root @ localhost ~] #

Note:

There are actually two which, one of which is alias. this is the so-called "command alias", which means that the input of which will wait for the subsequent commands!

Instance 3: Find the cd command

Command:

Which cd

Output:

Note:

The common cd command cannot be found! Why? This is because cd is a built-in bash command! However, by default, which finds the standard directory in the PATH, so it must not be found!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.