{"id":156,"date":"2014-06-26T03:22:37","date_gmt":"2014-06-26T03:22:37","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=156"},"modified":"2014-06-26T03:22:37","modified_gmt":"2014-06-26T03:22:37","slug":"linux%e7%b3%bb%e7%bb%9f%e4%b8%ad%e7%9a%84find%e5%91%bd%e4%bb%a4%e5%92%8cxargs%e5%91%bd%e4%bb%a4%e8%af%a6%e8%a7%a3%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%96%99","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/156.html","title":{"rendered":"Linux\u7cfb\u7edf\u4e2d\u7684find\u547d\u4ee4\u548cxargs\u547d\u4ee4\u8be6\u89e3[\u6280\u5de7\/\u8d44\u6599]"},"content":{"rendered":"<h6>\u5728Linux\u4e0b\u4f7f\u7528md5sum\u9012\u5f52\u751f\u6210\u6574\u4e2a\u76ee\u5f55\u7684MD5\u503c<\/h6>\n<div>\u4eca\u5929\u8981\u7528\u4f7f\u7528md5sum\u64cd\u4f5c\u76ee\u5f55\uff0c\u53d1\u73b0\u5b83\u8fd8\u4e0d\u652f\u6301\u9012\u5f52\u64cd\u4f5c\uff0c\u7f51\u4e0a\u6709\u4eba\u63d0\u51fa\u7528\u811a\u672c\uff0c\u5176\u5b9e\u4e0d\u7528\u90a3\u4e48\u9ebb\u70e6\u7684\u3002<\/div>\n<div>\u4e00\u884c\u547d\u4ee4\u5c31\u641e\u5b9a\u4e86\uff1a<br \/>\nfind .\/ -type f -print0 | xargs -0 md5sum &gt; .\/my.md5<\/div>\n<div>\u68c0\u67e5\u4e5f\u5f88\u7b80\u5355\uff1a<\/div>\n<div>md5sum -c my.md5<\/div>\n<h6>#\u627e\u5230\u6587\u4ef6\u5939\u4e2d\u6bd4all.log.3\u65b0\u7684\u6587\u4ef6\u7136\u540e\u5c06\u5176\u6253\u5305<\/h6>\n<div>$ find -depth -newer all.log.3 -type f -exec tar czf log.tgz {} ;#\u5c06\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u7684\u76ee\u5f55\u6253\u5305\u5e76\u538b\u7f29<\/p>\n<pre class=\"lang:default decode:true\">$ find -type d --maxdepth 1 -exec tar czf website_info.tgz {} ;\n$ find -type d --maxdepth 1 | xargs tar czf website_info.tgz<\/pre>\n<\/div>\n<hr \/>\n<div>\n<p><strong>\u67e5\u627e\u6700\u8fd1\u53d1\u751f\u53d8\u5316\u7684php\u6587\u4ef6<\/strong><\/p>\n<pre class=\"lang:default decode:true\">find . -type f -name '*.php' -mtime -7<\/pre>\n<p>-type f \u8868\u793a\u641c\u7d22\u6b63\u5e38\u7684\u4e00\u822c\u6587\u4ef6 \u00a0 -mtime -7 \u8868\u793a7*24\u5c0f\u65f6\u5185\u4fee\u6539\u7684\u6587\u4ef6<\/p>\n<\/div>\n<div>\n<p><strong>\u67e5\u627e\u6587\u4ef6\u4e2d\u662f\u5426\u5b58\u5728\u7591\u4f3c\u4ee3\u7801<\/strong><\/p>\n<pre class=\"lang:default decode:true\">find . -type f -name '*.php' | xargs grep -l \"eval *(\" --color<\/pre>\n<p>(*\u4ee3\u8868\u4efb\u610f\u4e2a\u7a7a\u683c)<\/p>\n<pre class=\"lang:default decode:true\">find . -type f -name '*.php' | xargs grep -l \"base64_decode *(\" --color\nfind . -type f -name '*.php' | xargs grep -l \"gzinflate *(\" --color\nfind . -type f -name '*.php' | xargs grep -l \"eval *(str_rot13 *(base64_decode *(\" --color<\/pre>\n<p>\u6ce8\u89e3\uff1a\u5f88\u591a\u547d\u4ee4\u4e0d\u652f\u6301\u7ba1\u9053\u4f20\u9012\u53c2\u6570\uff0c\u800c\u5b9e\u9645\u4e0a\u53c8\u9700\u8981\u8fd9\u6837\uff0c\u6240\u4ee5\u5c31\u7528\u4e86xargs\u547d\u4ee4\uff0c\u8fd9\u4e2a\u547d\u4ee4\u53ef\u4ee5\u7528\u6765\u7ba1\u9053\u4f20\u9012\u53c2\u6570\uff1bgrep -l\u8868\u793a\u53ea\u5305\u542b\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u6587\u4ef6\u540d\uff0c\u5982\u679c\u53bb\u6389-l\u5219\u4f1a\u663e\u793a\u5339\u914d\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u884c\u5185\u5bb9<\/p>\n<h5>\u6bd4\u8f83\u4ee3\u7801\u6587\u4ef6<\/h5>\n<hr \/>\n<p>\u8fd9\u79cd\u60c5\u51b5\u9700\u8981\u6709\u4e00\u4efd\u5e72\u51c0\u7684\u4ee3\u7801\uff0c\u8fd9\u4efd\u4ee3\u7801\u548c\u6b63\u5728\u4f7f\u7528\u7684\u4ee3\u7801\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"lang:default decode:true\">diff -r wordpress-clean\/ wordpress-compromised\/ -x wp-content<\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u6bd4\u8f83wordpress-clean\/ \u548cwordpress-comprised\/\u4e24\u4e2a\u76ee\u5f55\uff0c\u5e76\u4e14\u76ee\u5f55\u91cc\u9762\u7684wp-content\/\u5b50\u76ee\u5f55\u4e0d\u6bd4\u8f83\u3002<\/p>\n<hr \/>\n<\/div>\n<div>\n<pre class=\"lang:default decode:true\">find \/ ! -path \"\/usr\/share\/*\" ! -path \"\/usr\/lib\/*\" ! -path \"\/root\/.pyenv\/*\" -regex \".*.sh$|.*.pl$|.*.py$|.*.conf$|.*.cnf$|.*.ini$|.*\/..*history$|.*\/..*pass.*\" -print | zip pack.zip -@<\/pre>\n<\/div>\n<div>\n<hr \/>\n<\/div>\n<div>\n<ul class=\"list1\">\n<li class=\"text_import1\">find<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">[root@www ~]# find [PATH] [option] [action]\n\u9009\u9879\u4e0e\u53c2\u6570\uff1a\n1. \u4e0e\u65f6\u95f4\u6709\u5173\u7684\u9009\u9879\uff1a\u5171\u6709 -atime, -ctime \u4e0e -mtime \uff0c\u4ee5 -mtime \u8bf4\u660e\n   -mtime  n \uff1an \u4e3a\u6570\u5b57\uff0c\u610f\u4e49\u4e3a\u5728 n \u5929\u4e4b\u524d\u7684\u2018\u4e00\u5929\u4e4b\u5185\u2019\u88ab\u66f4\u52a8\u8fc7\u5185\u5bb9\u7684\u6863\u6848\uff1b\n   -mtime +n \uff1a\u5217\u51fa\u5728 n \u5929\u4e4b\u524d(\u4e0d\u542b n \u5929\u672c\u8eab)\u88ab\u66f4\u52a8\u8fc7\u5185\u5bb9\u7684\u6863\u6848\u6863\u540d\uff1b\n   -mtime -n \uff1a\u5217\u51fa\u5728 n \u5929\u4e4b\u5185(\u542b n \u5929\u672c\u8eab)\u88ab\u66f4\u52a8\u8fc7\u5185\u5bb9\u7684\u6863\u6848\u6863\u540d\u3002\n   -newer file \uff1afile \u4e3a\u4e00\u4e2a\u5b58\u5728\u7684\u6863\u6848\uff0c\u5217\u51fa\u6bd4 file \u8fd8\u8981\u65b0\u7684\u6863\u6848\u6863\u540d\n\n\u8303\u4f8b\u4e00\uff1a\u5c06\u8fc7\u53bb\u7cfb\u7edf\u4e0a\u9762 24 \u5c0f\u65f6\u5185\u6709\u66f4\u52a8\u8fc7\u5185\u5bb9 (mtime) \u7684\u6863\u6848\u5217\u51fa\n[root@www ~]# find \/ -mtime 0\n\n# \u90a3\u4e2a 0 \u662f\u91cd\u70b9\uff010 \u4ee3\u8868\u76ee\u524d\u7684\u65f6\u95f4\uff0c\u6240\u4ee5\uff0c\u4ece\u73b0\u5728\u5f00\u59cb\u5230 24 \u5c0f\u65f6\u524d\uff0c\n# \u6709\u53d8\u52a8\u8fc7\u5185\u5bb9\u7684\u6863\u6848\u90fd\u4f1a\u88ab\u5217\u51fa\u6765\uff01\u90a3\u5982\u679c\u662f\u4e09\u5929\u524d\u7684 24 \u5c0f\u65f6\u5185\uff1f\n# find \/ -mtime 3 \u6709\u53d8\u52a8\u8fc7\u7684\u6863\u6848\u90fd\u88ab\u5217\u51fa\u7684\u610f\u601d\uff01\n\n\u8303\u4f8b\u4e8c\uff1a\u5bfb\u627e \/etc \u5e95\u4e0b\u7684\u6863\u6848\uff0c\u5982\u679c\u6863\u6848\u65e5\u671f\u6bd4 \/etc\/passwd \u65b0\u5c31\u5217\u51fa\n\n[root@www ~]# find \/etc -newer \/etc\/passwd\n# -newer \u7528\u5728\u5206\u8fa8\u4e24\u4e2a\u6863\u6848\u4e4b\u95f4\u7684\u65b0\u65e7\u5173\u7cfb\u662f\u5f88\u6709\u7528\u7684\uff01<\/pre>\n<p>\u65f6\u95f4\u53c2\u6570\u771f\u662f\u633a\u6709\u610f\u601d\u7684\uff01\u6211\u4eec\u73b0\u5728\u77e5\u9053 atime, ctime \u4e0e mtime \u7684\u610f\u4e49\uff0c\u5982\u679c\u4f60\u60f3\u8981\u627e\u51fa\u4e00\u5929\u5185\u88ab\u66f4\u52a8\u8fc7\u7684\u6863\u6848\u540d\u79f0\uff0c \u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u8303\u4f8b\u4e00\u7684\u4f5c\u6cd5\u3002\u4f46\u5982\u679c\u6211\u60f3\u8981\u627e\u51fa\u2018<strong>4\u5929\u5185<\/strong>\u88ab\u66f4\u52a8\u8fc7\u7684\u6863\u6848\u6863\u540d\u2019\u5462\uff1f\u90a3\u53ef\u4ee5\u4f7f\u7528\u2018 <span class=\"text_import2\">find \/var -mtime -4<\/span>\u00a0\u2019\u3002\u90a3\u5982\u679c\u662f\u2018<strong>4\u5929\u524d\u7684\u90a3\u4e00\u5929<\/strong>\u2019\u5c31\u7528\u2018 <span class=\"text_import2\">find \/var -mtime 4<\/span>\u00a0\u2019\u3002<strong>\u6709\u6ca1\u6709\u52a0\u4e0a\u2018+, -\u2019\u5dee\u522b\u5f88\u5927\u5594\uff01<\/strong>\u6211\u4eec\u53ef\u4ee5\u7528\u7b80\u5355\u7684\u56fe\u793a\u6765\u8bf4\u660e\u4e00\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" id=\"mail_scale_image_4294971460_0\" class=\" mail_scale_image aligncenter\" title=\"find \u76f8\u5173\u7684\u65f6\u95f4\u53c2\u6570\u610f\u4e49\" src=\"http:\/\/www.ha97.com\/book\/vbird_linux\/linux_basic\/0220filemanager\/find_time.gif\" alt=\"\" border=\"1\" \/><\/p>\n<p><center>\u56fe5.2.1\u3001find \u76f8\u5173\u7684\u65f6\u95f4\u53c2\u6570\u610f\u4e49<\/center><center><\/center>\u56fe\u4e2d\u6700\u53f3\u8fb9\u4e3a\u76ee\u524d\u7684\u65f6\u95f4\uff0c\u8d8a\u5f80\u5de6\u8fb9\u5219\u4ee3\u8868\u8d8a\u65e9\u4e4b\u524d\u7684\u65f6\u95f4\u8f74\u5566\u3002\u7531\u56fe5.2.1\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u7684\u77e5\u9053\uff1a<\/p>\n<ul class=\"text_import2\">\n<li>+4\u4ee3\u8868\u5927\u4e8e\u7b49\u4e8e5\u5929\u524d\u7684\u6863\u540d\uff1aex&gt; find \/var -mtime +4<\/li>\n<li>-4\u4ee3\u8868\u5c0f\u4e8e\u7b49\u4e8e4\u5929\u5185\u7684\u6863\u6848\u6863\u540d\uff1aex&gt; find \/var -mtime -4<\/li>\n<li>4\u5219\u662f\u4ee3\u88684-5\u90a3\u4e00\u5929\u7684\u6863\u6848\u6863\u540d\uff1aex&gt; find \/var -mtime 4<\/li>\n<\/ul>\n<p>\u975e\u5e38\u6709\u8da3\u5427\uff01\u4f60\u53ef\u4ee5\u5728 \/var\/ \u76ee\u5f55\u4e0b\u641c\u5bfb\u4e00\u4e0b\uff0c\u611f\u53d7\u4e00\u4e0b\u8f93\u51fa\u6863\u6848\u7684\u5dee\u5f02\u5594\uff01\u518d\u6765\u770b\u770b\u5176\u4ed6 find \u7684\u7528\u6cd5\u5427\uff01<\/p>\n<pre class=\"lang:default decode:true\">\u9009\u9879\u4e0e\u53c2\u6570\uff1a\n3. \u4e0e\u6863\u6848\u6743\u9650\u53ca\u540d\u79f0\u6709\u5173\u7684\u53c2\u6570\uff1a\n    -name filename\uff1a\u641c\u5bfb\u6863\u6848\u540d\u79f0\u4e3a filename \u7684\u6863\u6848\uff1b\n    -size [+-]SIZE\uff1a\u641c\u5bfb\u6bd4 SIZE \u8fd8\u8981\u5927(+)\u6216\u5c0f(-)\u7684\u6863\u6848\u3002\u8fd9\u4e2a SIZE \u7684\u89c4\u683c\u6709\uff1a\n                   c: \u4ee3\u8868 byte\uff0c k: \u4ee3\u8868 1024bytes\u3002\u6240\u4ee5\uff0c\u8981\u627e\u6bd4 50KB\n                   \u8fd8\u8981\u5927\u7684\u6863\u6848\uff0c\u5c31\u662f\u2018 -size +50k \u2019\n    -type TYPE    \uff1a\u641c\u5bfb\u6863\u6848\u7684\u7c7b\u578b\u4e3a TYPE \u7684\uff0c\u7c7b\u578b\u4e3b\u8981\u6709\uff1a\u4e00\u822c\u6b63\u89c4\u6863\u6848 (f),\n                   \u88c5\u7f6e\u6863\u6848 (b, c), \u76ee\u5f55 (d), \u8fde\u7ed3\u6863 (l), socket (s),\n                   \u53ca FIFO (p) \u7b49\u5c5e\u6027\u3002\n    -perm mode  \uff1a\u641c\u5bfb\u6863\u6848\u6743\u9650\u2018\u521a\u597d\u7b49\u4e8e\u2019 mode \u7684\u6863\u6848\uff0c\u8fd9\u4e2a mode \u4e3a\u7c7b\u4f3c chmod\n                 \u7684\u5c5e\u6027\u503c\uff0c\u4e3e\u4f8b\u6765\u8bf4\uff0c -rwsr-xr-x \u7684\u5c5e\u6027\u4e3a 4755 \uff01\n    -perm -mode \uff1a\u641c\u5bfb\u6863\u6848\u6743\u9650\u2018\u5fc5\u987b\u8981\u5168\u90e8\u56ca\u62ec mode \u7684\u6743\u9650\u2019\u7684\u6863\u6848\uff0c\u4e3e\u4f8b\u6765\u8bf4\uff0c\n                 \u6211\u4eec\u8981\u641c\u5bfb -rwxr--r-- \uff0c\u4ea6\u5373 0744 \u7684\u6863\u6848\uff0c\u4f7f\u7528 -perm -0744\uff0c\n                 \u5f53\u4e00\u4e2a\u6863\u6848\u7684\u6743\u9650\u4e3a -rwsr-xr-x \uff0c\u4ea6\u5373 4755 \u65f6\uff0c\u4e5f\u4f1a\u88ab\u5217\u51fa\u6765\uff0c\n                 \u56e0\u4e3a -rwsr-xr-x \u7684\u5c5e\u6027\u5df2\u7ecf\u56ca\u62ec\u4e86 -rwxr--r-- \u7684\u5c5e\u6027\u4e86\u3002\n    -perm +mode \uff1a\u641c\u5bfb\u6863\u6848\u6743\u9650\u2018\u5305\u542b\u4efb\u4e00 mode \u7684\u6743\u9650\u2019\u7684\u6863\u6848\uff0c\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6211\u4eec\u641c\u5bfb\n                 -rwxr-xr-x \uff0c\u4ea6\u5373 -perm +755 \u65f6\uff0c\u4f46\u4e00\u4e2a\u6863\u6848\u5c5e\u6027\u4e3a -rw-------\n                 \u4e5f\u4f1a\u88ab\u5217\u51fa\u6765\uff0c\u56e0\u4e3a\u4ed6\u6709 -rw.... \u7684\u5c5e\u6027\u5b58\u5728\uff01\n\n\u8303\u4f8b\u4e94\uff1a\u627e\u51fa\u6863\u540d\u4e3a passwd \u8fd9\u4e2a\u6863\u6848\n[root@www ~]# find \/ -name passwd\n# \u5229\u7528\u8fd9\u4e2a -name \u53ef\u4ee5\u641c\u5bfb\u6863\u540d\u554a\uff01\n\n\u8303\u4f8b\u516d\uff1a\u627e\u51fa \/var \u76ee\u5f55\u4e0b\uff0c\u6863\u6848\u7c7b\u578b\u4e3a Socket \u7684\u6863\u540d\u6709\u54ea\u4e9b\uff1f\n[root@www ~]# find \/var -type s\n# \u8fd9\u4e2a -type \u7684\u5c5e\u6027\u4e5f\u5f88\u6709\u5e2e\u52a9\u5594\uff01\u5c24\u5176\u662f\u8981\u627e\u51fa\u90a3\u4e9b\u602a\u5f02\u7684\u6863\u6848\uff0c\n# \u4f8b\u5982 socket \u4e0e FIFO \u6863\u6848\uff0c\u53ef\u4ee5\u7528 find \/var -type p \u6216 -type s \u6765\u627e\uff01\n\n\u8303\u4f8b\u4e03\uff1a\u641c\u5bfb\u6863\u6848\u5f53\u4e2d\u542b\u6709 SGID \u6216 SUID \u6216 SBIT \u7684\u5c5e\u6027\n[root@www ~]# find \/ -perm +7000\n# \u6240\u8c13\u7684 7000 \u5c31\u662f ---s--s--t \uff0c\u90a3\u4e48\u53ea\u8981\u542b\u6709 s \u6216 t \u7684\u5c31\u5217\u51fa\uff0c\n# \u6240\u4ee5\u5f53\u7136\u8981\u4f7f\u7528 +7000 \uff0c\u4f7f\u7528 -7000 \u8868\u793a\u8981\u542b\u6709 ---s--s--t \u7684\u6240\u6709\u4e09\u4e2a\u6743\u9650\uff0c\n# \u56e0\u6b64\uff0c\u5c31\u662f +7000 \uff5e\u4e86\u4e4e\uff1f\n\n[root@www ~]# find \/bin \/sbin -perm +6000<\/pre>\n<p>\u4e0a\u8ff0\u8303\u4f8b\u4e2d\u6bd4\u8f83\u6709\u8da3\u7684\u5c31\u5c5e -perm \u8fd9\u4e2a\u9009\u9879\u5566\uff01\u4ed6\u7684\u91cd\u70b9\u5728\u627e\u51fa\u7279\u6b8a\u6743\u9650\u7684\u6863\u6848\u5570\uff01 \u6211\u4eec\u77e5\u9053 SUID \u4e0e SGID \u90fd\u53ef\u4ee5\u8bbe\u5b9a\u5728\u4e8c\u8fdb\u4f4d\u7a0b\u5f0f\u4e0a\uff0c\u5047\u8bbe\u6211\u60f3\u8981\u627e\u51fa\u6765 \/bin, \/sbin \u8fd9\u4e24\u4e2a\u76ee\u5f55\u4e0b\uff0c \u53ea\u8981\u5177\u6709 SUID \u6216 SGID \u5c31\u5217\u51fa\u6765\u8be5\u6863\u6848\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<p>\u56e0\u4e3a SUID \u662f 4 \u5206\uff0cSGID 2 \u5206\uff0c\u603b\u5171\u4e3a 6 \u5206\uff0c\u56e0\u6b64\u53ef\u7528 +6000 \u6765\u5904\u7406\u8fd9\u4e2a\u6743\u9650\uff01 \u81f3\u4e8e find \u540e\u9762\u53ef\u4ee5\u63a5\u591a\u4e2a\u76ee\u5f55\u6765\u8fdb\u884c\u641c\u5bfb\uff01\u53e6\u5916\uff0c <span class=\"text_import2\">find \u672c\u6765\u5c31\u4f1a\u641c\u5bfb\u6b21\u76ee\u5f55<\/span>\uff0c\u8fd9\u4e2a\u7279\u8272\u4e5f\u8981\u7279\u522b\u6ce8\u610f\u5594\uff01 \u6700\u540e\uff0c\u6211\u4eec\u518d\u6765\u770b\u4e00\u4e0b find \u8fd8\u6709\u4ec0\u4e48\u7279\u6b8a\u529f\u80fd\u5427\uff01<\/p>\n<p>find \u7684\u7279\u6b8a\u529f\u80fd\u5c31\u662f\u80fd\u591f\u8fdb\u884c\u989d\u5916\u7684\u52a8\u4f5c(action)\u3002\u6211\u4eec\u5c06\u8303\u4f8b\u516b\u7684\u4f8b\u5b50\u4ee5\u56fe\u89e3\u6765\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n<p><center><img decoding=\"async\" id=\"mail_scale_image_4294971460_1\" class=\" mail_scale_image\" title=\"find \u76f8\u5173\u7684\u989d\u5916\u52a8\u4f5c\" src=\"http:\/\/www.ha97.com\/book\/vbird_linux\/linux_basic\/0220filemanager\/find_exec.gif\" alt=\"\" border=\"0\" \/><br \/>\n\u56fe5.2.2\u3001find \u76f8\u5173\u7684\u989d\u5916\u52a8\u4f5c<\/center>\u8be5\u8303\u4f8b\u4e2d\u7279\u6b8a\u7684\u5730\u65b9\u6709 {} \u4ee5\u53ca ; \u8fd8\u6709 -exec \u8fd9\u4e2a\u5173\u952e\u5b57\uff0c\u8fd9\u4e9b\u4e1c\u897f\u7684\u610f\u4e49\u4e3a\uff1a<\/p>\n<ul class=\"text_import2\">\n<li>{} \u4ee3\u8868\u7684\u662f\u2018\u7531 find \u627e\u5230\u7684\u5185\u5bb9\u2019\uff0c\u5982\u4e0a\u56fe\u6240\u793a\uff0cfind \u7684\u7ed3\u679c\u4f1a\u88ab\u653e\u7f6e\u5230 {} \u4f4d\u7f6e\u4e2d\uff1b<\/li>\n<li>-exec \u4e00\u76f4\u5230 ; \u662f\u5173\u952e\u5b57\uff0c\u4ee3\u8868 find \u989d\u5916\u52a8\u4f5c\u7684\u5f00\u59cb (-exec) \u5230\u7ed3\u675f (;) \uff0c\u5728\u8fd9\u4e2d\u95f4\u7684\u5c31\u662f find \u6307\u4ee4\u5185\u7684\u989d\u5916\u52a8\u4f5c\u3002 \u5728\u672c\u4f8b\u4e2d\u5c31\u662f\u2018 ls -l {} \u2019\u5570\uff01<\/li>\n<li>\u56e0\u4e3a\u2018 ; \u2019\u5728 bash \u73af\u5883\u4e0b\u662f\u6709\u7279\u6b8a\u610f\u4e49\u7684\uff0c\u56e0\u6b64\u5229\u7528\u53cd\u659c\u7ebf\u6765\u8df3\u8131\u3002<\/li>\n<\/ul>\n<p>\u900f\u8fc7\u56fe 5.2.2 \u4f60\u5e94\u8be5\u5c31\u6bd4\u8f83\u5bb9\u6613\u4e86\u89e3 -exec \u5230 ; \u4e4b\u95f4\u7684\u610f\u4e49\u4e86\u5427\uff01<\/p>\n<p>\u5982\u679c\u4f60\u8981\u627e\u7684\u6863\u6848\u662f\u5177\u6709\u7279\u6b8a\u5c5e\u6027\u7684\uff0c\u4f8b\u5982 SUID \u3001\u6863\u6848\u62e5\u6709\u8005\u3001\u6863\u6848\u5927\u5c0f\u7b49\u7b49\uff0c \u90a3\u4e48\u5229\u7528 locate \u662f\u6ca1\u6709\u529e\u6cd5\u8fbe\u6210\u4f60\u7684\u641c\u5bfb\u7684\uff01\u6b64\u65f6 find \u5c31\u663e\u7684\u5f88\u91cd\u8981\u5566\uff01 \u53e6\u5916\uff0cfind \u8fd8\u53ef\u4ee5\u5229\u7528\u4e07\u7528\u5b57\u5143\u6765\u627e\u5bfb\u6863\u540d\u5462\uff01\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f60\u60f3\u8981\u627e\u51fa \/etc \u5e95\u4e0b\u6863\u540d\u5305\u542b httpd \u7684\u6863\u6848\uff0c \u90a3\u4e48\u4f60\u5c31\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">[root@www ~]# find \/etc -name '*httpd*'<\/pre>\n<p>\u4e0d\u4f46\u53ef\u4ee5\u6307\u5b9a\u641c\u5bfb\u7684\u76ee\u5f55(\u8fde\u540c\u6b21\u76ee\u5f55)\uff0c\u5e76\u4e14\u53ef\u4ee5\u5229\u7528\u989d\u5916\u7684\u9009\u9879\u4e0e\u53c2\u6570\u6765\u627e\u5230\u6700\u6b63\u786e\u7684\u6863\u540d\uff01\u771f\u662f\u597d\u597d\u7528\uff01 \u4e0d\u8fc7\u7531\u4e8e find \u5728\u5bfb\u627e\u8d44\u6599\u7684\u65f6\u540e\u76f8\u5f53\u7684\u64cd\u786c\u789f\uff01\u6240\u4ee5\u6ca1\u4e8b\u60c5\u4e0d\u8981\u4f7f\u7528 find \u5566\uff01\u6709\u66f4\u68d2\u7684\u6307\u4ee4\u53ef\u4ee5\u53d6\u4ee3\u5466\uff01\u90a3\u5c31\u662f\u4e0a\u9762\u63d0\u5230\u7684 <a href=\"http:\/\/www.ha97.com\/book\/vbird_linux\/linux_basic\/0220filemanager.htm#whereis\" target=\"_blank\">whereis<\/a>\u00a0\u4e0e <a href=\"http:\/\/www.ha97.com\/book\/vbird_linux\/linux_basic\/0220filemanager.htm#locate\" target=\"_blank\">locate<\/a>\u00a0\u5570\uff01<\/p>\n<hr \/>\n<h6>\u53c2\u6570\u4ee3\u6362\uff1a xargs<\/h6>\n<\/div>\n<div>\n<div class=\"block2\">\n<p>xargs \u662f\u5728\u505a\u4ec0\u4e48\u7684\u5462\uff1f\u5c31\u4ee5\u5b57\u9762\u4e0a\u7684\u610f\u4e49\u6765\u770b\uff0c x \u662f\u52a0\u51cf\u4e58\u9664\u7684\u4e58\u53f7\uff0cargs \u5219\u662f arguments (\u53c2\u6570) \u7684\u610f\u601d\uff0c\u6240\u4ee5\u8bf4\uff0c<span class=\"text_import2\">\u8fd9\u4e2a\u73a9\u610f\u513f\u5c31\u662f\u5728\u4ea7\u751f\u67d0\u4e2a\u6307\u4ee4\u7684\u53c2\u6570\u7684\u610f\u601d\uff01<\/span>\u00a0xargs \u53ef\u4ee5\u8bfb\u5165 stdin \u7684\u8d44\u6599\uff0c\u5e76\u4e14\u4ee5\u7a7a\u767d\u5b57\u5143\u6216\u65ad\u884c\u5b57\u5143\u4f5c\u4e3a\u5206\u8fa8\uff0c\u5c06 stdin \u7684\u8d44\u6599\u5206\u9694\u6210\u4e3a arguments \u3002 \u56e0\u4e3a\u662f\u4ee5\u7a7a\u767d\u5b57\u5143\u4f5c\u4e3a\u5206\u9694\uff0c\u6240\u4ee5\uff0c\u5982\u679c\u6709\u4e00\u4e9b\u6863\u540d\u6216\u8005\u662f\u5176\u4ed6\u610f\u4e49\u7684\u540d\u8bcd\u5185\u542b\u6709\u7a7a\u767d\u5b57\u5143\u7684\u65f6\u5019\uff0c xargs \u53ef\u80fd\u5c31\u4f1a\u8bef\u5224\u4e86\uff5e\u4ed6\u7684\u7528\u6cd5\u5176\u5b9e\u4e5f\u8fd8\u6ee1\u7b80\u5355\u7684\uff01\u5c31\u6765\u770b\u4e00\u770b\u5148\uff01<\/p>\n<pre class=\"lang:default decode:true\">[root@www ~]# xargs [-0epn] command\n\u9009\u9879\u4e0e\u53c2\u6570\uff1a\n-0  \uff1a\u5982\u679c\u8f93\u5165\u7684 stdin \u542b\u6709\u7279\u6b8a\u5b57\u5143\uff0c\u4f8b\u5982 `, , \u7a7a\u767d\u952e\u7b49\u7b49\u5b57\u5143\u65f6\uff0c\u8fd9\u4e2a -0 \u53c2\u6570\u53ef\u4ee5\u5c06\u4ed6\u8fd8\u539f\u6210\u4e00\u822c\u5b57\u5143\u3002\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u7279\u6b8a\u72b6\u6001\u5594\uff01\n-e  \uff1a\u8fd9\u4e2a\u662f EOF (end of file) \u7684\u610f\u601d\u3002\u540e\u9762\u53ef\u4ee5\u63a5\u4e00\u4e2a\u5b57\u4e32\uff0c\u5f53 xargs \u5206\u6790\u5230\u8fd9\u4e2a\u5b57\u4e32\u65f6\uff0c\u5c31\u4f1a\u505c\u6b62\u7ee7\u7eed\u5de5\u4f5c\uff01\n-p  \uff1a\u5728\u6267\u884c\u6bcf\u4e2a\u6307\u4ee4\u7684 argument \u65f6\uff0c\u90fd\u4f1a\u8be2\u95ee\u4f7f\u7528\u8005\u7684\u610f\u601d\uff1b\n-n  \uff1a\u540e\u9762\u63a5\u6b21\u6570\uff0c\u6bcf\u6b21 command \u6307\u4ee4\u6267\u884c\u65f6\uff0c\u8981\u4f7f\u7528\u51e0\u4e2a\u53c2\u6570\u7684\u610f\u601d\u3002\u770b\u8303\u4f8b\u4e09\u3002\n\u5f53 xargs \u540e\u9762\u6ca1\u6709\u63a5\u4efb\u4f55\u7684\u6307\u4ee4\u65f6\uff0c\u9884\u8bbe\u662f\u4ee5 echo \u6765\u8fdb\u884c\u8f93\u51fa\u5594\uff01\n\n\u8303\u4f8b\u4e00\uff1a\u5c06 \/etc\/passwd \u5185\u7684\u7b2c\u4e00\u680f\u53d6\u51fa\uff0c\u4ec5\u53d6\u4e09\u884c\uff0c\u4f7f\u7528 finger \u8fd9\u4e2a\u6307\u4ee4\u5c06\u6bcf\u4e2a\u5e10\u53f7\u5185\u5bb9\u79c0\u51fa\u6765\n[root@www ~]# cut -d':' -f1 \/etc\/passwd |head -n 3| xargs finger\nLogin: root                             Name: root\nDirectory: \/root                        Shell: \/bin\/bash\nNever logged in.\nNo mail.\nNo Plan.\n......\u5e95\u4e0b\u7701\u7565.....\n# \u7531 finger account \u53ef\u4ee5\u53d6\u5f97\u8be5\u5e10\u53f7\u7684\u76f8\u5173\u8bf4\u660e\u5185\u5bb9\uff0c\u4f8b\u5982\u4e0a\u9762\u7684\u8f93\u51fa\u5c31\u662f finger root\n# \u540e\u7684\u7ed3\u679c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u5f53\u4e2d\uff0c\u6211\u4eec\u5229\u7528 cut \u53d6\u51fa\u5e10\u53f7\u540d\u79f0\uff0c\u7528 head \u53d6\u51fa\u4e09\u4e2a\u5e10\u53f7\uff0c\n# \u6700\u540e\u5219\u662f\u7531 xargs \u5c06\u4e09\u4e2a\u5e10\u53f7\u7684\u540d\u79f0\u53d8\u6210 finger \u540e\u9762\u9700\u8981\u7684\u53c2\u6570\uff01\n\n\u8303\u4f8b\u4e8c\uff1a\u540c\u4e0a\uff0c\u4f46\u662f\u6bcf\u6b21\u6267\u884c finger \u65f6\uff0c\u90fd\u8981\u8be2\u95ee\u4f7f\u7528\u8005\u662f\u5426\u52a8\u4f5c\uff1f\n\n[root@www ~]# cut -d':' -f1 \/etc\/passwd |head -n 3| xargs -p finger\nfinger root bin daemon ?...y\n.....(\u5e95\u4e0b\u7701\u7565)....\n# \u5475\u5475\uff01\u8fd9\u4e2a -p \u7684\u9009\u9879\u53ef\u4ee5\u8ba9\u4f7f\u7528\u8005\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u88ab\u8be2\u95ee\u5230\u6bcf\u4e2a\u6307\u4ee4\u662f\u5426\u6267\u884c\uff01\n\n\u8303\u4f8b\u4e09\uff1a\u5c06\u6240\u6709\u7684 \/etc\/passwd \u5185\u7684\u5e10\u53f7\u90fd\u4ee5 finger \u67e5\u9605\uff0c\u4f46\u4e00\u6b21\u4ec5\u67e5\u9605\u4e94\u4e2a\u5e10\u53f7\n[root@www ~]# cut -d':' -f1 \/etc\/passwd | xargs -p -n 5 finger\nfinger root bin daemon adm lp ?...y\n\n.....(\u4e2d\u95f4\u7701\u7565)....\nfinger uucp operator games gopher ftp ?...y\n.....(\u5e95\u4e0b\u7701\u7565)....\n# \u5728\u8fd9\u91cc\u9e1f\u54e5\u4f7f\u7528\u4e86 -p \u8fd9\u4e2a\u53c2\u6570\u6765\u8ba9\u60a8\u5bf9\u4e8e -n \u66f4\u6709\u6982\u5ff5\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u67d0\u4e9b\u6307\u4ee4\u540e\u9762\n# \u53ef\u4ee5\u63a5\u7684 arguments \u662f\u6709\u9650\u5236\u7684\uff0c\u4e0d\u80fd\u65e0\u9650\u5236\u7684\u7d2f\u52a0\uff0c\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528 -n\n# \u6765\u5e2e\u52a9\u6211\u4eec\u5c06\u53c2\u6570\u5206\u6210\u6570\u4e2a\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u5206\u522b\u518d\u4ee5\u6307\u4ee4\u6765\u6267\u884c\uff01\u8fd9\u6837\u5c31 OK \u5566\uff01^_^\n\n\u8303\u4f8b\u56db\uff1a\u540c\u4e0a\uff0c\u4f46\u662f\u5f53\u5206\u6790\u5230 lp \u5c31\u7ed3\u675f\u8fd9\u4e32\u6307\u4ee4\uff1f\n[root@www ~]# cut -d':' -f1 \/etc\/passwd | xargs -p -e'lp' finger\nfinger root bin daemon adm ?...\n# \u4ed4\u7ec6\u4e0e\u4e0a\u9762\u7684\u6848\u4f8b\u505a\u6bd4\u8f83\u3002\u4e5f\u540c\u65f6\u6ce8\u610f\uff0c\u90a3\u4e2a -e'lp' \u662f\u8fde\u5728\u4e00\u8d77\u7684\uff0c\u4e2d\u95f4\u6ca1\u6709\u7a7a\u767d\u952e\u3002\n# \u4e0a\u4e2a\u4f8b\u5b50\u5f53\u4e2d\uff0c\u7b2c\u4e94\u4e2a\u53c2\u6570\u662f lp \u554a\uff0c\u90a3\u4e48\u6211\u4eec\u4e0b\u8fbe -e'lp' \u540e\uff0c\u5219\u5206\u6790\u5230 lp\n# \u8fd9\u4e2a\u5b57\u4e32\u65f6\uff0c\u540e\u9762\u7684\u5176\u4ed6 stdin \u7684\u5185\u5bb9\u5c31\u4f1a\u88ab xargs \u820d\u5f03\u6389\u4e86\uff01<\/pre>\n<p>\u5176\u5b9e\uff0c\u5728 man xargs \u91cc\u9762\u5c31\u6709\u4e09\u56db\u4e2a\u5c0f\u8303\u4f8b\uff0c\u60a8\u53ef\u4ee5\u81ea\u884c\u53c2\u8003\u4e00\u4e0b\u5185\u5bb9\u3002 \u6b64\u5916\uff0c xargs \u771f\u7684\u662f\u5f88\u597d\u7528\u7684\u4e00\u4e2a\u73a9\u610f\u513f\uff01\u60a8\u771f\u7684\u9700\u8981\u597d\u597d\u7684\u53c2\u8be6\u53c2\u8be6\uff01\u4f1a\u4f7f\u7528 xargs \u7684\u539f\u56e0\u662f\uff0c <span class=\"text_import2\">\u5f88\u591a\u6307\u4ee4\u5176\u5b9e\u5e76\u4e0d\u652f\u63f4\u7ba1\u7ebf\u547d\u4ee4\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u900f\u8fc7 xargs \u6765\u63d0\u4f9b\u8be5\u6307\u4ee4\u5f15\u7528 standard input \u4e4b\u7528<\/span>\uff01\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6211\u4eec\u4f7f\u7528\u5982\u4e0b\u7684\u8303\u4f8b\u6765\u8bf4\u660e\uff1a<\/p>\n<pre class=\"lang:default decode:true\">\u8303\u4f8b\u4e94\uff1a\u627e\u51fa \/sbin \u5e95\u4e0b\u5177\u6709\u7279\u6b8a\u6743\u9650\u7684\u6863\u540d\uff0c\u5e76\u4f7f\u7528 ls -l \u5217\u51fa\u8be6\u7ec6\u5c5e\u6027\n[root@www ~]# find \/sbin -perm +7000 | ls -l\n# \u7ed3\u679c\u7adf\u7136\u4ec5\u6709\u5217\u51fa root \u6240\u5728\u76ee\u5f55\u4e0b\u7684\u6863\u6848\uff01\u8fd9\u4e0d\u662f\u6211\u4eec\u8981\u7684\uff01\n# \u56e0\u4e3a ll (ls) \u5e76\u4e0d\u662f\u7ba1\u7ebf\u547d\u4ee4\u7684\u539f\u56e0\u554a\uff01\n\n[root@www ~]# find \/sbin -perm +7000 | xargs ls -l\n-rwsr-xr-x 1 root root 70420 May 25  2008 \/sbin\/mount.nfs\n-rwsr-xr-x 1 root root 70424 May 25  2008 \/sbin\/mount.nfs4\n-rwxr-sr-x 1 root root  5920 Jun 15  2008 \/sbin\/netreport\n....(\u5e95\u4e0b\u7701\u7565)....<\/pre>\n<h6>\u51cf\u53f7 &#8211; \u7684\u7528\u9014<\/h6>\n<\/div>\n<div class=\"block2\">\n<p>\u7ba1\u7ebf\u547d\u4ee4\u5728 bash \u7684\u8fde\u7eed\u7684\u5904\u7406\u7a0b\u5e8f\u4e2d\u662f\u76f8\u5f53\u91cd\u8981\u7684\uff01\u53e6\u5916\uff0c\u5728 log file \u7684\u5206\u6790\u5f53\u4e2d\u4e5f\u662f\u76f8\u5f53\u91cd\u8981\u7684\u4e00\u73af\uff0c \u6240\u4ee5\u8bf7\u7279\u522b\u7559\u610f\uff01\u53e6\u5916\uff0c\u5728\u7ba1\u7ebf\u547d\u4ee4\u5f53\u4e2d\uff0c\u5e38\u5e38\u4f1a\u4f7f\u7528\u5230\u524d\u4e00\u4e2a\u6307\u4ee4\u7684 stdout \u4f5c\u4e3a\u8fd9\u6b21\u7684 stdin \uff0c \u67d0\u4e9b\u6307\u4ee4\u9700\u8981\u7528\u5230\u6863\u6848\u540d\u79f0 (\u4f8b\u5982 tar) \u6765\u8fdb\u884c\u5904\u7406\u65f6\uff0c\u8be5 <span style=\"color: #ff0000;\"><strong>stdin \u4e0e stdout \u53ef\u4ee5\u5229\u7528\u51cf\u53f7 &#8220;-&#8221; \u6765\u66ff\u4ee3<\/strong><\/span>\uff0c \u4e3e\u4f8b\u6765\u8bf4\uff1a<\/p>\n<pre class=\"lang:default decode:true\">[root@www ~]# tar -cvf - \/home | tar -xvf -<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u8bf4\uff1a\u2018\u6211\u5c06 \/home \u91cc\u9762\u7684\u6863\u6848\u7ed9\u4ed6\u6253\u5305\uff0c\u4f46\u6253\u5305\u7684\u8d44\u6599\u4e0d\u662f\u7eaa\u5f55\u5230\u6863\u6848\uff0c\u800c\u662f\u4f20\u9001\u5230 stdout\uff1b \u7ecf\u8fc7\u7ba1\u7ebf\u540e\uff0c\u5c06 tar -cvf &#8211; \/home \u4f20\u9001\u7ed9\u540e\u9762\u7684 tar -xvf &#8211; \u2019\u3002\u540e\u9762\u7684\u8fd9\u4e2a &#8211; \u5219\u662f\u53d6\u7528\u524d\u4e00\u4e2a\u6307\u4ee4\u7684 stdout\uff0c \u56e0\u6b64\uff0c\u6211\u4eec\u5c31\u4e0d\u9700\u8981\u4f7f\u7528 file \u4e86\uff01\u8fd9\u662f\u5f88\u5e38\u89c1\u7684\u4f8b\u5b50\u5594\uff01\u6ce8\u610f\u6ce8\u610f\uff01<\/p>\n<hr \/>\n<h5><strong>Linux\u7cfb\u7edf\u4e2d\u7684find\u547d\u4ee4\u548cxargs\u547d\u4ee4\u8be6\u89e3<\/strong><\/h5>\n<\/div>\n<\/div>\n<h6><span style=\"color: #000000; font-size: medium;\">\u53c2\u8003\u94fe\u63a5\uff1a<\/span><\/h6>\n<ul>\n<li><a href=\"https:\/\/www.google.com\/#newwindow=1&amp;q=linux+%22argument+list+marker%22\" target=\"_blank\"><span style=\"color: #000000; font-size: medium;\">linux &#8220;argument list marker&#8221; &#8211; Google Search<\/span><\/a><\/li>\n<li><a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-bsd-xargs-construct-argument-lists-utility\/\" target=\"_blank\"><span style=\"color: #000000; font-size: medium;\">xargs: How To Control and Use Command Line Arguments<\/span><\/a><\/li>\n<li>\n<p style=\"color: #303030;\"><a style=\"color: #366900;\" href=\"http:\/\/linux.101hacks.com\/linux-commands\/xargs-command-examples\/\">http:\/\/linux.101hacks.com\/linux-commands\/xargs-command-examples\/<\/a><\/p>\n<p style=\"color: #303030;\"><a style=\"color: #366900;\" href=\"http:\/\/www.computerhope.com\/unix\/xargs.htm\">http:\/\/www.computerhope.com\/unix\/xargs.htm<\/a><\/p>\n<p style=\"color: #303030;\"><a style=\"color: #366900;\" href=\"http:\/\/offbytwo.com\/2011\/06\/26\/things-you-didnt-know-about-xargs.html\">http:\/\/offbytwo.com\/2011\/06\/26\/things-you-didnt-know-about-xargs.html<\/a><\/p>\n<p style=\"color: #303030;\"><a style=\"color: #366900;\" href=\"http:\/\/unixhelp.ed.ac.uk\/CGI\/man-cgi?xargs\">http:\/\/unixhelp.ed.ac.uk\/CGI\/man-cgi?xargs<\/a><\/p>\n<p style=\"color: #303030;\"><a href=\"http:\/\/www.cnblogs.com\/pengdonglin137\/p\/3384083.html\" target=\"_blank\">xargs: How To Control and Use Command Line Arguments<\/a><\/p>\n<h4 class=\"post-title entry-title item\" style=\"color: #353535;\">&#8220;xargs&#8221; All-IN-One Tutorial Guide<\/h4>\n<\/li>\n<li><span style=\"color: #000000; font-size: medium;\">&#8220;xargs&#8221; All-IN-One Tutorial Guide | UnixMantra <a href=\"http:\/\/www.unixmantra.com\/2013\/12\/xargs-all-in-one-tutorial-guide.html\">http:\/\/www.unixmantra.com\/2013\/12\/xargs-all-in-one-tutorial-guide.html<\/a><\/span><\/li>\n<\/ul>\n<ul>\n<li>xargs command <a href=\"http:\/\/jonlabelle.com\/snippets\/view\/shell\/xargs-command\">http:\/\/jonlabelle.com\/snippets\/view\/shell\/xargs-command<\/a><\/li>\n<\/ul>\n<hr \/>\n<ul>\n<li>\n<p style=\"color: #111111;\">xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.<\/p>\n<h2 style=\"color: #111111;\">xargs is more\u00a0<span style=\"color: #6666cc;\">safer and easy<\/span>\u00a0to use<\/h2>\n<p style=\"color: #111111;\">xargs functionality can be achived using the backquote feature of shell. But, it offers more options. It can deal with blanks or special characters in file names easily. It is often used with find, grep and other commands.<\/p>\n<h2 style=\"color: #111111;\">xargs examples<\/h2>\n<p style=\"color: #111111;\">For example following example will print 1 2 3 4 using xargs (echo command is default)<br \/>\n<code>$ echo 1 2 3 4 | xargs echo<\/code><br \/>\nOR<br \/>\n<code>$ echo 1 2 3 4 | xargs<\/code><br \/>\nYou can force xargs to use at most max-args arguments per command line. For example following will use first two argument per command:<br \/>\n<code>$ echo 1 2 3 4 | xargs -n 2<\/code><br \/>\nFind all .bak files in or below the current directory and delete them.<br \/>\n<code>$ find . -name \"*.bak\" -type f -print | xargs \/bin\/rm -f<\/code><\/p>\n<h2 style=\"color: #111111;\"><span style=\"color: #993399;\">{} as the argument list marker<\/span><\/h2>\n<p style=\"color: #111111;\">{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .bak files in or below the current directory and move them to ~\/.old.files directory:<br \/>\n<code>$ find . -name \"*.bak\" -print0 | xargs -0 -I {} mv {} ~\/old.files<\/code><br \/>\nYou can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:<br \/>\n<code>$ find . -name \"*.bak\" -print0 | xargs -0 -I file mv file ~\/old.files<\/code><br \/>\nWhere,<\/p>\n<ol style=\"color: #111111;\">\n<li><strong>-0<\/strong>\u00a0If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.<\/li>\n<li><strong>-I\u00a0<\/strong>Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.<\/li>\n<\/ol>\n<h3 style=\"color: #111111;\">Dealing\u00a0<span style=\"color: #009900;\">file names with blank spaces and newline<\/span><\/h3>\n<p style=\"color: #111111;\">The following will work incorrectly if there are any filenames containing newlines or spaces (it will find out all .mp3 file located in current directory and play them using mplayer):<br \/>\n<code>$ find . -iname \"*.mp3\" -print | xargs mplayer<\/code><br \/>\nTo get rid of this problem use -0 option:<br \/>\n<code>$ find . -iname \"*.mp3\" -print0 | xargs -0 -I mp3file mplayer mp3file<\/code><br \/>\nTo find out all *.c file located in 100s of subdirectories and move them to another directory called ~\/old.src, use:<br \/>\n<code>$ find \/path\/to\/dir -iname \"*.c\" -print0 | xargs -0 -I file mv file ~\/old.src<\/code><\/p>\n<h2 style=\"color: #111111;\">Avoiding\u00a0<span style=\"color: #ff0000;\">errors and resource hungry problems<\/span>\u00a0with xargs and find combo<\/h2>\n<p style=\"color: #111111;\">To copy all media files to another location called \/bakup\/iscsi, you can use cp as follows:<br \/>\n<code>$ cp -r -v -p \/share\/media\/mp3\/ \/backup\/iscsi\/mp3<\/code><br \/>\nHowever, cp command may fail if an error occurs such as if the number of files is too large for the cp command to handle. xargs in combination with find can handle such operation nicely. xargs is more resource efficient and will not halt with an error:<\/p>\n<pre class=\"lang:default decode:true \">$ find \/share\/media\/mp3\/ -type f -name \"*.mp3\" -print0 | xargs -0 -r -I file cp -v -p file --target-directory=\/bakup\/iscsi\/mp3<\/pre>\n<p>Please note that all of the above commands are tested with GNU\/xargs version. BSD and UNIX xargs command may not have options such as -r. Please refer to your local xargs man page for further info:<\/p>\n<p style=\"color: #111111;\"><code>man xargs<\/code><\/p>\n<\/li>\n<li><span style=\"color: #000000; font-size: medium;\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/span><\/li>\n<li>\n<div style=\"color: #333333;\"><b>xargs\u00a0<\/b>is a just like &#8220;awk&#8221; ,&#8221;find&#8221; &amp; &#8220;grep&#8221; commands processes the standard input on all unix flavoured operating sysems.<\/div>\n<div style=\"color: #333333;\">Basically &#8220;<b>xargs<\/b>&#8221; is used to remove or do some operation on long list of file names which were produced by &#8220;<b>find<\/b>&#8221; &amp; &#8220;<b>grep<\/b>&#8221; commands.Usually many UNIX operating system doesn&#8217;t accept such a long list of argument.UNIX\u00a0<b>xargs<\/b>\u00a0command divide that list into sub-list with acceptable length and made it work.For example I&#8217;d like to find out all *.sh file located in 100s of sub-directories and move them to another directory called\u00a0<b>~\/back.scripts<\/b>. How do I use command line args with xargs to achieve the same?as per man page &#8220;<b>xargs<\/b>&#8221; is used to execute a command, passing constructed argument list(s). The arguments are typically a long list of filenames (generated by\u00a0<b>ls<\/b>\u00a0or\u00a0<b>find\u00a0<\/b>etc) that are passed to\u00a0<b>xargs<\/b>\u00a0via a pipe.<\/div>\n<h4 style=\"color: #333333;\">Some features:<\/h4>\n<ul style=\"color: #333333;\">\n<li>xargs can execute the command supplying some initial arguments directly, and reading the remaining arguments from standard input (or piped input).<\/li>\n<li>xargs passes arguments to command in several bundles, this allows command to process more arguments than it could normally handle at once.<\/li>\n<li>Arguments in the standard input must be separated by unquoted blank characters, or unescaped blank characters or newline characters.<\/li>\n<li>Characters can be quoted by enclosing them in &#8220;double-quotes&#8221; (non-double-quote and non-newline chars only).<\/li>\n<li>Characters can be quoted by enclosing them in &#8216;apostrophes&#8217; (non-apostrophe and non-newline chars only).<\/li>\n<li>Any unquoted character can be escaped by preceding it with a backslash.<\/li>\n<\/ul>\n<div style=\"color: #333333;\"><b>e.g.<\/b>\u00a0file1 file2 &#8220;file three&#8221; &#8216;file four&#8217; file five<br \/>\nIf command is omitted then the equivalent of\u00a0<b>\/bin\/echo<\/b>\u00a0is used.<\/p>\n<h2>\u00a0Exit Status<\/h2>\n<div id=\"xargs__edcfa85233mela\" class=\"p\">This command returns the following exit values:<\/div>\n<table class=\"table\" border=\"0\" summary=\"\" frame=\"void\" rules=\"none\" cellspacing=\"0\" cellpadding=\"4\">\n<thead class=\"thead\" align=\"left\">\n<tr class=\"row\">\n<th id=\"d491553e577\" class=\"entry\" valign=\"top\" width=\"8.33%\">Item<\/th>\n<th id=\"d491553e579\" class=\"entry\" valign=\"top\" width=\"91.66%\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"tbody\">\n<tr id=\"xargs__row-d3e31387\" class=\"row\">\n<td class=\"entry\" headers=\"d491553e577 \" valign=\"top\" width=\"8.33%\"><b class=\"ph b\">0<\/b><\/td>\n<td class=\"entry\" headers=\"d491553e579 \" valign=\"top\" width=\"91.66%\">All invocations of the\u00a0<i class=\"ph i\">Command<\/i>\u00a0parameter returned exit status 0.<\/td>\n<\/tr>\n<tr id=\"xargs__row-d3e31396\" class=\"row\">\n<td class=\"entry\" headers=\"d491553e577 \" valign=\"top\" width=\"8.33%\"><b class=\"ph b\">1-125<\/b><\/td>\n<td class=\"entry\" headers=\"d491553e579 \" valign=\"top\" width=\"91.66%\">A command line meeting the specified requirements could not be assembled, one or more of the invocations of the\u00a0<i class=\"ph i\">Command<\/i>\u00a0parameter returned a non-zero exit status, or some other error occurred.<\/td>\n<\/tr>\n<tr id=\"xargs__row-d3e31405\" class=\"row\">\n<td class=\"entry\" headers=\"d491553e577 \" valign=\"top\" width=\"8.33%\"><b class=\"ph b\">126<\/b><\/td>\n<td class=\"entry\" headers=\"d491553e579 \" valign=\"top\" width=\"91.66%\"><i class=\"ph i\">Command<\/i>\u00a0was found but could not be invoked.<\/td>\n<\/tr>\n<tr id=\"xargs__row-d3e31414\" class=\"row\">\n<td class=\"entry\" headers=\"d491553e577 \" valign=\"top\" width=\"8.33%\"><b class=\"ph b\">127<\/b><\/td>\n<td class=\"entry\" headers=\"d491553e579 \" valign=\"top\" width=\"91.66%\"><i class=\"ph i\">Command<\/i>\u00a0could not be found.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h4 style=\"color: #333333;\">Examples<\/h4>\n<div style=\"color: #333333;\">Find all the .mp3 files in the music folder and pass to the ls command, -print0 is required if any filenames contain whitespace.:<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">\u00a0\u00a0 find .\/music -name &#8220;*.mp3&#8221; -print0 | xargs -0 ls<\/div>\n<p>Find all files in the work folder, pass to grep and search for profit:<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">\u00a0\u00a0 find .\/work -print | xargs grep &#8220;profit&#8221;<\/div>\n<\/div>\n<h4 style=\"color: #333333;\"><span style=\"color: #ff0000;\">{} as the argument list marker<\/span><\/h4>\n<div style=\"color: #333333;\">{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .bak files in or below the current directory and move them to ~\/.old.files directory:<\/div>\n<div style=\"color: #333333;\">\n<div id=\"com_box\" style=\"color: #222222;\">$ find . -name &#8220;*.sh&#8221; -print0 | xargs -0 -I {} mv {} ~\/back.scripts<\/div>\n<p>You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">$ find . -name &#8220;*.sh&#8221; -print0 | xargs -0 -I file mv file ~\/back.scripts<\/div>\n<p>Where,<br \/>\n<b>-0<\/b>\u00a0<span style=\"color: #ff0000;\">If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.(<strong><span style=\"color: #000000;\">\u7528\u4e8e\u5904\u7406\u6587\u4ef6\u540d\u4e2d\u542b\u6709\u7a7a\u683c\u6216\u7a7a\u884c\u7b49\u7a7a\u767d\u5b57\u7b26\u7684\u60c5\u51b5<\/span><\/strong>)<\/span><br \/>\n<b>-I<\/b>\u00a0Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.<\/p>\n<\/div>\n<h2 style=\"color: #333333;\">10 Popular\u00a0 &#8220;XARGS&#8221; Command Examples:<\/h2>\n<h3 style=\"color: #333333;\">1) With&amp; Without &#8220;xargs&#8221; observation:<\/h3>\n<div style=\"color: #333333;\">\u00a0in this example of xargs command we will see how output changes with use of xargs command in unix or Linux. Here is the output of find command without xargs first and than with xargs, you can clearly see that multiline output is converted into single line:<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">um@server#find . -name &#8220;*sh*&#8221;<br \/>\n.\/.bash_history<br \/>\n.\/.bash_profile<br \/>\n.\/.bash_profile.cf-before-edit<br \/>\n.\/.cshrc<br \/>\n.\/.cshrc.cf-before-edit<br \/>\n.\/.sh_history<br \/>\n.\/.ssh<br \/>\n.\/.ssh2<br \/>\n.\/scripts\/aix_sysinfo.ksh<br \/>\n.\/scripts\/chperm_messages.sh<br \/>\n.\/scripts\/linux_sysinfo.ksh<br \/>\n.\/scripts\/solaris_sysinfo_v1.1.ksh<br \/>\n.\/testlocked.kshum@server# find . -name &#8220;*bash*&#8221; | xargs<br \/>\n.\/.bash_history .\/.bash_profile .\/.bash_profile.cf-before-edit .\/.cshrc .\/.cshrc.cf-before-edit .\/.sh_history .\/.ssh .\/.ssh2 .\/scripts\/aix_sysinfo.ksh .\/scripts\/chperm_messages.sh .\/scripts\/linux_sysinfo.ksh .\/scripts\/solaris_sysinfo_v1.1.ksh .\/testlocked.ksh<\/div>\n<\/div>\n<h3 style=\"color: #333333;\">2) Xargs with grep:<\/h3>\n<div style=\"color: #333333;\">When you use &#8220;xargs&#8221; in conjusction with find and grep , the grep will look for the specifig word in\u00a0 each file in the from the stanadard input.<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">#find . -name &#8220;*.sh&#8221; | xargs grep &#8220;ksh&#8221;<\/div>\n<p>In the above exanmple first find all .sh\u00a0 files from current directory or below and than on each .sh file look for word &#8220;ksh&#8221;.<\/p>\n<\/div>\n<h3 style=\"color: #333333;\">3) <span style=\"color: #ff0000;\"><strong>Covert muti line output into single line<\/strong><\/span><\/h3>\n<div style=\"color: #333333;\">best example of xargs is\u00a0 converting output of one command into one line. For example you can run any command and then combine xargs to convert output into single line. here is an example xargs in unix which does that.<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">um@server#ls -1 *.sh<br \/>\nlinux_sysinfo.sh<br \/>\naix_sysinfo.sh<br \/>\naudit_script.sh<br \/>\nchperm_messages.shum@system#ls -1 *.sh | xargs<br \/>\nlinux_sysinfo.sh aix_sysinfo.sh audit_script.sh chperm_messages.sh<\/div>\n<\/div>\n<h3 style=\"color: #333333;\">4) To Delete temporary files using xargs &amp; find:<\/h3>\n<div style=\"color: #333333;\">Another common example of xargs command in unix is removing temporary files from system.<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">#find \/tmp -name &#8220;*.tmp&#8221; | xargs rm<\/div>\n<p>This will remove all .tmp file from \/tmp or below directory. xargs in unix is very fast as compared to deleting single file at a time which can also be done by using find command alone<\/p>\n<h3>5)\u00a0 <strong><span style=\"color: #ff0000;\">xargs -0 to handle space in file name<\/span><\/strong><\/h3>\n<p>Above example of xargs command in unix will not work as expected if any of file name contains space or new line on it. To avoid this problem we use find -print0 to produce null separated file name and xargs-0 to handle null separated items. Here is an example of xargs command in unix which can handle file name with spaces and newline:<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">#find \/tmp -name &#8220;*.tmp&#8221; -print0 | xargs -0 rm<\/div>\n<\/div>\n<h3 style=\"color: #333333;\">6) Counting number of lines\/words\/characters in each file using xargs &amp; find:<\/h3>\n<div style=\"color: #333333;\">&#8220;find&#8221;in conjuction with &#8220;xargs&#8221; and &#8220;wc&#8221;\u00a0 we can count number of lines\/words\/characters in each file under a perticaular directory.<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">um@server#ls -1 *.sh | xargs wc -l<br \/>\n112 linux_sysinfo.sh<br \/>\n85\u00a0 aix_sysinfo.sh<br \/>\n35\u00a0 audit_script.sh<br \/>\n18\u00a0 chperm_messages.sh<br \/>\n250 total<\/div>\n<p><b>Note:\u00a0<\/b>you can use &#8216;-c&#8217; &amp; &#8216;-w&#8217; with wc to obtain number of characters and words repectively.<\/p>\n<\/div>\n<h3 style=\"color: #333333;\">7) xargs and cut command in Unix:<\/h3>\n<div style=\"color: #333333;\">\u00a0Though most of xargs examples in unix will be along with find and grep command but xargs is not just limited to this two it can also be used with any command which generated long list of input for example we can use xargs with cut command in unix. In below example of unix xargs we will xargs example with cut command. for using cut command let&#8217;s first create a .csv file with some data e.g.<\/p>\n<div id=\"com_box\" style=\"color: #222222;\">um@server# cat fruits.txt<br \/>\nOrange,Greenorange<br \/>\nMango,Redmango<br \/>\nBanana,PinkbananaNow we will display name of actual fruit from first column using xargs command in one line:um@server:\/etc cut -d, -f1 smartphones.csv | sort | xargs<br \/>\nOrange Mango Banana<\/div>\n<\/div>\n<h3 style=\"color: #333333;\">8)<strong><span style=\"color: #ff0000;\">To insert file names into the middle of command lines<\/span><\/strong>, type:\uff08<span style=\"color: #ff0000;\">\u6279\u91cf\u91cd\u547d\u540d\uff0c\u800c\u4e14\u8003\u8651\u5230\u4e86\u6587\u4ef6\u540d\u4e2d\u7684\u7a7a\u767d\u7b26\u7b49\u7279\u6b8a\u5b57\u7b26<\/span>\uff09<\/h3>\n<div id=\"com_box\" style=\"color: #222222;\">um@server#ls | xargs\u00a0 -t\u00a0 -I\u00a0 {} mv {} {}.oldThis command sequence renames all files in the current directory by adding .old to the end of each name. The -I flag tells the xargs command to insert each line of the ls directory listing where {} (braces) appear. If the current directory contains the files chap1, chap2, and chap3, this constructs the following commands:#mv chap1 chap1.old<br \/>\n#mv chap2 chap2.old<br \/>\n#mv chap3 chap3.old<\/div>\n<h3 style=\"color: #333333;\">9) To run a command on files that you select individually, type:<\/h3>\n<div id=\"com_box\" style=\"color: #222222;\">um@server# ls | xargs\u00a0 -p\u00a0 -n\u00a0 1 ar r lib.aThis command sequence allows you to select files to add to the lib.a library. The -p flag tells the xargs command to display each ar command it constructs and to ask if you want to run it. Type y to run the command. Press the any other key if you do not want to run the command.Something similar to the following displays:ar r lib.a chap1 ?&#8230;<br \/>\nar r lib.a chap2 ?&#8230;<br \/>\nar r lib.a chap3 ?&#8230;<\/div>\n<h3 style=\"color: #333333;\">10) <span style=\"color: #ff0000;\">To construct a command that contains a specific number of arguments and to insert those arguments into the middle of a command line<\/span>, type:<\/h3>\n<div id=\"com_box\" style=\"color: #222222;\">um@server# ls | xargs -n6 | xargs -I{} echo {} &#8211; some files in the directoryIf the current directory contains files chap1 through chap10, the output constructed will be the following:<br \/>\nchap1 chap2 chap3 chap4 chap5 chap6 &#8211; some files in the directory<br \/>\nchap7 chap8 chap9 chap10 &#8211; some file in the directory<\/div>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u4e0b\u4f7f\u7528md5sum\u9012\u5f52\u751f\u6210\u6574\u4e2a\u76ee\u5f55\u7684MD5\u503c \u4eca\u5929\u8981\u7528\u4f7f\u7528md5sum\u64cd\u4f5c\u76ee\u5f55\uff0c\u53d1\u73b0\u5b83\u8fd8\u4e0d\u652f\u6301\u9012\u5f52 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[89,90],"class_list":["post-156","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-find","tag-xargs"],"views":4026,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}