man xargs是这样描述xargs的用途:
xargs is used to build and execute command lines from standard input.
中文意思大致是,xargs是基于标准输入构建和执行命令行。
Linux系统中一个命令的语法是: 命令名称 [命令选项] [命令参数]。其中,命令选项是以-或--开头的,命令选项的数量有时不止一个,可以有多个;命令参数是命令的操作对象,通常为文件名或目录名等,命令参数的数量有时不止一个,可以有多个。
xargs的语法是: xargs [xargs的命令选项] 其他命令的名称 [其他命令的命令选项] [其他命令的命令参数]。
xargs为它后面出现的其他命令构建一个或多个命令参数,或者说,xargs为它后面出现的其他命令构建标准输入(stdin)。
xargs的英文全称是executable arguments,指可执行的命令参数。或许这样理解更好一些,xargs is used to build executable arguments for a command from standard input。
例子1: xargs的标准输出是什么?xargs的标准输出默认是一行命令参数xargs命令,是将标准输入拆分成多个命令参数。既然是拆分,就需要有分隔符去分隔标准输入,这个默认的分隔符就是空格或换行符。
默认时,xargs命令是将标准输入转换为一行命令参数(注意: 是一行而不是多行),这行命令参数是为其他命令构建好的一个或多个命令参数,每个命令参数之间以一个空格间隔开。
一行命令参数 = 命令参数1 命令参数2 命令参数3 .. 命令参数N。
root@hgdm:~/examples# find ~/examples/ -name '*data*'/root/examples/cut_data.txt/root/examples/data333.txt/root/examples/data555.txt/root/examples/xargs_data.txt/root/examples/data1.txt/root/examples/sed_data.txt/root/examples/data2.txt/root/examples/data22.txt/root/examples/user_data.txt/root/examples/data111.txt/root/examples/data55.txt/root/examples/data444.txt/root/examples/data3.txt/root/examples/data11.txt/root/examples/sed_data2.txt/root/examples/sed_data_new.txt/root/examples/data4.txt/root/examples/data5.txt/root/examples/data44.txt/root/examples/data33.txt/root/examples/data222.txt# xargs命令把标准输入的数据(find命令的输出数据)转换成了一行数据,这行数据每个命令参数之间以一个空格分隔开root@hgdm:~/examples# find ~/examples/ -name '*data*' | xargs/root/examples/cut_data.txt /root/examples/data333.txt /root/examples/data555.txt /root/examples/xargs_data.txt /root/examples/data1.txt /root/examples/sed_data.txt /root/examples/data2.txt /root/examples/data22.txt /root/examples/user_data.txt /root/examples/data111.txt /root/examples/data55.txt /root/examples/data444.txt /root/examples/data3.txt /root/examples/data11.txt /root/examples/sed_data2.txt /root/examples/sed_data_new.txt /root/examples/data4.txt /root/examples/data5.txt /root/examples/data44.txt /root/examples/data33.txt /root/examples/data222.txt
root@hgdm:~/examples# cat xargs_data.txt If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)# 此时,grep命令的标准输入是'xargs_data.txt'这个字符串root@hgdm:~/examples# echo 'xargs_data.txt' | grep 'data'xargs_data.txt# 此时,grep命令的标准输入是'xargs_data.txt'这个字符串,而first things first不在这个字符串里,所以找不到内容root@hgdm:~/examples# echo 'xargs_data.txt' | grep 'first things first'root@hgdm:~/examples# # 此时,grep命令为grep命令构建了标准输入,把字符串'xargs_data.txt'转换为文件xargs_data.txt# 所以,这里grep命令的标准输入是xargs_data.txt这个文件,而不是'xargs_data.txt'这个字符串root@hgdm:~/examples# echo 'xargs_data.txt' | xargs grep 'first things first'If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)
xargs为grep构建标准输入
例子3: xargs命令为cat命令构建标准输入(命令参数)root@hgdm:~/examples# cat xargs_data.txt If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)# 此时,cat命令的标准输入是'xargs_data.txt'这个字符串root@hgdm:~/examples# echo 'xargs_data.txt' | catxargs_data.txt# 此时,xargs命令为cat命令构建了标准输入,把字符串'xargs_data.txt'转换为文件xargs_data.txt# 所以,这里cat命令的标准输入是xargs_data.txt这个文件root@hgdm:~/examples# echo 'xargs_data.txt' | xargs catIf there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)
xargs为cat构建标准输入
例子4: xargs命令为wc命令构建标准输入(命令参数)wc -l命令是用于统计一个标准输入里有几行数据。
root@hgdm:~/examples# cat xargs_data.txt If there is any one secret of effectiveness, it is concentration. Effective executives do first things first and they do one thing at a time. (Peter Drucker)Do first things first, and second things not at all. (Peter Drucker)Intelligence is not the ability to store information, but to know where to find it. (Albert Einstein)When you make a mistake, there are only three things you should ever do about it: admit it, learn from it, and don't repeat it. (Bear Bryant)# wc的标准输入是'xargs_data.txt'这个字符串,只有一行数据,因此输出1root@hgdm:~/examples# echo 'xargs_data.txt' | wc -l1# xargs命令为wc命令构建了标准输入,把字符串'xargs_data.txt'转换为文件xargs_data.txt# 所以,这里wc命令的标准输入是xargs_data.txt这个文件,而xargs_data.txt里有4行数据,因此输出4root@hgdm:~/examples# echo 'xargs_data.txt' | xargs wc -l 4 xargs_data.txtroot@hgdm:~/examples#
xargs为wc构建标准输入
例子5:xargs -d -n-d命令选项,用于指定xargs标准输入(stdin)的每个参数之间的分隔符,默认的分隔符为空格或换行符(\n)。-n命令选项,用于指定xargs标准输出(stdout)的每行最多含几个参数。不使用-n,默认情况下,xargs的标准输出是一行命令参数,这行命令参数里每个参数之间以一个空格分隔开。root@hgdm:~/examples# echo '孤:舟:蓑:笠:翁:独:钓:寒:江:雪' | xargs -d :孤 舟 蓑 笠 翁 独 钓 寒 江 雪root@hgdm:~/examples# echo '孤:舟:蓑:笠:翁:独:钓:寒:江:雪' | xargs -d : -n 2孤 舟蓑 笠翁 独钓 寒江 雪root@hgdm:~/examples#
xargs -d -n
例子6:xargs -0-0这个命令选项用于指定标准输入(stdin)的参数分隔符为null字符,而不是使用默认空格或换行符作为参数分隔符。当标准输入的参数里包含空格、换行符、引号或反斜杠时,-0是非常有用的。
比较以下两个命令的区别
# 这个find命令输出的每个目录或文件是以null字符作为分隔符的,# 假若find输出的目录名或文件名含有空格或换行符,# 那么,xargs -0为rm构造标准输入时也能准确区分这些含空格或换行符的目录或文件,# 执行rm时不会出现删错的情况。find ~/examples/ -name '*data*' -print0 | xargs -0 rm -rf
# 这个find命令输出的每个目录或文件是以换行符作为分隔符,# 假若find输出的目录名或文件名含有空格或换行符,# 那么xargs为rm构造标准输入时,就不能准确区分这些含空格或换行符的目录或文件,把含空格或换行符的目录名或文件名也拆分成命令参数,# 执行rm时可能会出现删错的情况。find ~/examples/ -name '*data*' -print | xargs rm -rf