One Linux command per day (+)--find command Xargs

Source: Internet
Author: User

When you use the-EXEC option of the Find command to process a matching file, the Find command passes all matching files to exec execution, but some systems have a limit on the length of the command that can be passed to exec, so that an overflow error occurs after the Find command runs for several minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.

In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there are too many processes, degraded system performance, and therefore inefficient, and the use of Xargs The command has only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

Usage examples:

Example 1: Find every normal file in your system, and then use the Xargs command to test what type of file they belong to

Command:

Find. -type F-print | Xargs file

Example 2: Find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file

Command:

Find/-name "core"-print | Xargs echo "" >/tmp/core.log

Example 3: Find files with read, write, and execute permissions for all users in the current directory and reclaim the appropriate write permissions

Command:

Find. -perm-7-print | Xargs chmod o-w

Example 4: Use the grep command to search all common files for the word hostname

Find. -type F-print | Xargs grep "hostname"

Example 5: Use the grep command to search all normal files in the current directory for the word hostnames

Command:

Find. -name \*-type F-print | Xargs grep "Hostnames"

Example 6: Performing MV with Xargs

Command:

Find. -name "*.log" | Xargs-i MV {} test4

Xargs prompt xargs:argument line too long solution after instance 7:find:

Command:

Find. -type F-atime +0-print0 | xargs-0-l1-t rm-f-l1 is processed one at a time,-T is processed before the print out command.

Good cheap, at the beginning I see he gave is "-ll", harm I measured half a day, all hints invalid parameters, also thought is two ll, the first also masked number 11, no justice. Finally found is L1, the front letter, the number behind.

Find. -type F-atime +0-print0 |  xargs-0-l2-t rm-f-l2 is processed 2 at a time,-T is processed before the print out command. (change to such a good understanding)

Example 8: Use the-i parameter default front output with {} instead, the-i parameter can specify other substitution characters, as in the example []

Command:

Find. -name "File" | Xargs-i [] CP []..

Use of the-p parameter for instance 9:xargs

Command:

Find. -name "*.log" |   Xargs-p-i mv {}. The-p parameter prompts you to confirm that the following command is executed, y executes, and N does not execute.

  

  

One Linux command per day (+)--find command Xargs

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.