The difference between which, whereis, locate, and find commands under Linux

Source: Internet
Author: User

We often find a file in Linux, but we do not know where to put it, you can use some of the following commands to search. These are the information found on the Internet (reference 1), because sometimes it is not used for a long time, and when it is used, it is often confused, so it is easy to use.

    • Which viewing the location of an executable file
    • Whereis viewing the location of a file
    • Locate to view file locations with a database
    • Find actual search hard disk query file name (find can also be based on file size-size time-atime Regular expression-regex)
1, which

Grammar:
[Root @redhat ~]# which executable file name
For example:
[Root @redhat ~]# which passwd
/usr/bin/passwd
Which is to find the executable file through the PATH environment variable to that route, so the basic function is to find the executable file

2, Whereis

Grammar:
[Root @redhat ~]# whereis [-bmsu] file or directory name
Parameter description:
-B: Only binary files are found
-M: Only files found under the manual path of the description file
-S: Find source files only
-u: File without document description
For example:
[Root @redhat ~]# Whereis passwd
passwd:/usr/bin/passwd/etc/passwd/usr/share/man/man1/passwd.1.gz/usr/share/man/man5/passwd.5.gz
Find the files associated with the passwd file

[Root @redhat ~]# whereis-b passwd
passwd:/usr/bin/passwd/etc/passwd
Only binary files are found

Whereis looks very fast compared to find, because the Linux system records all the files in the system in a database file (as described in reference 1 and most articles, Whereis is found in a database file, In reference 2 to find the database file directory/var/lib/slocate/slocate.db, I did not find this directory on the server, because I did not install locate command, then Whereis exactly how to find it? Looking for a long time no, from reference 3 there is a personal comparison of the answer that I believe, from/{bin,sbin,etc}/usr{lib,bin,old,new,local,games,include,etc,src,man,sbin,x386,tex, G++-include}
/usr/local/{x386,tex,x11,include,lib,man,etc,bin,games,emacs} In the search, also did not go to see the source of Whereis, if there is a definite can exchange a bit, When using Whereis and the locate described below, the data is looked up from the database, rather than being looked up by traversing the hard disk like the Find command, which is naturally very efficient.
However, the database file is not updated in real time and is updated once a week by default, so when we use Whereis and locate to find files, we sometimes find data that has been deleted, or just created the file, but cannot find it because the database file is not updated.

3, locate
Grammar:
[[Email protected] ~]# Locate file or directory name
For example
[Email protected] ~]# locate passwd
/home/weblogic/bea/user_projects/domains/zhanggongzhe112/myserver/stage/_appsdir_db_war/db.war/jsp/as/user/ passwd.jsp
/home/weblogic/bea/user_projects/domains/zhanggongzhe112/myserver/stage/_appsdir_admin_war/admin.war/jsp/ platform/passwd.jsp
/lib/security/pam_unix_passwd.so
/lib/security/pam_passwdqc.so
/usr/include/rpcsvc/yppasswd.x
/usr/include/rpcsvc/yppasswd.h
/usr/lib/perl5/5.8.5/i386-linux-thread-multi/rpcsvc/yppasswd.ph
/usr/lib/kde3/kded_kpasswdserver.la
/usr/lib/kde3/kded_kpasswdserver.so
/usr/lib/ruby/1.8/webrick/httpauth/htpasswd.rb
/usr/bin/vncpasswd
/usr/bin/userpasswd
/usr/bin/yppasswd
............

4. Find
Grammar:
[[email protected] ~]# Find Path parameter
Parameter description:
Time Lookup Parameters:
-atime N: Lists the files accessed within the n*24 hours
-ctime N: List the changes, additions, or directories in n*24 hours
-mtime N: Lists files or directories that have been modified within n*24 hours
-newer file: List files that are newer than
Name Lookup parameters:
-gid N: Looking for a file with group ID n
-group Name: Find a file with the name of the group
-uid N: Looking for a file with owner ID n
-user Name: Find a file with the user name called name
-name file: Look for files named file (wildcard characters can be used)
For example
[Email protected] ~]# Find/-name Zgz
/home/zgz
/home/zgz/zgz
/home/weblogic/bea/user_projects/domains/zgz
/home/oracle/product/10g/cfgtoollogs/dbca/zgz
/home/oracle/product/10g/cfgtoollogs/emca/zgz
/home/oracle/oradata/zgz

[[email protected] ~]# Find/-name ' *zgz* '
/home/zgz
/home/zgz/zgz1
/home/zgz/zgzdirzgz
/home/zgz/zgz
/home/zgz/zgzdir
/home/weblogic/bea/user_projects/domains/zgz
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00006
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00002
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00004
/home/weblogic/bea/user_projects/domains/zgz/zgz.log
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00008
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00005

When we can't find the files we need with Whereis and locate, we can use find, but find is traversing lookups on the hard disk, so it consumes hard disk resources and is very inefficient, so it is recommended that you use Whereis and locate first.
Locate is found in the database, and the database is updated as large as daily.
Whereis can find executable commands and man page
Find is finding files based on criteria.

which can find executable files and aliases (alias)

The difference between which, whereis, locate, and find commands under Linux

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.