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)
Resources:
1. http://312788172.iteye.com/blog/730280
2. http://bbs.csdn.net/topics/320154707
3. http://zhidao.baidu.com/question/122024300.html
These five commands can also refer to the following content:
Find commands in Linux finds, locate, Whereis, which, type differences
1. Find
Www.2cto.com
Java code
Find is the most common and powerful look-up command you can use to find any file you're looking for. Unlike query database (/var/lib/locatedb) files, find finds disk space
Find uses the following format:
$ find < Specify directory > < specify conditions > < specify Actions >
-< specified directory;: the directory to be searched and all its subdirectories. The current directory is assumed by default.
-< specified conditions;: The characteristics of the file to be searched.
-< specify actions;: Specific processing of search results.
Www.2cto.com
If no parameters are added, find searches the current directory and its subdirectories by default, and does not filter any results (that is, all files are returned) and displays them all on the screen.
Examples of use of find:
$ find. -name ' my* '
Searches the current directory (with subdirectories, below), with all files with the file name beginning with my.
$ find. -name ' my* '-ls
Searches the current directory for all file names that begin with my, and displays their details.
$ find. -type f-mmin-10
Searches the current directory for all the normal files that have been updated in the last 10 minutes. If you do not add the-type f parameter, search for normal files + special files + directories.
2. Locate
Java code
The locate command is actually another way of writing "Find-name", but much faster than the latter because it does not search for a specific directory, but instead searches for a database (/var/lib/locatedb), which contains all the local file information. The Linux system automatically creates this database and updates it automatically once a day, so you can't find the latest changed files using the Locate command. To avoid this situation, you can manually update the database by using the UpdateDB command before using locate.
Examples of use of the Locate command:
$ locate/etc/sh
Search all files in the ETC directory that begin with SH.
$ locate ~/m
Search for all files that start with M in the user's home directory.
$ locate-i ~/m
Searches all files that begin with M in the user's home directory, and ignores case.
3. Whereis
Java code
The Whereis command can only be used for program name searches, and only binary files (parameter-B), man description file (parameter-m), and source code file (parameter-s) are searched. If the argument is omitted, all information is returned. As with locate, query database (/var/lib/locatedb) files
Examples of use of the Whereis command:
$ whereis grep
4. Which
Java code
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. That is, with the which command, you can see whether a system command exists, and the command that executes exactly which location.
Examples of use of the which command:
$ which grep
5. Type
Java code
The type command is not actually a lookup command, it is used to distinguish whether a command is brought by the shell or by a standalone binary file outside the shell. If a command is an external command, then using the-p parameter displays the path to the command, which is equivalent to the which command.
Www.2cto.com
Use instances of the type command:
$ type cd
The system will prompt that the CD is the Shell's own command (build-in).
$ type grep
The system prompts that grep is an external command and displays the path to the command.
$ type-p grep
When you add the-p parameter, it is equivalent to the which command.
Commands for finding files commonly found in Linux