• Linux常用基本命令(xargs )


    xargs:能够将管道或者标准输入传递的数据转换成xargs命令后面跟随的参数

    ghostwu@dev:~/linux/cp$ ls
    ghostwu_hardlink  ghostwu_home  ghostwu_softlink  ghostwu.txt
    ghostwu@dev:~/linux/cp$ cat ghostwu.txt 
    1 2 3
    4 5 6 7
    8 9 10
    ghostwu@dev:~/linux/cp$ xargs < ghostwu.txt 
    1 2 3 4 5 6 7 8 9 10

    把ghostwu.txt文件的内容 通过输入重定向 传递给xargs,相当于echo 1 2 3 4 5 6 7 8 9 10

    -n: 指定一行显示几个

    ghostwu@dev:~/linux/cp$ xargs -n 2 < ghostwu.txt 
    1 2
    3 4
    5 6
    7 8
    9 10

    -d:指定分隔符

    ghostwu@dev:~/linux/cp$ echo a-b-c-d
    a-b-c-d
    ghostwu@dev:~/linux/cp$ echo a-b-c-d | xargs -d -
    a b c d

    -i: 以{}替代前面的结果,经常跟find命令结合使用

    把家目录下.py结尾的文件 复制 到tmp目录下

    ghostwu@dev:~$ ls
    Desktop    examples.desktop  linux  Pictures  python     tmp
    Documents  git_test          Music  project   software   Videos
    Downloads  info              php    Public    Templates
    ghostwu@dev:~$ ls tmp
    ghostwu@dev:~$ find . -name "*.py" | xargs -i cp -a {} tmp/
    ghostwu@dev:~$ ls tmp
    for.py    func3.py  func5.py  global2.py  while1.py
    func2.py  func4.py  func.py   global.py   while.py

    删除tmp下以.py结尾的文件

    ghostwu@dev:~$ ls tmp
    for.py    func3.py  func5.py  global2.py  while1.py
    func2.py  func4.py  func.py   global.py   while.py
    ghostwu@dev:~$ touch tmp/{a..f}.txt
    ghostwu@dev:~$ ls tmp
    a.txt  c.txt  e.txt   f.txt     func3.py  func5.py  global2.py  while1.py
    b.txt  d.txt  for.py  func2.py  func4.py  func.py   global.py   while.py
    ghostwu@dev:~$ find ./tmp -name "*.py" | xargs -i rm -rf {}
    ghostwu@dev:~$ ls tmp
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt

    创建带空格的文件

    ghostwu@dev:~/tmp$ touch "hello ghostwu.txt"
    ghostwu@dev:~/tmp$ ls
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  hello ghostwu.txt
    ghostwu@dev:~/tmp$ touch hi ghostwu.txt
    ghostwu@dev:~/tmp$ ls
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  hello ghostwu.txt  hi ghostwu.txt
    ghostwu@dev:~/tmp$ rm hello ghostwu.txt 
    ghostwu@dev:~/tmp$ ls
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  hi ghostwu.txt
    ghostwu@dev:~/tmp$ rm hi ghostwu.txt 
    ghostwu@dev:~/tmp$ ls
    a.txt  b.txt  c.txt  d.txt  e.txt  f.txt

    不能删除带有空格的文件

    ghostwu@dev:~/tmp$ ls
    alltxt.tar.gz  b.txt  d.txt  f.txt              hi ghostwu.txt
    a.txt          c.txt  e.txt  hello ghostwu.txt
    ghostwu@dev:~/tmp$ find . -name "*.txt" | xargs rm
    rm: cannot remove './hello': No such file or directory
    rm: cannot remove 'ghostwu.txt': No such file or directory
    rm: cannot remove './hi': No such file or directory
    rm: cannot remove 'ghostwu.txt': No such file or directory
    ghostwu@dev:~/tmp$ ls
    alltxt.tar.gz  hello ghostwu.txt  hi ghostwu.txt

    用man手册查找find 中 -print0选项

    -print0
    True; print the full file name on the standard output, followed
    by a null character (instead of the newline character that
    -print uses). This allows file names that contain newlines or
    other types of white space to be correctly interpreted by pro‐
    grams that process the find output. This option corresponds to
    the -0 option of xargs.

    意思大概就是 find 命令的-print0选项启用之后,会把文件名中包含换行或者其他空白字符正确的解释出来。这个选项通常跟 xargs 的 -0选项结合使用

    所以,可以向下面这种方式,执行xargs 

    ghostwu@dev:~/tmp$ ls
    alltxt.tar.gz  b.txt  d.txt  f.txt              hi ghostwu.txt
    a.txt          c.txt  e.txt  hello ghostwu.txt
    ghostwu@dev:~/tmp$ find . -name "*.txt" -print0 | xargs -0 rm
    ghostwu@dev:~/tmp$ ls
    alltxt.tar.gz

    查找家目录下面所有的.py文件,然后打包

    ghostwu@dev:~/tmp$ ls
    ghostwu@dev:~/tmp$ find ~ -name "*.py" | xargs tar cvf allpy.tar.gz
    tar: Removing leading `/' from member names
    /home/ghostwu/python/func2.py
    /home/ghostwu/python/func3.py
    /home/ghostwu/python/func4.py
    /home/ghostwu/python/func.py
    /home/ghostwu/python/global.py
    /home/ghostwu/python/global2.py
    /home/ghostwu/python/while.py
    /home/ghostwu/python/for.py
    /home/ghostwu/python/func5.py
    /home/ghostwu/python/while1.py
    ghostwu@dev:~/tmp$ ls
    allpy.tar.gz
    ghostwu@dev:~/tmp$ tar -tvf allpy.tar.gz 
    -rw-rw-r-- ghostwu/ghostwu 179 2018-03-18 21:29 home/ghostwu/python/func2.py
    -rw-rw-r-- ghostwu/ghostwu  58 2018-03-18 21:31 home/ghostwu/python/func3.py
    -rw-rw-r-- ghostwu/ghostwu  81 2018-03-18 21:33 home/ghostwu/python/func4.py
    -rw-rw-r-- ghostwu/ghostwu  44 2018-03-18 21:26 home/ghostwu/python/func.py
    -rw-rw-r-- ghostwu/ghostwu 150 2018-03-18 21:53 home/ghostwu/python/global.py
    -rw-rw-r-- ghostwu/ghostwu 124 2018-03-18 21:55 home/ghostwu/python/global2.py
    -rw-rw-r-- ghostwu/ghostwu  90 2018-03-18 21:19 home/ghostwu/python/while.py
    -rw-rw-r-- ghostwu/ghostwu  82 2018-03-18 21:08 home/ghostwu/python/for.py
    -rw-rw-r-- ghostwu/ghostwu  99 2018-03-18 21:48 home/ghostwu/python/func5.py
    -rw-rw-r-- ghostwu/ghostwu  92 2018-03-18 21:23 home/ghostwu/python/while1.py
  • 相关阅读:
    MFC绘图机制(二)-双缓存
    C89:论数组/指针/引用
    C89:论预处理命令
    图像优化大坑
    MFC 对话框和属性表
    jQuery-plugin-pagePiling
    jquery-ui-chosen
    JavaScript DOM编程艺术小笔记
    微信公众号素材
    iOS沙箱传值
  • 原文地址:https://www.cnblogs.com/ghostwu/p/9000147.html
Copyright © 2020-2023  润新知