Linux --- command --- xargs usage if you have spent a lot of time on Unix command lines, you may have seen xargs. If you have not heard of xargs, let me explain it first, xargs is a program that input or execute commands from the standard. Common use I often see the combination of find and xargs to perform some operations on the list of objects returned by find. Update: Based on feedback on Twitter and hacker news, find is a very powerful command. It also has parameters such as-exec and-delete that can be used to execute commands and delete files, even so, if you don't know much about the find options, or you can't remember the odd syntax-exec, you can still use the simpler xargs, in addition, xargs is superior in efficiency. Recursively search for all Python files and count the number of lines in the file. find.-name '*. py' | xargs wc-l Recursively search for all Emacs Backup files and delete: find.-name '*~ '| Xargs rm recursively searches for all Python files and searches for the import Statement: find. -name '*. py' | xargs grep 'import': there is a problem in the example above when processing spaces in the file/directory name. If the file or directory name contains spaces, there may be some problems, this is because xargs divides input based on the blank space by default. A simple solution is to tell find to use NUL (\ 0) to split the result (by providing the-print0 option to find ), it also tells xargs to use Nul to separate input (-0 ). Delete the backup file, even if it contains spaces: find.-name '*~ '-Print0 | xargs-0 rm parameter location in the above example, xargs reads all non-null elements from the standard input and connects them to provide them to the given command for execution, this is useful in many cases. Even so, sometimes you may want to insert a parameter in the middle of the command, and the-J mark will come in handy, xargs adds the input to the string after the-J parameter and then executes it. Copy all backup files to the backups Directory: find.-name '*~ '-Print 0 | xargs-0-J % cp % ~ The maximum command length of/backups is sometimes input to xargs through pipelines, which may cause the command to be executed to exceed the maximum command line length limit. You can use the following command to obtain the maximum command line length: getconf ARG_MAX to avoid exceeding the limit, xargs has its own maximum length limit for the result command. If the provided parameters may cause the called command to exceed the length limit, xargs splits the input into multiple parts and calls the target command multiple times. The default length limit is 4096, which may be far smaller than the ARG_MAX setting of most current systems, you can override this default setting by providing the-s identifier to xargs. This is especially useful when you operate on a large source code repository. Specify the number of parameters. If the command you want to execute only accepts one or two parameters, for example, you can use the diff command to compare the two files, the-n option of xargs is very useful. It can specify several parameters for the target command at a time. If the number of parameters is greater than the number you set, the command will be called repeatedly, until all input is executed. Note that the number of parameters called last time may be less than the specified number. Let's take a simple example: $ echo {0 .. 9} | xargs-n 20 12 34 56 78 9 Similarly, you can also use the-L parameter to specify only a few rows of input at a time, for example,-L 1 transfers a line from the input every time to the command to be executed as a parameter. Of course, you can change 1 to any line, but 1 is the most commonly used, the following command demonstrates how to get code changes for each git commit: git log -- format = "% H % P" | xargs-L 1 git diff run commands in parallel. You may use xargs to call some commands that require intensive computing, how nice is it if xargs can use the multi-core computing power on your computer? Yes, the-P parameter does this. xargs can call multiple commands in parallel at a time. For example, you can run multiple ffmpeg encoders in parallel, however, let's take a simpler example: Parallel sleep $ time echo {1 .. 5} | xargs-n 1-P 5 sleepreal 0m5. 013 suser 0m0. 003 ssys 0m0. 014s linear sleep $ time echo {1 .. 5} | xargs-n 1 sleepreal 0m15. 022 suser 0m0. 004 ssys 0m0. 015s if you are interested in using xargs for parallel computing, you may want to look at GNU parallel. In comparison, the advantage of xargs is that most systems support it by default, it can also be easily installed on BSD and OS X, but parallel has many outstanding features.