{"id":85,"date":"2013-05-03T19:32:33","date_gmt":"2013-05-03T11:32:33","guid":{"rendered":"http:\/\/coderbee.net\/?p=85"},"modified":"2013-05-03T20:40:46","modified_gmt":"2013-05-03T12:40:46","slug":"xargs-%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/linux\/20130503\/85","title":{"rendered":"xargs \u547d\u4ee4"},"content":{"rendered":"<p>xargs\u7684\u4f5c\u7528\u662f\u5c06\u53c2\u6570\u5217\u8868\u8f6c\u6362\u6210\u5c0f\u5757\u5206\u6bb5\u4f20\u9012\u7ed9\u5176\u4ed6\u547d\u4ee4\uff0c\u4ee5\u907f\u514d\u53c2\u6570\u5217\u8868\u8fc7\u957f\u7684\u95ee\u9898\uff0c\u5176\u4f5c\u7528\u4e00\u822c\u7b49\u540c\u4e8eshell\u4e2d\u53cd\u5f15\u53f7\uff0c\u4f46\u66f4\u52a0\u7075\u6d3b\uff0c\u5e76\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u8f93\u5165\u4e2d\u7684\u7a7a\u683c\u7b49\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u547d\u4ee4\u7684\u4e00\u822c\u5f62\u5f0f\uff1a<code>xargs  [option...]  [command  [initial-arguments]]<\/code><\/p>\n<h2>\u9009\u9879<\/h2>\n<ul>\n<li><code>-a inputfile<\/code> \u6216 <code>--arg-file=inputfile<\/code>\uff0c\u4eceinputfile\u8bfb\u53d6\u540d\u5b57\u800c\u4e0d\u662f\u6807\u51c6\u8f93\u5165\u3002\u5982\u679c\u4f7f\u7528\u4e86\u8fd9\u4e2a\u9009\u9879\uff0c\u6807\u51c6\u8f93\u5165\u6d41\u5728\u547d\u4ee4\u6267\u884c\u65f6\u5c06\u4fdd\u6301\u4e0d\u53d8\uff0c\u5426\u5219stdin\u4ece\u91cd\u5b9a\u5411\u81ea<code>\/dev\/null<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><code>-0<\/code> \u6216 <code>--null<\/code>\uff0c\uff08<code>-0<\/code>\u662f\u6570\u5b570\uff09\u8f93\u5165\u6587\u4ef6\u540d\u5c06\u4ee5null\u5b57\u7b26\u800c\u4e0d\u662f\u7a7a\u767d\u7b26\u7ed3\u675f\uff0c\u4efb\u4f55\u5f15\u53f7\u548c\u53cd\u659c\u6760\u5c06\u4e0d\u88ab\u7279\u6b8a\u5904\u7406\uff08\u6bcf\u4e2a\u5b57\u7b26\u53d6\u5b57\u9762\u4e49\uff09\u3002Disables the end of file string, which is treated like any other argument.<\/p>\n<\/li>\n<li>\n<p><code>-d delim<\/code> \u6216 <code>--delimiter delim<\/code>\uff0c\u8f93\u5165\u6587\u4ef6\u540d\u4ee5\u6307\u5b9a\u7684\u5b57\u7b26\u800c\u4e0d\u662f\u7a7a\u767d\u7b26\u7ed3\u675f\uff0c\u4efb\u4f55\u5f15\u53f7\u548c\u53cd\u659c\u6760\u5c06\u4e0d\u88ab\u7279\u6b8a\u5904\u7406\uff08\u6bcf\u4e2a\u5b57\u7b26\u53d6\u5b57\u9762\u4e49\uff09\u3002Disables the end of file string, which is treated like any other argument\u3002\u4e00\u822c\u662f\u5355\u4e2a\u5b57\u7b26\uff0c\u4e0d\u652f\u6301\u591a\u5b57\u8282\u5b57\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p><code>-I replace-str<\/code> \u6216 <code>-i[replace-str]<\/code> \u6216 <code>--replace[=replace-str]<\/code>\uff0c\u7528stdin\u8bfb\u53d6\u7684\u540d\u5b57\u66ff\u6362\u521d\u59cb\u53c2\u6570\u4e2d\u51fa\u73b0\u7684<code>replace-str<\/code>\uff0c<code>replace-str<\/code>\u9ed8\u8ba4\u662f<code>{}<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><code>-r<\/code> \u6216 <code>--no-run-if-empty<\/code>\uff0c\u5982\u679c\u6807\u51c6\u8f93\u5165\u662f\u7a7a\u7684\uff0c\u4e0d\u6267\u884c\u547d\u4ee4\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u547d\u4ee4\u81f3\u5c11\u6267\u884c\u4e00\u6b21\uff0c\u5373\u4f7f\u6ca1\u6709\u8f93\u5165\u3002<\/p>\n<\/li>\n<li>\n<p><code>-t<\/code> \u6216 <code>--verbose<\/code>\uff0c\u5728stderr\u4e0a\u8f93\u51fa\u547d\u4ee4\uff0c\u5728\u547d\u4ee4\u6267\u884c\u524d\u3002<\/p>\n<\/li>\n<li>\n<p><code>-p<\/code>\uff0c\u5728\u547d\u4ee4\u6267\u884c\u524d\u8ba9\u7528\u6237\u786e\u8ba4\u662f\u5426\u6267\u884c\u3002<\/p>\n<\/li>\n<li>\n<p><code>-P max-procs<\/code> \u6216 <code>--max-procs=max-procs<\/code>\uff0c\u4efb\u610f\u65f6\u523b\uff0c\u540c\u65f6\u6267\u884c\u547d\u4ee4\u7684\u6700\u5927\u8fdb\u7a0b\u6570\u3002\u9ed8\u8ba4\u662f1\u3002\u5982\u679c\u662f0\uff0cxargs\u5c06\u540c\u65f6\u8fd0\u884c\u5c3d\u53ef\u80fd\u591a\u7684\u8fdb\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p><code>L max-lines<\/code> \u6216 <code>-l[max-lines]<\/code> \u6216 <code>--max-lines[=max-lines]<\/code>\uff0c\u6bcf\u4e2a\u547d\u4ee4\u884c\u6700\u591a\u5904\u7406<code>max-lines<\/code>\u4e2a\u975e\u7a7a\u767d\u8f93\u5165\u884c\u3002<\/p>\n<\/li>\n<li>\n<p><code>-n max-args<\/code> \u6216 <code>--max-args=max-args<\/code>\uff0c\u6bcf\u4e2a\u547d\u4ee4\u884c\u6700\u591a\u4f7f\u7528<code>max-args<\/code>\u4e2a\u53c2\u6570\u3002<\/p>\n<\/li>\n<li>\n<p><code>-s max-chars<\/code> \u6216 <code>--max-chars=max-chars<\/code>\uff0c\u6bcf\u4e2a\u547d\u4ee4\u884c\u6700\u591a\u4f7f\u7528max-chars\u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u547d\u4ee4\u3001\u521d\u59cb\u5316\u53c2\u6570\u548c\u53c2\u6570\u5b57\u7b26\u4e32\u7684\u7ed3\u675f\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p><code>-x<\/code> \u6216 <code>--exit<\/code>\uff0c\u5982\u679c\u8d85\u51fa\u4e86<code>-s<\/code>\u9009\u9879\u6307\u5b9a\u7684\u5927\u5c0f\u5c06\u9000\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p><code>--process-slot-var=environment-variable-name<\/code>\uff0c<br \/>\n<!--more--><\/p>\n<\/li>\n<\/ul>\n<h2>\u4e3e\u4f8b<\/h2>\n<p><code>find . -name \"struts*.xml\" | xargs -t grep \"struts-2.0.dtd\"<\/code>\uff0c\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u67e5\u627e\u6587\u4ef6\u540d\u5f00\u5934\u662fstruts\u7684xml\u6587\u4ef6\uff0c\u7136\u540e\u518d\u627e\u5230\u7684xml\u6587\u4ef6\u91cc\u641c\u7d22<code>struts-2.0.dtd<\/code>\u5b57\u7b26\u4e32\uff0c\u5728\u641c\u7d22\u4e4b\u524d\u5148\u5728stderr\u4e0a\u8f93\u51fa\u6bcf\u6b21\u6267\u884c\u7684grep\u5b8c\u6574\u547d\u4ee4\u3002<\/p>\n<p><code>find faultReport -name \"strut*.xml\" | xargs -t -i grep \"struts-2.3.dtd\" {}<\/code>\uff0c\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u67e5\u627e\u6587\u4ef6\u540d\u5f00\u5934\u662fstruts\u7684xml\u6587\u4ef6\uff0c\u7136\u540e\u518d\u627e\u5230\u7684xml\u6587\u4ef6\u91cc\u641c\u7d22<code>struts-2.0.dtd<\/code>\u5b57\u7b26\u4e32\uff0c\u6bcf\u6b21\u5728\u641c\u7d22\u4e4b\u524d\u7528<code>find<\/code>\u547d\u4ee4\u627e\u5230\u7684\u6587\u4ef6\u540d\u66ff\u6362<code>grep<\/code>\u547d\u4ee4\u4e2d\u7684<code>{}<\/code>\uff0c\u7136\u540e\u5728stderr\u4e0a\u8f93\u51fa\u6267\u884c\u7684grep\u5b8c\u6574\u547d\u4ee4\u3002<\/p>\n<p><code>find .  -iname \"*.log\" | xargs rm -f<\/code>\uff0c\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u67e5\u627e\u540e\u7f00\u662flog\u7684\u6587\u4ef6\uff0c\u7136\u540e\u5220\u9664\u3002<\/p>\n<p><code>find . -iname \"*.jpg\" -type f -print | xargs tar -czf images.tar.gz<\/code>\uff0c\u67e5\u627e\u6240\u6709jpg\u6587\u4ef6\u5e76\u538b\u7f29\u3002<\/p>\n<p><code>find . -iname \"*.jpg\" | xargs -n1 -i cp {} .\/backup\/<\/code>\uff0c\u67e5\u627e\u6240\u6709jpg\u6587\u4ef6\u5e76\u62f7\u8d1d\u5230\u5f53\u524d\u76ee\u5f55\u7684backup\u76ee\u5f55\u4e0b\u3002<code>-n1<\/code>\u9009\u9879\u7684\u4f5c\u7528\u662f\u6bcf\u6b21\u53ea\u62f7\u8d1d\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<p><code>ls | xargs -r -i cp {}  {}.bak<\/code>\uff0c\u53ef\u4ee5\u66ff\u6362\u591a\u6b21\u3002<\/p>\n<p><code>file * | grep ASCII | cut -d: -f1 | xargs -p vi<\/code>\uff0c\u628a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6587\u672c\u6587\u4ef6\u540d\u4f20\u9012\u7ed9xargs\uff0cxargs\u628a\u8fd9\u4e9b\u6587\u4ef6\u540d\u4f5c\u4e3avi\u7684\u53c2\u6570\uff0c\u5728\u6267\u884cvi\u547d\u4ee4\u524d\u8ba9\u7528\u6237\u786e\u8ba4\u662f\u5426\u6267\u884c\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"http:\/\/www.gnu.org\/software\/findutils\/manual\/html_node\/find_html\/Invoking-xargs.html\">http:\/\/www.gnu.org\/software\/findutils\/manual\/html_node\/find_html\/Invoking-xargs.html<\/a><\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>xargs\u7684\u4f5c\u7528\u662f\u5c06\u53c2\u6570\u5217\u8868\u8f6c\u6362\u6210\u5c0f\u5757\u5206\u6bb5\u4f20\u9012\u7ed9\u5176\u4ed6\u547d\u4ee4\uff0c\u4ee5\u907f\u514d\u53c2\u6570\u5217\u8868\u8fc7\u957f\u7684\u95ee &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/linux\/20130503\/85\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[29,28],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/85"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}