<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[Xargs命令的含义及使用]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 17 Dec 2009 09:33:39 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	1、在“linux下批量修改文件名”中使用到了此命令，将源文件批量mv的时间,将上一个命令的输出作为参数传入下一命令，比你个执行下一命令。<br/>find -name ‘*.jpg’ -printf %f &#92;&#92;n&#124;grep -v ^[0-9] &#124;grep -v logo.jpg&#124;awk -F ‘.’ ‘&#123;print $1&#125;’&#124;<br/>xargs -i&#123;&#125; mv &#123;&#125;.jpg `date +%Y%m%d`&#123;&#125;.jpg<br/><br/>find ./ -name tmp -type f -print &#124; xargs /bin/rm -f<br/>找到tmp文件，删除。<br/><br/>find ./ -name tmp -type f -print &#124; xargs -0 /bin/rm -f<br/>找到tmp文件，删除。当文件名字包含新行或空格时使用。<br/><br/>cut -d: -f1 < /etc/passwd &#124; sort &#124; xargs echo<br/>列举用户。<br/><br/>find ./ -type f -print &#124; xargs -i mv -f &#123;&#125;&nbsp;&nbsp;`date +%Y%m%d`&#123;&#125;<br/>文件重命名的批量方法，加上时间戳。<br/><br/>2、首先进行的一个测试<br/>–列出所有用户<br/>root@test-177-ce0 # cut -d : -f 1 </etc/passwd<br/>root<br/>daemon<br/>bin<br/>sys<br/>adm<br/>lp<br/>uucp<br/>nuucp<br/>smmsp<br/>listen<br/>gdm<br/>webservd<br/>postgres<br/>svctag<br/>nobody<br/>noaccess<br/>nobody4<br/>lyj<br/>oracle<br/>syslog<br/><br/>–排序<br/>root@test-177-ce0 # cut -d : -f 1 </etc/passwd &#124;sort<br/>adm<br/>bin<br/>daemon<br/>gdm<br/>listen<br/>lp<br/>lyj<br/>noaccess<br/>nobody<br/>nobody4<br/>nuucp<br/>oracle<br/>postgres<br/>root<br/>smmsp<br/>svctag<br/>sys<br/>syslog<br/>uucp<br/>webservd<br/><br/>–打印输出<br/>root@test-177-ce0 # cut -d : -f 1 </etc/passwd &#124;sort&#124;xargs echo<br/>adm bin daemon gdm listen lp lyj noaccess nobody nobody4 nuucp oracle postgres root smmsp svctag sys syslog uucp webservd<br/><br/>相当于如下命令，定义参数并且使用。<br/>root@test-177-ce0 # x=`cut -d : -f 1 </etc/passwd &#124;sort`<br/>root@test-177-ce0 # echo $x<br/>adm bin daemon gdm listen lp lyj noaccess nobody nobody4 nuucp oracle postgres root smmsp svctag sys syslog uucp webservd<br/><br/>3、使用介绍<br/>root@test-177-ce0 # xargs –help<br/>xargs:非法选项 — help<br/>xargs:用法 xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] [-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] [cmd [args ...]]<br/><br/>把一条命令在标准输出上的输出结果，转换为另一个程序或或者另一个命令的参数。<br/><br/>构造参数列表并进行调用工具。<br/>NAME<br/>xargs – construct argument lists and invoke utility<br/><br/>SYNOPSIS<br/>/usr/bin/xargs [-t]&nbsp;&nbsp;[-p]&nbsp;&nbsp;[&nbsp;&nbsp;-e&nbsp;&nbsp;[eofstr]]&nbsp;&nbsp;[-E eofstr]&nbsp;&nbsp;[-<br/>I replstr]&nbsp;&nbsp;[&nbsp;&nbsp;-i&nbsp;&nbsp;[replstr]]&nbsp;&nbsp;[-L number]&nbsp;&nbsp;[ -l [number]] [<br/>-n number [-x]] [-s size] [ utility [ argument...]]<br/><br/>/usr/xpg6/bin/xargs [-t] [-p] [ -e [eofstr]] [-E eofstr]&nbsp;&nbsp;[-<br/>I replstr]&nbsp;&nbsp;[&nbsp;&nbsp;-i&nbsp;&nbsp;[replstr]]&nbsp;&nbsp;[-L number]&nbsp;&nbsp;[ -l [number]] [<br/>-n number [-x]] [-s size] [ utility [ argument...]]<br/><br/>DESCRIPTION<br/>The xargs utility constructs a command&nbsp;&nbsp;line&nbsp;&nbsp;consisting&nbsp;&nbsp;of<br/>the&nbsp;&nbsp;utility&nbsp;&nbsp;and argument operands specified followed by as<br/>many arguments read in sequence from standard input as&nbsp;&nbsp;will<br/>fit&nbsp;&nbsp;in&nbsp;&nbsp;length&nbsp;&nbsp;and&nbsp;&nbsp;number&nbsp;&nbsp;constraints&nbsp;&nbsp;specified&nbsp;&nbsp;by the<br/>options. The xargs utility then invokes the constructed com-<br/>mand&nbsp;&nbsp;line&nbsp;&nbsp;and&nbsp;&nbsp;waits&nbsp;&nbsp;for its completion. This sequence is<br/>repeated until an end-of-file condition is detected on stan-<br/>dard&nbsp;&nbsp;input&nbsp;&nbsp;or&nbsp;&nbsp;an invocation of a constructed command line<br/>returns an exit status of 255.<br/><br/>Arguments&nbsp;&nbsp;in&nbsp;&nbsp;the&nbsp;&nbsp;standard&nbsp;&nbsp;input&nbsp;&nbsp;must&nbsp;&nbsp;be&nbsp;&nbsp;separated&nbsp;&nbsp;by<br/>unquoted&nbsp;&nbsp;blank characters, or unescaped blank characters or<br/>newline characters. A string of&nbsp;&nbsp;zero&nbsp;&nbsp;or&nbsp;&nbsp;more&nbsp;&nbsp;non-double-<br/>quote&nbsp;&nbsp;(”)&nbsp;&nbsp;and&nbsp;&nbsp;non-newline&nbsp;&nbsp;characters&nbsp;&nbsp;can&nbsp;&nbsp;be&nbsp;&nbsp;quoted by<br/>enclosing them in double-quotes. A string of&nbsp;&nbsp;zero&nbsp;&nbsp;or&nbsp;&nbsp;more<br/>non-apostrophe&nbsp;&nbsp;(’) and non-newline characters can be quoted<br/>by enclosing them in apostrophes. Any unquoted character can<br/>be escaped by preceding it with a backslash (&#92;). The utility<br/>will be executed one or more times until the end-of-file&nbsp;&nbsp;is<br/>reached. The results are unspecified if the utility named by<br/>utility attempts to read from its standard input.<br/><br/>参数含义：<br/><br/>-i 选项：告诉 xargs 用每项的名称替换 &#123;&#125;。<br/>find -name ‘*.jpg’ -printf %f &#92;&#92;n&#124;grep -v ^[0-9] &#124;grep -v logo.jpg&#124;awk -F ‘.’ ‘&#123;print $1&#125;’&#124;<br/>xargs -i&#123;&#125; mv &#123;&#125;.jpg `date +%Y%m%d`&#123;&#125;.jpg<br/><br/>-t 选项指示 xargs 先打印命令，然后再执行。<br/><br/>-p 选项：它使操作具有可交互性：<br/>要求您在运行每个命令之前进行确认。如果您按下 “y”，则执行命令，删除文件时的确认。<br/><br/>-t 选项：使用一个详细模式,显示要运行的命令。<br/><br/>-r 选项：没有要处理的内容，停止xargs；<br/>$ file * &#124; grep No &#124; cut -d”:” -f1 &#124; xargs -t -r wc -l $<br/>如果没有要运行的内容，该命令退出。<br/><br/>-n 选项：限制单个命令行的参数个数。<br/>如rm只能接受有限数量的参数。-n 选项限制单个限制单个命令行的参数个数。<br/>向 xargs ls -ltr 传递五个文件，但每次向 ls -ltr 仅传递两个文件。<br/>root@test-177-ce0 # file * &#124; grep zip<br/>js.zip:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ZIP<br/>s2new.zip:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZIP<br/>seas.zip:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ZIP<br/>root@test-177-ce0 # file * &#124; grep zip &#124; cut -d”:” -f1 &#124; xargs -t -n2 ls -ltr<br/>ls -ltr js.zip s2new.zip<br/>-rw-r–r–&nbsp;&nbsp; 1 root&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;134756 1 17:32 js.zip<br/>-rw-r–r–&nbsp;&nbsp; 1 root&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp; 214847212&nbsp;&nbsp;14:32 s2new.zip<br/>ls -ltr seas.zip<br/>-rw-r–r–&nbsp;&nbsp; 1 root&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp; 243173439&nbsp;&nbsp;15:15 seas.zip<br/>root@test-177-ce0 #<br/><br/>4、Xargs的优势<br/>1)统计文件中的行数<br/>cut -d : -f 1显示第一个字段<br/>file -Lz *，用于查找是符号链接或者经过压缩的文件<br/>ls -ltr 命令，如下所示：<br/><br/>xargs 本身虽然没有多大用处，但在与其他命令相结合时，它的功能非常强大。<br/>–统计文件的长度<br/>root@test-177-ce0 # wc -l `file zianed.log &#124;grep ascii&#124;cut -d : -f 1&#124;xargs cat`<br/>98 /etc/opt/SUNWexplo/default/explorer<br/>452 /opt/SUNWexplo/bin/explorer<br/>550 total<br/>–统计文本的长度<br/>root@test-177-ce0 # file zianed.log &#124;grep ascii&#124;cut -d : -f 1&#124;xargs cat&#124;wc -l<br/>2<br/><br/>2)快速批量重命名文件。<br/>$ ls &#124; xargs -t -i mv &#123;&#125; &#123;&#125;.bak<br/><br/>3)搜索文件，使用 vi 打开要编辑的文件时：<br/>$ file * &#124; grep ASCII &#124; cut -d”:” -f1 &#124; xargs vi<br/>使用 vi 逐个打开文件。希望搜索多个文件并打开它们进行编辑时，使用该命令非常方便。<br/><br/>4、与管道的区别<br/>管道是把一个命令的输出传递给另一个命令作为输入，比如：<br/>command1 &#124; command2<br/>但是command2仅仅把输出的内容作为输入参数。<br/><br/>例子：<br/>root@test-177-ce0 # file zianed.log &#124;grep ascii&#124;cut -d : -f 1&#124;cat<br/>zianed.log<br/>root@test-177-ce0 # file zianed.log &#124;grep ascii&#124;cut -d : -f 1&#124;xargs cat<br/>/etc/opt/SUNWexplo/default/explorer<br/>/opt/SUNWexplo/bin/explorer<br/><br/>xargs能把管道传来的字符串当作文件交给其后的命令执行。要对匹配文件操作时，使用find and xargs。<br/>xargs是shell命令的一个，可以把管道输入的内容转化为其参数要操作的文件。<br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] Xargs命令的含义及使用]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>