One Linux command a day (1): xargs


One Linux command a day (1): xargs

Command introduction

Xargs can separate the data in stdin with spaces or newline characters to form arguments separated by spaces and pass them to other userscommand. Because spaces are used as separators, xargs may misjudge when there are spaces in some file names or other meaningful strings. In short, xargs is used to pass parameters to other commands, which is one of the important components of building single line commands.

Xargs is used because many commands do not support using pipe | to pass parameters. For example:

find  / sbin  - perm  + seven hundred  |  ls  - l          #  This command is an error because standard input cannot be used as an argument to LS
find  / sbin  - perm  + seven hundred  |  xargs   ls  - l    #  That's right

Command format


Option description

Note that mandatory parameters for long options are also mandatory for short options.

-0, --null
  #If the stdin you enter contains special characters, such as back quotes  `、 Backslash  、 Xargs will restore it to normal characters when the characters such as space are used. Is the default option
-a, --arg-file=FILE
  #Reads input from the specified file file instead of standard input
-d, --delimiter=DEL
  #Specifies the separator when xargs processes input. Xargs processes the input content by using the space and newline character as the separator by default, and separates the output arguments by the space
  #EOF_ STR is end   of   file   String, indicating the end of the input
-e, --eof[=EOF_STR]
  #The effect is equivalent to  - E option, and  - When e option is different, the option does not conform to POSIX standard and EOF_ STR is not mandatory. Without EOF_ STR indicates that the input has no terminator
  #Each parameter of xargs output is assigned to the following command separately, and the parameter needs to use the specified replacement string replace_ STR instead. REPLACE_ STR can use {}  $ @  Its main function is to adjust the parameter position when there are multiple parameters after xargs command.
  For example, backup the file with the suffix of TXT: find. - name "*. TXT"  |  xargs  - I  {}   cp  {} / tmp/{}.bak
-i, --replace[=REPLACE_STR]
  #The effect is the same  - I option, parameter replace_ STR is optional and defaults to  {}。 Recommended use  - I option because it is POSIX compliant
  #Limit the maximum number of input lines. Implied  - X option
-l, --max-lines[=MAX_LINES]
  #The effect is the same  - L option, parameter Max_ Lines is optional and defaults to 1. Recommended use  - L option, because it is POSIX compliant
-n, --max-args=MAX_ARGS
  #Indicates the maximum number of parameters used at a time when a command is executed
-o, --open-tty
  #Before executing the command, reopen stdin in the child process as / dev / TTY. This is very useful if you want xargs to run interactive applications
-P, --max-procs=MAX_PROCS
  #Maximum process per run; The default value is 1. If Max_ Procs is   0, xargs will run as many processes as possible at a time. Generally used with - N or - L options
-p, --interactive
  #Ask the user once each time an argument is executed
  #Sets the specified environment variable to a unique value in each running child process. Once the child exits, the value is reused. For example, this can be used for the initial load distribution scheme
-r, --no-run-if-empty
  #When the input of xargs is empty, stop xargs and do not need to execute the following commands. Is the default option
-s, --max-chars=MAX_CHARS
  #The maximum number of command line characters refers to the maximum number of command line characters of the command after xargs, including command, space and newline character. Each parameter is passed into the command after xargs
  #Displays the operating system's restrictions on the length of the command line
-t, --verbose
  #Print the command to standard error output first, and then execute it
-x, --exit
  #Cooperation  - s   Use when the number of command line characters is greater than  - s   Exit when the specified value is set   xargs
  #Show help and exit
  #Display version information and exit

Common examples

1. WillShellThe back quotation marks of special characters are restored to general characters.

echo '`0123`4 56789' | xargs -t echo
echo `0123`4 56789 
`0123`4 56789

If you directly perform the following operation, you will report that you cannot find the command 01234, because the counter quotation marks will execute 01234 as a command in the shell, but 01234 is not a command- T means to print the command first and then execute it.Worth collecting! Linux system common command quick reference manual

echo `01234` 56789
-bash: 01234: command not found

2. Set the end identifier when xargs reads in parameters, ending with a comma. Note that the end flag must be a separate field, that is, a field separated by a space or a newline character.

echo 01234 , 56789 | xargs -E ","

3. Use RM, MV, etccommandWhen multiple files are operated at the same time, the error of “argument list too long” parameter list is sometimes reported. At this time, xargs can be used to solve the problem. Xargs separates the standard input string and passes it as a parameter to the followingcommand. For example, add a suffix to all files in the current directory.

ls | xargs -t -i mv {} {}.bak
#   Select eligible files
ls | grep -E "201701|201702|201703" | xargs -I {} mv {} {}.bak

4. Sets the maximum number of characters on the command line. By default, the parameters are passed into the command for execution one by one.

echo "01234 56789" | xargs -t -s 11
echo 01234 
echo 56789 

5. Set the number of lines in the standard input as the parameter of the command. The default is to merge all lines in the standard input into one line and transfer them to the command for execution at one time.

echo -e "01234n56789n01234" | xargs -t -L 2 echo
echo 01234 56789 
01234 56789
echo 01234 

6. Merge the contents of the file into one line separated by spaces.

#   List file contents
cat test.txt
a b c d e
f g h i j 
k l m n o
#   Merging multiple lines of input into one line of output
cat test.txt | xargs
a b c d e f g h i j k l m n o

7. Combine with PS, grep, awk and kill to terminate the specified process.

ps -ef | grep spp | awk '{printf "%s ",$2}' | xargs kill -9

PS – EF | grep spp is used to find the process containing spp, awk ‘{printf% s’, $2, FNR} prints out the target process ID, and xargs kill – 9 passes the target process ID as a parameter to kill – 9 to kill the process.Please collect! Summary of 40 necessary commands for Linux operation and maintenance


One Linux command a day (1): xargs