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' | cat
xargs_data.txt
# 此時,xargs命令為cat命令構建了标準輸入,把字符串'xargs_data.txt'轉換為文件xargs_data.txt
# 所以,這裡cat命令的标準輸入是xargs_data.txt這個文件
root@hgdm:~/examples# echo 'xargs_data.txt' | xargs cat
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)
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'這個字符串,隻有一行數據,因此輸出1
root@hgdm:~/examples# echo 'xargs_data.txt' | wc -l
1
# xargs命令為wc命令構建了标準輸入,把字符串'xargs_data.txt'轉換為文件xargs_data.txt
# 所以,這裡wc命令的标準輸入是xargs_data.txt這個文件,而xargs_data.txt裡有4行數據,因此輸出4
root@hgdm:~/examples# echo 'xargs_data.txt' | xargs wc -l
4 xargs_data.txt
root@hgdm:~/examples#
xargs為wc構建标準輸入
例子5:xargs -d -n
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
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!