Linux xargs用法的简单例子

Xargs从标准输入或者文件读取数据,生成并执行命令,也就是它可以获取生一个命令的输出并将它作为参数传给其他指令。

如果没有指定确定的指令,xargs会默认执行echo指令。

日常工作中,xargs指令非常有用。本文将给初学者讲解几条实用的Linux xargs指令例子

  1. 找出所有的.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 images.tar.gz
    tar: Removing leading `/' from member names
    /home/wlin/Downloads/Screen Shot 2019-03-18 at 1.49.53 PM.png
    /home/wlin/Downloads/pycharm-community-2018.3.4/bin/pycharm.png
    /home/wlin/Downloads/reproduced_error (1).png
    /home/wlin/Downloads/reproduced_error (3).png
    /home/wlin/Downloads/Screenshot from 2019-01-18 08-34-04.png
    /home/wlin/Downloads/reproduced_error (2).png
    /home/wlin/Downloads/reproduced_error.png
    
  2. 将多行输出变为一行输出
    # 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 systemd-private-62580f5e23a84a1bbdf71c879dd7defe-chronyd.service-XzjNM9 systemd-private-62580f5e23a84a1bbdf71c879dd7defe-colord.service-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
    
  3. 统计某一目录下每个文件的单词数
    $ 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
    
    
  4. 找到所有符合条件的文件并批量删除
    # '-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’
    
  5. 将文件批量复制到指定的多个目录下
    $ echo ~/RC_CI/auto_testing_CI ~/RC_CI/bug_regression_CI | xargs -n 1 cp -v README.md
    ‘README.md’ -> ‘/home/wlin/RC_CI/auto_testing_CI/README.md’
    ‘README.md’ -> ‘/home/wlin/RC_CI/bug_regression_CI/README.md’
    
  6. 批量重命名文件夹和文件
    $ find /home/wlin/Downloads/ -name phantomjs-icon.png  -type f | xargs -n1 rename -v 'phantomjs-icon.png' 'phantomjs-icon.jpg' 
    `/home/wlin/Downloads/phantomjs-1.9.8/src/phantomjs-icon.png' -> `/home/wlin/Downloads/phantomjs-1.9.8/src/phantomjs-icon.jpg'
    `/home/wlin/Downloads/phantomjs-2.0.0/src/phantomjs-icon.png' -> `/home/wlin/Downloads/phantomjs-2.0.0/src/phantomjs-icon.jpg'
    
  7. 删除目录下除了特定文件外的所有文件
    $ find . -type f -not -name '*gz' -print0 | xargs -0 rm -v 
    removed ‘./errata_services_template/Errata_Makefile’
    removed ‘./errata_services_template/README.md’
    removed ‘./errata_services_template/errata-all-services-in-one-pod.yaml’
    removed ‘./errata_services_template/errata-template.yaml’
    removed ‘./errata_services_template/et-default-staging-settings.configmap’
    removed ‘./errata_services_template/et-default-staging-settings.secret’
    
  8. 遇到特定字符终止执行
    # If the end of file string occurs as a line of input, the rest of the input is ignored.
    $ echo 'aaa ddd bbb ccc' > 1.txt
    [wlin@localhost upshift]$ xargs -E 'ddd'  -a 1.txt echo
    aaa
    
  9. 输出执行的指令
    $ find ~/Downloads -name "1.png" -type f  | xargs -0 -t
    echo /home/wlin/Downloads/P70526-130554.jpg/1.png
     
    /home/wlin/Downloads/P70526-130554.jpg/1.png
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值