Linux常用基本命令(xargs )
Posted ghostwu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux常用基本命令(xargs )相关的知识,希望对你有一定的参考价值。
xargs:能够将管道或者标准输入传递的数据转换成xargs命令后面跟随的参数
[email protected]:~/linux/cp$ ls ghostwu_hardlink ghostwu_home ghostwu_softlink ghostwu.txt [email protected]:~/linux/cp$ cat ghostwu.txt 1 2 3 4 5 6 7 8 9 10 [email protected]:~/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: 指定一行显示几个
[email protected]:~/linux/cp$ xargs -n 2 < ghostwu.txt 1 2 3 4 5 6 7 8 9 10
-d:指定分隔符
[email protected]:~/linux/cp$ echo a-b-c-d a-b-c-d [email protected]:~/linux/cp$ echo a-b-c-d | xargs -d - a b c d
-i: 以{}替代前面的结果,经常跟find命令结合使用
把家目录下.py结尾的文件 复制 到tmp目录下
[email protected]:~$ ls Desktop examples.desktop linux Pictures python tmp Documents git_test Music project software Videos Downloads info php Public Templates [email protected]:~$ ls tmp [email protected]:~$ find . -name "*.py" | xargs -i cp -a {} tmp/ [email protected]:~$ ls tmp for.py func3.py func5.py global2.py while1.py func2.py func4.py func.py global.py while.py
删除tmp下以.py结尾的文件
[email protected]:~$ ls tmp for.py func3.py func5.py global2.py while1.py func2.py func4.py func.py global.py while.py [email protected]:~$ touch tmp/{a..f}.txt [email protected]:~$ 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 [email protected]:~$ find ./tmp -name "*.py" | xargs -i rm -rf {} [email protected]:~$ ls tmp a.txt b.txt c.txt d.txt e.txt f.txt
创建带空格的文件
[email protected]:~/tmp$ touch "hello ghostwu.txt" [email protected]:~/tmp$ ls a.txt b.txt c.txt d.txt e.txt f.txt hello ghostwu.txt [email protected]:~/tmp$ touch hi\ ghostwu.txt [email protected]:~/tmp$ ls a.txt b.txt c.txt d.txt e.txt f.txt hello ghostwu.txt hi ghostwu.txt [email protected]:~/tmp$ rm hello\ ghostwu.txt [email protected]:~/tmp$ ls a.txt b.txt c.txt d.txt e.txt f.txt hi ghostwu.txt [email protected]:~/tmp$ rm hi\ ghostwu.txt [email protected]:~/tmp$ ls a.txt b.txt c.txt d.txt e.txt f.txt
不能删除带有空格的文件
[email protected]:~/tmp$ ls alltxt.tar.gz b.txt d.txt f.txt hi ghostwu.txt a.txt c.txt e.txt hello ghostwu.txt [email protected]:~/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 [email protected]:~/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
[email protected]:~/tmp$ ls alltxt.tar.gz b.txt d.txt f.txt hi ghostwu.txt a.txt c.txt e.txt hello ghostwu.txt [email protected]:~/tmp$ find . -name "*.txt" -print0 | xargs -0 rm [email protected]:~/tmp$ ls alltxt.tar.gz
查找家目录下面所有的.py文件,然后打包
[email protected]:~/tmp$ ls [email protected]:~/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 [email protected]:~/tmp$ ls allpy.tar.gz [email protected]:~/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
以上是关于Linux常用基本命令(xargs )的主要内容,如果未能解决你的问题,请参考以下文章