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

ghostwu@dev:~/linux/cp$ ls
ghostwu_hardlink ghostwu_home ghostwu_softlink ghostwu.txt
ghostwu@dev:~/linux/cp$ cat ghostwu.txt ghostwu@dev:~/linux/cp$ xargs < ghostwu.txt

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

-n: 指定一行显示几个

ghostwu@dev:~/linux/cp$ xargs -n  < ghostwu.txt 

-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 - 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 -- : home/ghostwu/python/func2.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/func3.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/func4.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/func.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/global.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/global2.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/while.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/for.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/func5.py
-rw-rw-r-- ghostwu/ghostwu -- : home/ghostwu/python/while1.py

最新文章

  1. &lt; meta &gt; 元素
  2. CI(CodeIgniter)框架入门教程——第二课 初始MVC
  3. connect to https://dl-ssl.google.com refused
  4. sql cast()和convert()
  5. 一些常用css技巧的为什么(二)我所理解的line-height
  6. 9款基于HTML5/SVG/Canvas的折线图表应用
  7. Top 10 questions about Java Collections--reference
  8. 精通 Oracle+Python,第 9 部分:Jython 和 IronPython — 在 Python 中使用 JDBC 和 ODP.NET
  9. log4j级别输出
  10. UML学习-时序图
  11. 在Oracle中使用sql获取数据库名称
  12. Java疯狂讲义(四)
  13. 【C#】Deep copy of objects
  14. iOS UIActivityIndicatorView 的使用
  15. 2751: [HAOI2012]容易题(easy)
  16. springboot 入门五-日志一
  17. 基于opencv3.0下的运动车辆识别
  18. EF Core in Action 中文翻译 第一部分导航
  19. Tiny6410 裸机开发--裸机点亮LED
  20. andriod自定义视图

热门文章

  1. “全栈2019”Java多线程第三十三章:await与signal/signalAll
  2. python 通过pytz模块进行时区的转换,获取指定时区的时间
  3. zookeeper的安装与配置(单机和集群)
  4. 利用koa实现mongodb数据库的增删改查
  5. while循环中使用scanf函数
  6. flask开发的CMS管理系统
  7. [Umbraco] 创建第一个页面
  8. 全网最详细的Hadoop HA集群启动后,两个namenode都是standby的解决办法(图文详解)
  9. 使用node和express+mongodb实现数据增删改功能
  10. postgresql逻辑结构--表空间(四)