Summary of 4 search commands which, whereis, locate, find under Linux

 

(1)which   [-a]    cmdname1 cmdname2 ......

           Function: locate a command, from the environment variable PATH, locate/return the path where the executable file matching the specified name is located

           Principle: When the which command is executed, which will search for the executable file name that can match the name of the command in the current environment variable PATH in turn, without the -a option, return the path of the first matching executable file, otherwise return in turn Pathnames of all executables that satisfy the condition.

           Applicable occasions: Generally used to find the path where the command/executable file is located. Sometimes the same command may exist in multiple paths. This command can be used to find out which position the currently executed command is. 

           

(2)whereis    [-bmsu]    filename1  filename2 .......

          Function: locate the binary, source, and manual page files for a command. Namely: locate/return the path where the binary file, source file and help manual file matching the specified name are located.

          Principle: The whereis command first removes the prefix spaces and any characters starting with . in the filename, and then searches the database (var/lib/slocate/slocate.db) for the binary files, source files that match the above processed filename. Before using the documentation and help manual files, the database can be manually updated using the updatedb command.

          Applicable occasions: Binary file, source file and help manual file path search.

          

(3)locate    [option]    filename1 filename2 ......

         Function: find files by name from one or more databases prepared by updatedb. Like the whereis command, it is also searched from the index established by the database. The difference is that this command finds all partially matching files. Before using it, you can use the updatedb command to manually update the database .

         Principle: By default (when filename does not contain the wildcard *), locate will give the path of all files matching *filename*.

         Applicable occasions: Fuzzy search without file type nature (you only remember part of the name of a file).

         

(4)find    [option]    [path1  path2  ......]    [filename]

          Parameter Description:

                     Time lookup parameters:

                                 -atime n: List files accessed within n*24 hours

                                 -ctime n: List files whose state has changed within n*24 hours

                                 -mtime n: List files that have been modified within n*24 hours

                                 -newer file: list files that are newer than file

                      Name lookup parameters:

                                 -gid n: find files with group ID n

                                 -group name: Find files with group name name

                                 -uid n: find files with owner ID n

                                 -user name: find files with owner name

                                 -name file: Find the file with the file name file (wildcards can be used)

  Role: search for files in a directory hierarchy. Search files recursively from the current directory.

          Principle: Traverse the current working directory and its subdirectories. The find command traverses and searches on the hard disk, which consumes a lot of hard disk resources, and the search efficiency is lower than whereis and locate.

          Applicable occasions: Try not to use find when you can use which, whereis and locate.

          

          

 

          The comparison of the 4 commands is shown in the table below:

          

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291852&siteId=291194637