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

Linux常用基本命令(xargs )的更多相关文章

  1. Linux常用基本命令(less)

    转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...

  2. Linux 常用基本命令及应用技巧

    需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...

  3. linux常用基本命令

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...

  4. 【Linux】linux常用基本命令(转)

    (转自:http://blog.csdn.net/xiaoguaihai/article/details/8705992) Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用 ...

  5. 【Linux】linux常用基本命令

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日 ...

  6. linux常用基本命令整理小结

    linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...

  7. Linux常用基本命令(file,chown)

    1,file命令作用,查看文件的类型 ghostwu@dev:~$ .htm ./linux/rename ghostwu@dev:~$ .htm ./linux/rename/.htm: empty ...

  8. Linux 常用基本命令

    这两天有俩哥们问了我linux的事,问我在工作中需不需要用到,需不需要学会 一个是工作1年不到的,我跟他说,建议你学学,在以后肯定是要用到的,虽然用到的机会不多,但是会总比不会好 另一个是工作6年的, ...

  9. Linux常用基本命令:三剑客命令之-awk内置函数用法

    awk的内置函数大致可以分类为算数函数.字符串函数.时间函数.其他函数等 算数函数 最常用的算数函数有rand函数.srand函数.int函数. 可以使用rand函数生成随机数,但是使用rand函数时 ...

随机推荐

  1. CE修改器使用教程 [基础篇]

    Cheat Engine  是一款内存修改编辑工具 ,它允许你修改你的游戏或软件内存数据,以得到一些其他功能.它包括16进制编辑,反汇编程序,内存查找工具.与同类修改工具相比,它具有强大的反汇编功能, ...

  2. intellij 引入本地库并war打包

    一.引入本地库 1.File -> Project Structure -> Libraries,点击+,新增本地lib库. 2.File -> Project Structure ...

  3. redis ERR This instance has cluster support disabled

    Redis  集群的时候报错: redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster sup ...

  4. 【May Be DNK】JSON.parse() and JSON.stringify()的两个实用技巧

    结论 一.数据深拷贝 使用方法:JSON.parse(JSON.stringify(param)) let o = {a: 1, b: 2} let o1 = JSON.parse(JSON.stri ...

  5. mybatis四大接口之 ResultSetHandler

    1. 继承结构 2. ResultSetHandler public interface ResultSetHandler { // 将Statement执行后产生的结果集(可能有多个结果集)映射为结 ...

  6. Redis---quickList(快速列表)

    1. 概述 考虑到链表的附加空间相对太高,prev 和 next 指针就要占去 16 个字节 (64bit 系统的指针是 8 个字节),另外每个节点的内存都是单独分配,会加剧内存的碎片化,影响内存管理 ...

  7. python的datetime常用方法

    把datetime转成字符串 datetime.strftime("%Y-%m-%d-%H") 把字符串转成datetime datetime.strptime(datetime, ...

  8. 【转】谷歌三大核心技术(一)The Google File System中文版

      The Google File System中文版 译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的.可伸缩的分布式文件系统.GFS虽然运行在廉价 ...

  9. Liferay7.0与cas单点登录配置

    1.简介     Liferay7.0支持多种登录方式,包括:常规的.opensso.cas.ntlm.ldap.openid.Facebook.Google等. 其中, (1) 常规:则是默认Lif ...

  10. 杂谈:Windows操作系统的介绍与对Win8操作系统市场反响冷淡原因的分析

    Windows操作系统,毫无疑问是操作系统市场上的霸主,也正因为Windows操作系统的诞生让电脑的操作性能变得更加平民化,深的用户的喜爱.至今身边的人也是选择windows操作系统的居多,这篇文章也 ...