Find commands under Linux which, Whereis, locate, find (6/20)

Source: Internet
Author: User
Tags svn

The following 4 main commands are found under Linux: Which, Whereis, locate, and find.

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

Command parameters:

-n Specifies the length of the file name, which must be greater than or equal to the longest file name in all files.

-P is the same as the-n parameter, but the path to the file is included here.

-W Specifies the width of the field at the output.

-V Display version information

Role: Locate a command, from the environment variable path, locates/returns the path to the executable file that matches the specified name

Principle: When executing the which command, which will look in the current environment variable path for the executable file name that matches the name of the desired command, without the-a option, returning the path of the first matching executable, otherwise returning all the executable files that meet the criteria in turn The path name.

Application: Typically used to find the path where the command/executable is located. Sometimes the same command may exist under multiple paths, which can be used to find the command at which point the currently executing command is located.

(2) Whereis [-BMSU] filename1 filename2 ....

Functions: Locate the binary, source, and manual page files for a command. That is: Locate/return the path to the binary file, source file, and help book file that match the specified name.

Principle: The Whereis command first removes the prefix spaces in filename and any characters that begin with., and then finds the binary that matches the previously processed filename in the database (VAR/LIB/SLOCATE/SLOCATE.DB). files, source files, and help book files, you can manually update the database using the UpdateDB command before using it.

Application: Search for binary files, source files, and help book file paths.

The Whereis command locates the location of the executable file, the source code file, and the Help file in the file system. The properties of these files should belong to the original code, binary files, or Help files. The Whereis program also has the ability to search for source code, specify alternate search paths, and search for unusual items.

Command parameter:-B locates the executable file. -M locate the help file. -S Locate the source code file. -U searches the default path for files other than executables, source code files, and Help files.

-b Specifies the path to the search executable file. -m specifies the path to the search Help file. -s Specifies the path of the search source code file.

4. Usage examples:

Example 1: Find files related to * * files

Command: Whereis SVN

SVN:/usr/bin/svn/usr/local/svn/usr/share/man/man1/svn.1.gz

Description: Tomcat is not installed, can not find out, SVN installation found a lot of related files

Example 2: Only binary files are found

Command: Whereis-b SVN

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

Function: Find files by name from one or more databases prepared by UpdateDB. As with the Whereis directive, it is also found from the database-built index, where the command looks for all partially matched files and can be manually updated with the UpdateDB command before using the.

Principle: By default (when filename does not contain a wildcard character *), locate gives the path to all files that match *filename*.

Application: No Fuzzy Lookup of file type nature (you only remember part of a file name).

The locate command can quickly find the file when searching the database, the database is updated by the UpdateDB program, UpdateDB is created periodically by Cron Daemon, and the locate command searches the database faster than the entire data from the hard disk. But the worse is locate found the file if recently established or newly renamed, may not be found, in the default value, UpdateDB will run once a day, can be modified crontab to update the set value. (Etc/crontab)

Locate designated to search for eligible files, it will be stored in the file and directory name of the database, to find matching template style conditions of the file or directory, you can use special characters (such as "*" or "?" And so on) to specify the template style, as specified by Kcpa*ner, locate will find all files or directories that have a starting string of KCPA and end with NER, such as the name Kcpartner if the directory name is Kcpa_ner, all files including subdirectories are listed under that directory.

Locate command and find find files similar function, but locate is through the update program to the hard disk all the files and directory data first set up an index database, in the execution of LOACTE directly to find the index, query faster, the index database is generally managed by the operating system, However, you can also directly release the update to force the system to immediately modify the index database.

3. Command parameters:

-E will be excluded from the scope of the search.

-1 if it is 1. The security mode is started. In safe mode, the user does not see files that the permissions cannot see. This slows down because the locate must get access to the file in the actual file system.

-F excludes specific file systems, for example, we have no reason to put the files in the proc file system in the database.

-Q Quiet mode, no error message is displayed.

-N displays at most x outputs.

-R uses the normal arithmetic to do the searching condition.

-o Specifies the name of the data inventory.

-d Specifies the path of the repository

-H Display Auxiliary message

-V Displays the version message of the program

4. Usage examples:

Example 1: Find all files related to PWD

Command: Locate pwd

Example 2: Search all files in the ETC directory that begin with SH

Command: locate/etc/sh

Example 3: Search the ETC directory for all files starting with M

Command: locate/etc/m

(4) Find [option] [path1 path2 ...] [FileName]

Parameter description:

Time Lookup Parameters:

-atime N: List files that have been accessed in n*24 hours

-ctime N: List the files that have changed status within n*24 hours

-mtime N: Lists files that have been modified in 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 the file with the owner name called name

-name file: Look for files named file (wildcard characters can be used)

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

Principle: Traverse the current working directory and its subdirectories, the Find command is to traverse lookups on the hard disk, consuming hard disk resources, and finding efficiency is lower than whereis and locate.

Application occasions: can use which, whereis and locate when try not to use find.

A comparison of 4 commands is shown in the following table:

Find commands under Linux which, Whereis, locate, find (6/20)

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.