Xargs从标准输入或者文件读取数据,生成并执行命令,也就是它可以获取生一个命令的输出并将它作为参数传给其他指令。
如果没有指定确定的指令,xargs会默认执行echo指令。
日常工作中,xargs指令非常有用。本文将给初学者讲解几条实用的Linux xargs指令例子
- 找出所有的.png文件并用tar工具进行归档整理
# -print0: print the files you find # -0: deal with the spaces in the files name find ~/Downloads/ -name *.png -type f -print0 | xargs -0 tar -cvzf tar: Removing leading `/' from member names /home/wlin/Downloads/Screen Shot 2019-03-18 at 1.49.53 /home/wlin/Downloads/pycharm-community-2018.3.4/bin/ /home/wlin/Downloads/reproduced_error (1).png /home/wlin/Downloads/reproduced_error (3).png /home/wlin/Downloads/Screenshot from 2019-01-18 /home/wlin/Downloads/reproduced_error (2).png /home/wlin/Downloads/reproduced_error.png
- 将多行输出变为一行输出
# print all files under the /tmp dir in one line $ ls /tmp | xargs 1 hsperfdata_root hsperfdata_wlin install_python.sh krb5cc_24766_zKHknd log lua_KKkOvA mozilla_wlin0 pip-uninstall-fnntxqjn ssr -XzjNM9 -7Qprc5 # print all user accounts in one line $ cut -d: -f1 < /etc/passwd | xargs root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-network dbus polkitd unbound rpc postfix colord sshd saslauth abrt openvpn setroubleshoot rtkit pulse gluster radvd ntp chrony tss usbmuxd geoclue qemu nm-openvpn sssd rpcuser nfsnobody gdm rhel-liveuser avahi mysql dictd cara postgres dev test apache
- 统计某一目录下每个文件的单词数
$ find /home/wlin/RC_CI/ -name *.py -type f -print0 | xargs -0 wc 58 158 2136 /home/wlin/RC_CI/auto_testing_CI/talk_to_rc_jenkins.py 67 204 2206 /home/wlin/RC_CI/auto_testing_CI/confluence_client.py 56 184 2698 /home/wlin/RC_CI/auto_testing_CI/nightly_ci3.py 122 443 4706 /home/wlin/RC_CI/auto_testing_CI/talk_to_rc_jenkins_to_manage_E2E_testing.py 58 149 2119 /home/wlin/RC_CI/auto_testing_CI/talk_to_jenkins_to_send_ts2_hunter_report.py 219 943 11084 /home/wlin/RC_CI/auto_testing_CI/talk_to_rc_jenkins_to_hunt_TS2_failure.py 144 564 6547 /home/wlin/RC_CI/auto_testing_CI/talk_to_perf_jenkins.py 45 107 1551 /home/wlin/RC_CI/auto_testing_CI/single_performance_report_parser.py
- 找到所有符合条件的文件并批量删除
# '-v' shows which files are removed $ find ~/RC_CI -name auto_testing_CI -type d -print0 | xargs -0 rm -v -rf removed ‘/home/wlin/RC_CI/auto_testing_CI/CI_Shell_format_perf_report.sh’ removed ‘/home/wlin/RC_CI/auto_testing_CI/talk_to_rc_jenkins.py’ removed ‘/home/wlin/RC_CI/auto_testing_CI/CI_Shell_common_usage.sh’ removed ‘/home/wlin/RC_CI/auto_testing_CI/confluence_client.py’ removed ‘/home/wlin/RC_CI/auto_testing_CI/CI_Shell_format_robot_report.sh’
- 将文件批量复制到指定的多个目录下
$ echo ~/RC_CI/auto_testing_CI ~/RC_CI/bug_regression_CI | xargs -n 1 cp -v ‘’ -> ‘/home/wlin/RC_CI/auto_testing_CI/’ ‘’ -> ‘/home/wlin/RC_CI/bug_regression_CI/’
- 批量重命名文件夹和文件
$ find /home/wlin/Downloads/ -name -type f | xargs -n1 rename -v '' '' `/home/wlin/Downloads/phantomjs-1.9.8/src/' -> `/home/wlin/Downloads/phantomjs-1.9.8/src/' `/home/wlin/Downloads/phantomjs-2.0.0/src/' -> `/home/wlin/Downloads/phantomjs-2.0.0/src/'
- 删除目录下除了特定文件外的所有文件
$ find . -type f -not -name '*gz' -print0 | xargs -0 rm -v removed ‘./errata_services_template/Errata_Makefile’ removed ‘./errata_services_template/’ removed ‘./errata_services_template/’ removed ‘./errata_services_template/’ removed ‘./errata_services_template/’ removed ‘./errata_services_template/’
- 遇到特定字符终止执行
# If the end of file string occurs as a line of input, the rest of the input is ignored. $ echo 'aaa ddd bbb ccc' > [wlin@localhost upshift]$ xargs -E 'ddd' -a echo aaa
- 输出执行的指令
$ find ~/Downloads -name "" -type f | xargs -0 -t echo /home/wlin/Downloads// /home/wlin/Downloads//