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. jQuery基础(2)

    一.jQuery的属性操作 jQuery的属性操作分为四部分: html标签属性操作:是对html文档中的标签属性进行读取,设置和移除操作.比如attr().removeAttr(): DOM属性操作 ...

  2. spring cloud学习(六) 配置中心-自动更新

    上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...

  3. javascript 模块化学习:Class打包出来无法实例化问题

    菜鸟初学前端,第一次尝试使用webpack, 目的是做一个lib,想用webpack + babel 自动化打包出来 es5 的js代码 模块中用到了Class,直接export{className} ...

  4. zookeeper的命令使用

    这篇是接着上篇zookeeper集群做的,所以有不熟悉的可以返回看下zookeeper集群的相关内容. 这里是相关的命名行使用方法: 基本命令用法 连接server zkCli.sh -server ...

  5. webApp开发中的总结

    meta标签:  H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...

  6. Angular使用总结 --- 搜索场景中使用rxjs的操作符

    在有input输入框的搜索/过滤业务中,总会考虑如何减少发起请求频率,尽量使每次的请求都是有效的.节流和防抖是比较常见的做法,这类函数的实现方式也不难,不过终归还是需要自己封装.rxjs提供了各种操作 ...

  7. job任务执行流程与分区机制

    job任务执行流程    1.run job阶段        ①收集整个job的环境信息(比如通过conf设定的参数,还有mapperClass,reducerClass,以及输出kv类型)     ...

  8. Python使用动态的变量名

    当我们在使用Python处理一些重复性很高的事情时,有时候需要很多的变量来存放一些暂行性的数据,由于这些变量的数量很大,所以这使我们就会想到能不能使用循环来像生成数据值一样生成变量名呢,当然是可以的 ...

  9. 【xsy2194】Philosopher set+线段树合并

    题目大意:给你一个长度为$n$的序列,有$m$次操作,每次操作是以下两种之一: 对某个区间内的数按照升序/降序排序,询问某个区间内数的积在十进制下首位数字是多少. 数据范围:$n,m≤2\times ...

  10. ajaxsubmit 上传文件 在IE中返回的内容 提示下载文件

    在ajaxSubmit提交表单的时候,如果表单内有文件上传的话,会判断参数是否配置的iframe为false参数,如果没有,会用创建隐藏iframe方式提交表单,如果设定了iframe为false,则 ...