The problem of the exec

Parameter is too long

When a matched file is processed using the -exec option of the find command, the find command passes all matched files together to exec execution. However, some systems have a limit on the length of the command that can be passed to exec, resulting in an overflow argument line too long a few minutes after the find command is run. The error message is usually “parameter column is too long” or “Parameter column overflow”. This is where the xargs command comes in handy, especially in conjunction with the find command.

The find command passes the matched files to the Xargs command, which fetches only a portion of the files at a time rather than all of them, unlike the -exec option. This allows it to process the first set of files retrieved, then the next batch, and so on.

The process too much

On some systems, using the -exec option triggers a process for each matched file, rather than executing all matched files at once; In some cases, there will be too many processes, system performance decline, so the efficiency is not high; With the xargs command, there is only one process. In addition, when using the Xargs command, whether to fetch all the parameters at once or in batches, and the number of parameters at a time, depends on the command options and the corresponding tunable parameters in the system kernel.

Querying all permissions in the current directory Yes777The file

> find . -perm 777 | xargs ls -al
Copy the code

Query all files in the current directory.txtFile and output to the specified filea.log(No line breaks)

> find . -name *.txt | xargs echo > /tmp/a.log
Copy the code

Xargs simply changes \n to a space, so there may be some problems. For example, if the file name has a space, xargs will have a problem.

use-nSpecifies the number of lines of output

  • -n num Specifies the number of arguments to be used at one time. By default, all arguments are used.

Query all files in the current directory.txtFile and output to the specified fileb.log(With line breaks)

> find . -name *.txt | xargs -n 1 echo > /tmp/b.log 
Copy the code

-n 1 Displays only one output in each line

Query The read, write, and execute permissions of all the files in the current directory and revoke the corresponding write permissions

> find . -perm 777 | xargs chmod o-w
Copy the code

Find all in the current directory.txtFile and find containedrumenzString file

> find . -name *.txt | xargs grep -n "rumenz"
Copy the code

Find all in the current directory.txtFile and move to the specified directory

> find . -name *.txt | xargs -i cp -rf {} /tmp; 
Copy the code

The -i argument replaces the standard output of the pipe with {}

With the -i argument, the default leading output is replaced with {}. The -i argument can be used to customize the leading output symbol

Customizing the placeholder for the previous output is []

> find . -name *.txt | xargs -I [] cp -rf [] /tmp
Copy the code

-p Indicates whether to execute the command

> find . -name *.log | xargs -p -i mv {} /tmp
Copy the code

The -p argument prompts you to confirm whether to execute the following command,y does, n does not.

Downloading files in batches

Url. TXT are all required to download the link address

> cat url.txt | xargs wget -c
Copy the code

Summary: When to use it-i

If the command can be followed by content and there is no destination path, omit -i; otherwise, add -i.

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station