Xargs of the linux——–find command

Reference blog:

findCommand xargs

findThe difference between – exec and xargs in the command is that the find command passes all matched files together to exec for execution, but passes all matched files in batches to xargs for execution.

When the number of files matched is too large, some systems restrict the length of commands that find commands can pass to – exec, which can lead to overflow errors such as’Too long parameter column’or’parameter column overflow’ after a few minutes of running the find command.

In some systems, using the – exec option initiates a corresponding process for each matched file, not all matched files are executed as parameters at one time; in some cases, too many processes and system performance degradation will occur, thus inefficient use of x;The args command has only one process. In addition, when using the xargs command, whether all parameters are obtained at once, or in batches, and the number of parameters obtained each time depends on the options of the command and the corresponding tunable parameters in the system kernel.

 

Chestnut:

1. Find files and test which files they belong to.

Order:

find  . -type f -print | xargs file Or find. -type f -exec file {};

Output:

./syncdata.log:    ASCII text

./fgExtractor.log: ASCII text

2.Locate the file and save the result to the aa.log file.

Command: find. type F – print | xargs echo “& gt;. / aa. log or find. type F – exec echo {} & gt;. / bb. log\;

3.Find all files in the current directory with read, write, and execute permissions for all users, and retrieve the corresponding write permissions

Command: find.perm-7-print | xargs Chmod O-W or find.perm-7-print-exec Chmod o-w{}\;

4.Use the grep command to search for the word hostname in all ordinary files.

Command: find. -type f -print xargs grep “GG”

Output is./tast1.txt:gggggggg

find . -type  f -exec grep ‘gg’ {} \;

Output is gggggggg

5.Using xargs to execute MV

Command: find. name “*. txt” | xargs – I'{}’ MV {} qpy11 or find. name “*. txt” exec MV {} qpy11;

Where the -I parameter specifies other characters to replace the matching characters.

6.Using xargs’s parameter -p

Command: find. -name “*.txt” |xargs -p -I [] MV [] qpy11/

-p The parameter will prompt you to confirm whether or not to execute the following command. Y executes, and N does not execute.

7.Using xargs to execute RM

Command: find. -name “*.txt” |xargs RM -rf

8.Custom controls the number of files processed each time.Re study)

When executing xargs meets’argument line too long’:

Command: find. -type f xargs -0 -L2 -t RM -f

-L2There are 2 operations at a time. -t prints commands before processing.  

Leave a Reply

Your email address will not be published. Required fields are marked *