Linux常用基本命令(xargs )
xargs:能够将管道或者标准输入传递的数据转换成xargs命令后面跟随的参数
ghostwu@dev:~/linux/cp$ ls ghostwu_hardlink ghostwu_home ghostwu_softlink ghostwu.txt ghostwu@dev:~/linux/cp$ cat ghostwu.txt 1 2 3 4 5 6 7 8 9 10 ghostwu@dev:~/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: 指定一行显示几个
ghostwu@dev:~/linux/cp$ xargs -n 2 < ghostwu.txt 1 2 3 4 5 6 7 8 9 10
-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 -0 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 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

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
解决Linux不活动用户登录超时后自动登出
在平常的工作中,windows系统使用的比较多一些,身边的很多同事都会对自己的电脑进行一些个性化设置,比如离开一段时间后自动锁屏、自动关闭屏幕、自动注销当前登录等。在windows下可以这样操作,那么在Linux下是否也可以这样操作呢?经过一段时间的摸索,本人发现了2中方法来实现Linux下不活动用户在登录一段时间后自动登出操作,分享给大家。 方法一、通过修改.bashrc或.bash_profile文件来实现 通过修改home目录下的.bashrc或.bash_profile文件来实现。这两个文件选择其中一个在末尾加入如下一行,具体操作如下: [GeekDevOps@GeekDevOps /]$ cd ~ [GeekDevOps@GeekDevOps ~]$ echo "TMOUT=90">>.bashrc [GeekDevOps@GeekDevOps ~]$ source .bashrc 以上代码中我们选择的是.bashrc文件,如果您选择的是.bash_profile,那么也是一样的操作流程。以上我们设置用户自动登出时间为90秒,90秒后屏幕显示如下: [Gee...
- 下一篇
Linux下LNMP web服务的安装
第1章 安装Nginx 1.1 Nginx官网 nginx news http://nginx.org/ 1.2 安装nginx 1.2.1 安装Nginx所需的pcre库 作用:实现伪静态的功能 yum install pcre pcre-devel -y 1.2.2 安装编译依赖包: yum install gcc gcc-devel -y yum install openssl openssl-devel -y 1.2.3 下载源码包: wget -q http://nginx.org/download/nginx-1.6.3.tar.gz 参数:-q 下载不提示。 1.2.4 解压 tar xf nginx-1.6.3.tar.gz cd nginx-1.6.3 1.2.5 查询yum仓库有没有rpm包 yum list |grep nginx 或yum list *nginx* 1.2.6 添加系统用户: useradd www -s /sbin/nologin -M 1.2.7 开始编译安装nginx 1.2.7.1 配置编译参数 ./configure --u...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Hadoop3单机部署,实现最简伪集群
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果