Find files under Linux: Differences between which, Whereis, locate, and find commands

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.
Which view the location of the executable file, check the environment variables
Whereis View the location of the file, check the database, update it weekly
Locate with database to view file location, database check, weekly update
Find actual search hard disk query file name, check hard disk

1, which
Grammar:
[[email protected] ~]# which executable file name
For example:
[email protected] ~]# 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:
[[email protected] ~]# 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:
[Email protected] ~]# 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

[Email protected] ~]# 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 single database file, and when you use Whereis and the locate described below, the data is looked up from the database, not like the Find command, By traversing the hard drive to find, the efficiency will naturally be very high.
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)

Find files under Linux: Differences between which, Whereis, locate, and find commands

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.