Detailed description of the find command in linux and the linuxfind command

Source: Internet
Author: User

Detailed description of the find command in linux and the linuxfind command

Http://blog.csdn.net/pipisorry/article/details/39831419

Find Syntax:

Find [start Directory] search for conditional operations

Find path option [-exec COMMAND {}\;]

Because the find command will perform recursive search on the files and Their subdirectories starting from the given directory based on the option, that is, the search condition.

Search criteria:

The search condition in this command can be a composite condition consisting of not, and, or, a logical operator.

(1) and: logic and is represented by "-a" in the command. It is the default option of the system. It indicates that the search condition is met only when all the given items are met. For example:

Find-name 'tmp '-xtype c-user 'inin' % This command looks for all the files whose conditions are met

(2) or: logical or, represented by "-o" in the command. This operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:

Find-name 'tmp '-o-name 'mina' *' % This command queries all files whose names are 'tmp 'or match 'mina.

(3) not: Non-logical. Use "!" in the command. . This operator is used to search for objects that do not meet the given conditions. For example:

Find! -Name 'tmp '% This command queries all files whose names are not 'tmp.

Note: When usedMany logical options can be enclosed in parentheses.Include these options. To avoid Shell misunderstanding, you must add the Escape Character "\" before the phone number to remove the meaning of the brackets. Example:

Find \ (-name 'tmp '-xtype c-user 'inin '\)

Query condition option parameter:

-Name 'string': searches for all files whose names match the given strings. Wildcards *,? , [].

-Lname 'string': searches for all symbolic link files of the given string matching the file name. Wildcards *,? , [].

-Gid n: searches for all files belonging to the user group with ID n.

-Uid n: searches for all files belonging to users with ID n.

-Group 'string': searches for all files belonging to the string given by the user group name.

-User 'string': searches for all files belonging to the string given by the user name.

-Empty: searches for directories or files with a size of 0.

-PathThe 'string' searches for all files whose path names match the given strings. Wildcards *,? , [].

-Perm permission: searches for files and directories with the specified permission. The permission expression can be 711,644.

-Size n [bckw]: searches for objects of the specified file size. The character after n indicates the unit. The default value is B, which indicates the block size of 512 bytes.

-Type x: Search for objects of the x type. x is one of the following characters:

B device files, c character device files, d directory files, p name pipeline (FIFO)

F common file; l symbolic link file (symbolic links); s socket file;-xtype x and-type are basically the same, but only search for symbolic link files.

Search by Time

-Amin n: searches for all files that were accessed n minutes ago.

-Atime n: searches for all files that have been accessed n days ago.

-Cmin n: searches for all files whose status has been modified n minutes ago.

-Ctime n: searches for all files whose status has been modified n days ago.

-Mmin n: searches for all files whose contents have been modified n minutes ago.

-Mtime n: searches for all files whose contents have been modified n days ago.

-Print: outputs the search result to the standard output.

Example: in root and subdirectory search does not include directory/root/bin, greek user, file type is normal file, 3 days ago named test-find.c file, and output the structure. The find command is as follows:

Find/-name "test-find.c"-type f-mtime + 3-user greek-prune/root/bin-print

Of course, here,-Print is a default option, so we do not have to configure it..

Exec option:

-Exec: Specifies the shell command for the structure command to be searched. Note that the format must be correct: "-exec command {}\;"

There must be spaces between} And;

{} Indicates that the command parameter is the file found. The end of the command must end.

Example: run the following command to delete the files found in the preceding example:

Find/-name "test-find.c"-type f-mtime + 3-user greek-prune/root/bin-exec rm {}\;

Find command instance:

Find.-name 'main * '-exec more {}\;% find all files starting with main in the current directory and display the content of these files.

Find. \ (-name. out-o-name '*. o' \)>-atime + 7-exec rm {}\; % Delete. out or *. o file.

% "\ (" And "\)" indicate parentheses (), and "\" is called escape characters. The reason for writing this is that (and) have different meanings for Shell, rather than the purpose of combining conditions.

% "-Name a. out" refers to the file named a. out;

% "-Name '*. o'" refers to all files whose names end with. o. The-o between the two-names indicates the logic or (or), that is, to find the file named a. out or whose name ends with. o.

The % find Command finds the token file in the current directory and Its subdirectories, and then checks whether the last access time is 7 days ago (condition-atime + 7). If yes, run rm (-exec rm {}\;) on the file {}\;).

{} Indicates the name of the file that meets the criteria found, and \; indicates the syntax requirements.

% The Last \ In the first line of the above command is the line feed. When a command is too long to be written in a row, you can enter one \. Then, the system displays one>, indicating that the user can continue to enter the command.



Problem:

The find command in linux does not contain some directories.

The find command ignores one or more sub-directories.

Solution:

When you use find in linux, you can use the-prune parameter to filter directories that are not searched. The path parameter to be ignored must be followed by the search path, otherwise, this parameter does not work.

Man find

...-path pattern              File  name matches shell pattern pattern.  The metacharacters do              not treat `/' or `.' specially; so, for example,                        find . -path "./sr*sc"              will print an entry for a directory called `./src/misc' (if  one              exists).   To  ignore  a whole directory tree, use -prune rather              than checking every file in the tree.  For example, to skip  the              directory  `src/emacs'  and  all files and directories under it,              and print the names of the other files found, do something  like              this:                        find . -path ./src/emacs -prune -o -print              Note that the pattern match test applies to the whole file name,              starting from one of the start points named on the command line.              It  would  only  make sense to use an absolute path name here if              the relevant start point is also an absolute path.   This  means              that this command will never match anything:                        find bar -path /foo/bar/myfile -print              The  predicate -path is also supported by HP-UX find and will be              in a forthcoming version of the POSIX standard....

You can also use the parameter-wholename, but it is not recommended.
 -wholename pattern              See -path.    This alternative is less portable than -path.
-Prune uses this option to make the find command not to be searched in the specified directory. If the-depth option is used at the same time,-prune will be ignored by the find command.

Also note \ (Leading and trailing Spaces

Eg:

Root @ ubuntu:/tmp1 # find. /-type f #/tmp1 all files in all folders. /file. /1/1. cpp. /2/2. cpproot @ ubuntu:/tmp1 # find. /-path. /1-prune-o-type f-print #/tmp1 find the file but ignore the file in the folder/1. /file. /2/2. cpproot @ ubuntu:/tmp1 # find. /\ (-path. /1-o-path. /2 \)-prune-o-type f-print #/tmp1 searches for files and ignores the files in the folder/1/2. /file

Use-path (or-wholename) for the file mode "./1". If this mode has been found,-prune can prevent find from going to this directory.

Find./-path./1-prune-o-type f-print
Find [-path...] [expression] After the path list is an expression
-Path./1-prune-o-print is-path./1-a-prune-o
Boolean "-o" enables find to process the rest of the command for other directories. Since there is a hypothetical implicit and operator (-a) between each expression, if the left expression is calculated as false, the expression after and is not calculated; therefore, the-o operator is required.
[Detailed explanation: the short expression of-print is evaluated in order, and-a and-o are both short-circuit values, similar to shell & | if-path. /1 is true, then evaluate-prune,-prune returns true, and the logical expression... -a-prune is true; otherwise, the value-prune is not required, and the logical expression is false.
If-path. /1-a-prune is false, then evaluate-print,-print returns true, or the logical expression is true; otherwise, do not ask the value-print, or the logical expression is true.] The combination of this expression can be written as if-path./1 then
-Prune
Else
-Print
Find ignores Multiple folders Find./\ (-path./1-o-path./2 \)-prune-o-type f-name "*"-print
Parentheses indicate the combination of expressions. \ Indicates a reference, that is, it indicates that shell does not give a special explanation for the subsequent characters, but leaves it to the find command to explain its meaning.
Search for a specific file, and add-name and other options after-o
Eg: count how many lines of c/c ++ code I have written:
Root @ ubuntu:/media/E/mine/C ++ # find. /\ (-path. /SvdFeature-o-path. /SvdFeatureDemo-o-path. /SvdFeatureInfer-o-path. /testSVD \)-prune-o \ (-name '*. c '-o-name '*. cpp '-o-name '*. h'-type f \)-print | xargs wc-l | more
...
16867 total usage


From: http://blog.csdn.net/pipisorry/article/details/39831419

Ref:

Find

Use of the Find command in linux

The linux find command ignores one or more sub-directories.



How to Use the Linux find command?

Find [start Directory] search for conditional operations

There is a way to express it: find path option [-exec COMMAND {}\;]
The find Command performs recursive search on the files in the directory and Their subdirectories Based on the option, that is, the search condition, the Search Condition in the command can be a composite condition composed of not, and, or, and (1) and: logical and, represented by "-a" in the command, is the default option of the system, indicating that only when all the given parts meet the search conditions are met. For example:

Find-name 'tmp '-xtype c-user 'inin'

% This command looks for all files whose conditions are met

(2) or: logical or, represented by "-o" in the command. This operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:

Find-name 'tmp '-o-name 'mina *'

% This command is used to query all objects whose names are 'tmp 'or match 'mina.

(3) not: Non-logical. Use "!" in the command. . This operator is used to search for objects that do not meet the given conditions. For example:

Find! -Name 'tmp'

% This command is used to query all objects whose names are not 'tmp. Note: When many logical options are used, you can enclose these options in parentheses. To avoid Shell misunderstanding, you must add the Escape Character "\" before the phone number to remove the meaning of the brackets. Example:

Find \ (-name 'tmp '-xtype c-user 'inin '\)

Linux and find commands

* In the find command is not a wildcard, but a regular expression. In the regular expression:
* Matches zero or multiple characters that match exactly before it. For example, the regular expression. * means that it can match any number of characters.
Refer to Encyclopedia:
Reference: baike.baidu.com/view/94238.htm

Related Article

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.