Linux常用基本命令(xargs )
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 )的更多相关文章
- Linux常用基本命令(less)
转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...
- Linux 常用基本命令及应用技巧
需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...
- linux常用基本命令
Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...
- 【Linux】linux常用基本命令(转)
(转自:http://blog.csdn.net/xiaoguaihai/article/details/8705992) Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用 ...
- 【Linux】linux常用基本命令
Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日 ...
- linux常用基本命令整理小结
linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...
- Linux常用基本命令(file,chown)
1,file命令作用,查看文件的类型 ghostwu@dev:~$ .htm ./linux/rename ghostwu@dev:~$ .htm ./linux/rename/.htm: empty ...
- Linux 常用基本命令
这两天有俩哥们问了我linux的事,问我在工作中需不需要用到,需不需要学会 一个是工作1年不到的,我跟他说,建议你学学,在以后肯定是要用到的,虽然用到的机会不多,但是会总比不会好 另一个是工作6年的, ...
- Linux常用基本命令:三剑客命令之-awk内置函数用法
awk的内置函数大致可以分类为算数函数.字符串函数.时间函数.其他函数等 算数函数 最常用的算数函数有rand函数.srand函数.int函数. 可以使用rand函数生成随机数,但是使用rand函数时 ...
随机推荐
- PICE(2):JDBCStreaming - gRPC-JDBC Service
在一个akka-cluster环境里,从数据调用的角度上,JDBC数据库与集群中其它节点是脱离的.这是因为JDBC数据库不是分布式的,不具备节点位置透明化特性.所以,JDBC数据库服务器必须通过服务方 ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- day 38 jq 入门 学习(一)
前情提要: jq是简化版本的js 可以把很多很复杂的js 提炼让前端代码更好写 一:jq的使用 <!DOCTYPE html> <html lang="en"&g ...
- fastjson 反序列化漏洞笔记,比较乱
现在思路还是有点乱,希望后面能重新写 先上pon.xml 包 <?xml version="1.0" encoding="UTF-8"?> < ...
- [原创]K8 DNN密码解密工具(DotNetNuke Password Decrypt)
工具: K8_DNN_Password_Decrypt编译: VS2012 C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http: ...
- Vue2.5开发去哪儿网App 城市列表开发
一,城市选择页面路由配置 ...
- macbook 安装oracle RAC
http://blog.itpub.net/29047826/viewspace-1268923/ http://blog.itpub.net/24930246/viewspace-1426856/
- [Error] 'exit' was not declared in this scope的解决方法
新手刚开始用Linux和c++写程序,可能会出现下面的错误 error: ‘exit’ was not declared in this scope 解决方法是 添加 #include <cst ...
- Hadoop(三)搭建Hadoop全分布式集群
原文地址:http://www.cnblogs.com/zhangyinhua/p/7652686.html 阅读目录(Content) 一.搭建Hadoop全分布式集群前提 1.1.网络 1.2.安 ...
- Ehcache的视频-如何用Ehcache提升你的Java应用性能
Java应用最广的缓存(分布式缓存)Ehcache的Youtube介绍视频 (需-翻-墙)