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函数时 ...
随机推荐
- JQuery Mobile - 解决页面点击时候,页眉和页脚消失问题!
当点击页面时候,页眉和页脚会消失!解决方法,在页面和页脚中加入: data-quicklinks="true" 实际使用代码: <div data-role="pa ...
- “全栈2019”Java多线程第三十二章:显式锁Lock等待唤醒机制详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第二十八章:公平锁与非公平锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Python运维脚本整理
Python 实现的自动化服务器管理 import sys import os import paramiko ssh = paramiko.SSHClient() ssh.set_missing_h ...
- maven build的常用生命周期
常用的maven build goals: validate - validate the project is correct and all necessary information is av ...
- [LeetCode]640解方程式
问题描述: 示例 1: 输入: "x+5-3+x=6+x-2" 输出: "x=2" 示例 2: 输入: "x=x" 输出: "In ...
- [JavaWeb] Servlet Filter
作用: Servlet 过滤器可以动态地拦截请求和响应,以变换或使用包含在请求或响应中的信息. 可以将一个或多个 Servlet 过滤器附加到一个 Servlet 或一组 Servlet.Servle ...
- 解释一下核主成分分析(Kernel Principal Component Analysis, KPCA)的公式推导过程(转载)
KPCA,中文名称”核主成分分析“,是对PCA算法的非线性扩展,言外之意,PCA是线性的,其对于非线性数据往往显得无能为力,例如,不同人之间的人脸图像,肯定存在非线性关系,自己做的基于ORL数据集的实 ...
- 《CEO说 像企业家一样思考》读书笔记
伟大的CEO和街头小贩一样都有共同的思维方式,他们总是能够通过复杂的表象看到商业本质,化繁为简,抓住企业经营的根本要素:商业智慧.所谓商业智慧,即企业家最应关注的企业运营的六个关键要素:现金净流入.利 ...
- AJAX(Jquery)
一)jQuery常用AJAX-API 目的:简化客户端与服务端进行局部刷新的异步通讯 (1)取得服务端当前时间 简单形式:jQuery对象.load(url) 返回结果自动添加到jQuery对象代表的 ...