Use of Linux xargs Command

  • 2021-08-21 22:07:58
  • OfStack

1. Functions:

xargs can pass space-delimited parameters (arguments) from data separated by spaces or line breaks in stdin to other commands. Because spaces are used as separators, xargs may misjudge when there are 1 file name or nouns with spaces in other meanings. Simply put, xargs is a filter for passing parameters to other commands, and it is one of the important components for building single-line commands.

xargs is used because many commands do not support the use of pipes to pass parameters, such as:


find /sbin -perm +700 |ls -l     // This command is an error , Because standard input cannot be used as a ls Parameters of 
find /sbin -perm +700 |xargs ls -l  // This is the right way 

2. Command format


xargs [ Options ] [command]

3. Description of options:


-0:如果输入的stdin含有特殊字符,例如反引号`、反斜杠\、空格等字符时,xargs可以将它还原成1般字符。为xargs的默认选项。
-e <flag>,-E <flag>,--eof=<eof-str>:eof是end of file string的意思。flag可以是1个字符串或者是由空格分隔的多个字符串,当xargs分析到这个flag时,就会停止工作。见示例2。
-p:当每次执行1个argument的时候询问1次用户。
-n <num>:表示命令在执行的时候1次使用的argument的个数,由num指定,默认是用所有的参数。
-t:表示先打印命令,然后再执行。
-a <file>:从文件中读入作为sdtin。
-i[replace-str]:告诉xargs可以使用{}代替从标准输入读取的参数,可以指定替代字符串replace-str,如果没有指定,默认为{}。建议使用-I,其符合POSIX标准。
-I [replace-str]:将xargs的输出每1项参数,单独赋值给后面的命令,参数需要用指定的代替字符串replace-str代替,也就是说replace-str不可缺省,必须显示指明,可以使用{} $ @等符号,其主要作用是当xargs  command后有多个参数时,调整参数位置。例如:find . -name "*.txt"|xargs -I {} cp {} /tmp/{}.bak。
-r:或者--no-run-if-empty,当xargs的输入为空的时候则停止xargs,不用再去执行后面的命令了,-r是xargs的默认选项。
-s <num>:命令行的最大字符数,指的是xargs后面那个命令的最大命令行字符数,包括命令、空格和换行符。每个参数单独传入xargs后面的命令。见示例4。
-L <line_num>:设置标准输入中最大的行数作为命令每1次执行的参数。见示例5。
-d <delim>, --delimiter=<delim>: xargs处理标准输入默认是按换行符和空格作为分隔符,输出arguments的分隔符是空格,这里修改xargs处理标准输入时的分隔符。
-x:eXit的意思,主要是配合-s使用,当命令行字符数大于-s指定的数值时,退出xargs。
-P:修改最大的进程数,默认是1,为0时候为as many as it can。该选项比较少用,目前还不清楚该用法。

4. Examples of usage

(1) Restore the special characters of shell to 1-character.


[b3335@MIC ~]$ echo '`0123`4 56789'|xargs -t echo
echo `0123`4 56789 
`0123`4 56789

If you do the following directly, you will report that the command 01234 cannot be found, because the reverse quotation marks will execute 01234 as a command in shell, but 01234 is not a command. -t means printing the command before executing it.


[b3335@MIC ~]$ echo `01234` 56789
-bash: 01234: command not found
56789

(2) Set the end flag when xargs reads in parameters, ending with a comma. Note here that the closing flag must be a separate field, that is, a field separated by a space or line break.


[b3335@MIC ~]$ echo 01234 , 56789|xargs -E ","
01234

(3) When using rm, mv and other commands to operate multiple files at the same time, sometimes the error of "argument list too long" parameter list is too long can be reported. At this time, xargs can be used to solve it. xargs separates the standard input string and passes it as a parameter to the following commands. For example, add suffix names to all files in the current directory.


ls | xargs -t -i mv {} {}.bak

# Select a file that meets the criteria 
ls|grep -E "201701|201702|201703|201704|201705|201706|201707|201708|201709|201710" |xargs -i mv {} {}.bak

(4) Set the maximum number of characters on the command line. Parameters are executed in 1 command by default.


[b3335@MIC test]$ echo "01234 56789"|xargs -t -s 11
echo 01234 
01234
echo 56789 
56789

(5) Set how many lines each time in the standard input as the parameter of the command. By default, all lines in the standard input are merged into one line and passed to the command for execution.


[b3335@MIC test]$ echo -e "01234\n56789\n01234" | xargs -t -L 2 echo 
echo 01234 56789 
01234 56789
echo 01234 
01234

(6) Separate the file contents with spaces for peer output.


// List file contents 
cat test.txt
a b c d e
f g h i j 
k l m n o

// Multi-line input and single-line 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 forcibly terminate the specified process


xargs [ Options ] [command]
0

Command interpretation:
ps -ef|grep spp Used to find processes that contain spp, awk '{printf "%s ",$2,FNR} Print out the target process ID, xargs kill -9 The target process ID is passed as a parameter to kill-9 to kill the process.

The above is the use of Linux xargs command details, more information about Linux xargs command please pay attention to other related articles on this site!


Related articles: