The difference between the locate command and the Find command (turn)

Source: Internet
Author: User

Http://blog.chinaunix.net/u2/62156/showart_498502.html

One: Locate command
The locate command is used to locate files, which are faster than the Find command, and require a database that is established by routine work (crontab) procedures on a daily basis. Once we have established this database, we can easily search for the required files.
That is, run first: updatedb (either in that directory, can be placed in crontab), the SLOCATE.DB database can be quickly found under/var/lib/slocate/. Execute the #updatedb command directly from the command prompt:
For example: Find related word issue
$ locate Issue
/etc/issue
/etc/issue.net
/usr/man/man5/issue.5
/usr/man/man5/issue.net.5
Two: Find command:
Function: Searches the directory structure for files and performs the specified actions. This command provides a considerable number of lookup conditions and is powerful.
Syntax: Find start directory look for condition actions
Description: The Find command starts from the specified starting directory, recursively searches its subdirectories, finds the files that satisfy the search criteria, and takes related actions.
This command provides a search condition that can be a composite condition consisting of a logical operator not, and, or. The meaning of the logical operator and, or, not is:
(1) and: logic and, expressed as "a" in the command, is the default option for the system, indicating that the condition is satisfied only if the given condition is satisfied. For example:
$ Find–name ' tmp ' –xtype c-user ' inin '
This command looks for all three files that are satisfied with the given condition.
(2) or: logic or, in the command, "-O". This operator indicates that a condition is satisfied if one is satisfied in the given condition. For example:
$ Find–name ' tmp ' –o–name ' mina* '
 
This command queries all files named ' tmp ' or matching ' mina* '.
(3) Not: logical, in command. Said This operator represents the lookup of a file that does not meet the given criteria. For example:
$ find! –name ' tmp '
This command queries all files for which the filename is not ' tmp '.
It should be explained that when you use a lot of logical options, you can enclose these options in parentheses. To avoid misinterpretation of parentheses by the shell itself, it is necessary to add the escape character "" before the word number to remove the meaning of the parentheses.
Example: $ find (–name ' tmp ' –xtype c-user ' inin ')
Look for conditions with the following options:
First, the n values in each of the following options can have three input methods, assuming N is 20, then:
+20 means after 20 (21,22,23 etc.)
-20 indicates 20 ago (19,18,17 etc.)
20 says it's exactly 20.
1. Find by name and file attribute.
-Name ' string ' finds the file name matches all the files for the given string, and the wildcard character * 、?、 [] is available within the string.
-lname ' string ' lookup file name matches all symbolic link files for the given string, and the wildcard character * 、?、 [] is available within the string.
-gid N finds all files that belong to a user group with an ID number of n.
-uid N finds all files belonging to the user with ID number n.
-group ' string ' looks for all files that belong to the given string named by the user group.
-user ' string ' finds all files that belong to a given string of user names.
-empty find directories or files of size 0.
-path ' string ' finds all files that match the given string, and the wildcard character * 、?、 [] is available within the string.
-perm permissions to find files and directories with the specified permissions, the representation of the permission can be as 711,644.
-size n[bckw] looks for files of the specified file size, and the characters after n indicate units, the default is B, which represents a 512-byte block.
-type x finds a file of type X, and X is one of the following characters:
B Block Device file
C Character device file
D Catalog Files
P Named pipe (FIFO)
F Normal File
L Symbolic Link file (Symbolic links)
s socket file
-xtype x is basically the same as-type, but finds only symbolic link files.
2. Take time as a condition to find
-Amin N finds all files that have been accessed before n minutes.
-Atime n finds all files that have been accessed before n days.
-Cmin n finds all files where the file state was modified before n minutes.
-CTime n finds all files where the file state was modified before n days.
-Mmin n finds all files where the contents of the file were modified before n minutes.
-Mtime n finds all files where the contents of the file were modified before n days.
3. Executable operation
-EXEC command name {} Executes the given Linux command on a qualifying file without asking the user if the command needs to be executed. {} indicates that the command's argument is the found file; the end of the command must end with ";".
-The OK command name {} Executes the given Linux command for a qualifying file, and unlike exec, it asks the user if the command needs to be executed.
-LS lists all the files found in detail.
-fprintf FileName Writes the file name found to the specified file.
-Print Displays the file name found on the standard output device.
-printf format format please refer to the C language book.
Example 1: Finds all files in the current directory that start with main and displays the contents of those files.
$ find. -Name ' main* '-exec more {};
Example 2: Deletes a. Out or *.O file that has not been accessed in the current directory for all weeks.
$ find. (-Name A.out-o-name ' *.O ')
>-Atime +7-exec rm {};
The description is as follows:
  "." In the command. Represents the current directory, where find will start with the current directory, looking for files in its subdirectories one at a time that meet the criteria that you specify later. (and) represents parentheses (), where "" is called an escape character. This is written because for the shell, (and) has a different meaning, rather than the use here for the combination of conditions. "-Name a.out" means to look for a file named a.out; "-name ' *.o ' means a file to find all names ending with. O. Between these two-name-O representations of logic or (or), that is, find the file with the name A.out or the end of. O, find the file in the current directory and its subdirectories, and then make a judgment to see if the last access time was 7 days ago (-atime +7), if, The command RM (-exec rm{};) is executed on the file. where {} represents a qualifying file name that is currently found; The last line in the first line of the above command is a continuation character. When the command is too long to write on a single line, you can enter one, and then the system will display a, indicating that the user continues to enter the command.

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.