Differences between which, whereis, locate, and find in Linux

Source: Internet
Author: User
Tags linux shell commands

We often search for a file or command in Linux, but do not know where it is stored. You can use the following commands to search.
Which
Whereis view the file location
Use locate with the database to view the file location
Find actual search hard disk Query file name

1. Which
Syntax: Which Executable File Name
For example:
[Root @ RedHat ~] # Which passwd
/Usr/bin/passwd
Which searches for executable files through the PATH environment variable, so the basic function is to find executable files.

2. whereis
Syntax: whereis [-bmsu] file or directory name
Parameter description:
-B: Only binary files are available.
-M: only find the file in the manual path of the instruction file.
-S: only find the source file
-U: file without instructions
For example:
[Root @ RedHat ~] # Whereis passwd
Passwd: /usr/bin/passwd/etc/passwd/usr/bin/X11/passwd/usr/share/man/man5/passwd.5.gz/usr/share/man/Man1/passwd.1.gz/usr /share/man/Man1/passwd.1ssl.gz
Search out all files related to the passwd file.

[Root @ RedHat ~] # Whereis-B passwd
Passwd:/usr/bin/passwd/etc/passwd/usr/bin/X11/passwd
Search for binary files only

Compared with find, whereis searches very quickly, because the Linux system records all files in the system in a database file, when whereis and the locate described below are used, data will be searched from the database, instead of traversing the hard disk like the find command, which will naturally be very efficient.
However, this database file is not updated in real time. It is updated once a week by default. Therefore, when we use whereis and locate to find the file, we sometimes find the deleted data, you can't find the file you just created because the database file is not updated.

3. Locate
Syntax: locate file or directory name
For example:
[Root @ RedHat ~] # Locate passwd
/Etc/passwd
/Etc/passwd-
/Etc/cron. daily/passwd
/Etc/init/passwd. conf
/Etc/init. d/passwd
/Etc/PAM. d/chpasswd
/Etc/PAM. d/passwd
/Etc/security/opasswd
............

4. Find
Syntax: Find path parameters
Parameter description:
Time search parameters:
-Atime N: lists the files that have been retrieved from the memory for N * 24 hours.
-Ctime N: lists the files or directories that have been changed or added within N x 24 hours.
-Mtime N: lists the files or directories modified within N x 24 hours.
-Newer file: Lists files that are newer than file.
Name Search parameters:
-Gid N: Find the file whose group ID is N.
-Group Name: Find the file with the group name
-Uid n: Find the file whose owner ID is N.
-User name: Find the file whose user name is name
-Name file: searches for files with file names (wildcards can be used)
For example:
[Root @ RedHat ~] # Find/-name zgz
/Home/zgz
/Home/zgz
/Home/WebLogic/BEA/user_projects/domains/zgz
/Home/Oracle/product/10g/export toollogs/dbca/zgz
/Home/Oracle/product/10g/export toollogs/emca/zgz
/Home/Oracle/oradata/zgz

[Root @ RedHat ~] # Find/-name '* zgz *'
/Home/zgz
/Home/zgz/zgz1
/Home/zgz/zgzdirzgz
/Home/zgz
/Home/zgz/zgzdir
/Home/WebLogic/BEA/user_projects/domains/zgz
/Home/WebLogic/BEA/user_projects/domains/zgz. log00006
/Home/WebLogic/BEA/user_projects/domains/zgz. log00002
/Home/WebLogic/BEA/user_projects/domains/zgz. log00004
/Home/WebLogic/BEA/user_projects/domains/zgz. Log
/Home/WebLogic/BEA/user_projects/domains/zgz. log1_8
/Home/WebLogic/BEA/user_projects/domains/zgz. log00005

When we use whereis and locate to find the files we need, we can use find, but find is to traverse the search on the hard disk, which consumes a lot of hard disk resources, and the efficiency is very low. Therefore, we recommend that you prioritize whereis and locate.

Summary:
Which can only query executable files and aliases (alias), and find
Whereis can only query binary files (including executable files), instructions, and source files, from the Linux File database (/var/lib/slocate. DB or/var/lib/mlocate. DB ).

Locate is searched in the database. The database is updated every day and the file name is partially matched (see the result of 3 locate passwd: opasswd)
Find is the most powerful and can be used to query everything. Search for files based on conditions and find files on hard disks, which is very inefficient.

Reference recommendations:

Differences between which, whereis, locate, and find commands in Linux

Summary of common Linux shell commands and directory partitions

Find command in Linux

Linux xargs command

Linux soft link and hard link

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.