Find and Xargs small traps

Source: Internet
Author: User



Find and Xargs

1 Find command format with basic parameters
The basic format of the Find command is: Find Pathname-options [-print-exec-ok]
The specific parameters are as follows:
Pathname: Finding a path
-name: Search by document name
-perm: Search by Document permissions
-prune: Not found in the currently specified path. If the-depth option is specified at the same time, prune is ignored
-user: Search by document owner
-group: Search by Group of documents
-mtime-n +n: Search by document change time. -n refers to the current time n days or less than +n n days
-nogroup: Finding invalid genus Group document
-nouser: Finding invalid owner document
-newer file1!file2: Find change times than file1 file2 older documents
-type: Finding a document of a type
B: Block Device documentation
D: Catalogue
C: Character device documentation
P: Piping Documentation
L: Symbolic Link document
F: General Documentation
-size N[c] finds documents with a document length of n block with [C] indicating document length in bytes
-depth: When looking, first find the current directory document, and then find it in its subdirectories
-fstype: Find documents in a document system of a certain type, document system type can be found in/etc/fstab
-mount: Find documents do not cross the document System mount point
-follow: If a linked document is encountered, track to the document that the link points to
-cpio: Use the Cpio command with a matching document to back up the document to a tape device
-print: Output a matching document to standard output
-exec: Executes the given shell command against the matching document. The form is: command {} \; Note the space between {} and \;
-ok: Same as-exec function. Only the shell command given by this parameter is executed in a more secure mode. Before each command is executed, a prompt is given to let the user determine whether to perform
The above command parameters are not necessary to master, only need to master the more practical, the other to use when the query is more convenient.
2 Example of the Find command practical scenario
Find/-name "[a-z]*"//in the root directory and its subdirectories look for files with file names beginning with lowercase letters, note that this is not a regular expression.
Find./-user Barry//Locate the file in the current directory owner is Barry's file.
Find/barry-type d//Find files of type directory in the Barry directory
Find/barry-mtime +5//In the Barry directory find files Last updated 5 days ago
find/barry-ctime-3//Find files that last updated within 3 days, note that this is the time to update the file attributes, not the file contents
Find/barry-size +5k//Find files with file size above 5k
The above lookup criteria can be used at the same time to find files that meet multiple criteria at the same time.
Find./-type f-exec rm {} \; To execute the RM command for the found eligible file, note the space between {} and \
However, there is a problem, when the file is found very long, for each file to execute the RM command, resulting in a number of processes, resource consumption is more severe. So Xargs is smart, and it doesn't produce multiple processes.
3 xargs Command
The Xargs command receives a piece of data, which is a parameter list, and each segment is a parameter according to the specified delimiter. For example:
Find./-type F | Xargs RM
This will execute the RM command for each file found, similar to the-exec function.
However, the problem comes, when find found the path of the file has a space, xargs will divide the path into two segments, each paragraph as a parameter, the default xargs is the space to ask the segmentation basis.
If you let the Xargs and find command take another special character as a delimiter, the problem is not solved, so that xargs will not misunderstand find.
The find command can use the-PRINT0 option to take the output of the Find command as a delimiter with the null character ' 0 ', which means that the path is separated by a null character.
Similarly, the Xargs command uses-zero to specify a null character as a delimiter, extracting parameters from the data passed in. As follows:
Find./-type f-print0 | xargs-0 RM
This will be a harmony.


Original

Http://www.cnblogs.com/barrychiao/archive/2012/10/10/2718777.html

Find and Xargs small traps

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.