xargs与管道的区别
一、直观感受
echo '--help' | cat
echo的输出通过管道定向到cat的输入, 然后cat从其标准输入中读取待处理的文本内容, 输出结果:
--help
echo '--help' | xargs cat
等价于 cat --help, xargs将其接受的字符串 --help 变成cat的参数来运行, 输出结果:
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output. -A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit With no FILE, or when FILE is -, read standard input. Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
像 cat与grep这些文字处理工具能标准输入中读取待处理的内容,但像kill , rm 这些程序如果命令行参数中没有指定要处理的内容则不会默认从标准输入中读取
有时候需要 ps -ef | grep 'ddd' | kill 这样的效果,筛选出符合某条件的进程pid然后结束。那么应该怎样达到这样的效果呢。有几个解决办法:
. 通过 kill `ps -ef | grep 'ddd'`
#等同于拼接字符串得到的命令,其效果类似于 kill $pid . for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill - $procid; done
#其实与第一种原理一样,只不过需要多次kill的时候是循环处理的,每次处理一个 . ps -ef | grep 'ddd' | xargs kill
#xargs命令可以通过管道接受字符串,作为后面命令的命令行参数
xargs的默认命令是echo,空格是默认定界符。
二、xargs命令用法
xargs可以做单行和多行之间的转换
cat test.txt a b c d e f g h i j k l m n o p q r #多行输入单行输出: cat test.txt | xargs a b c d e f g h i j k l m n o p q r #-n 指定每行输出几项: cat test.txt | xargs -n5 a b c d e f g h i j k l m n o p q r #-d指定定界符: echo "nameXnameXnameXname" | xargs -dX name name name name #d、n 结合使用: echo "nameXnameXnameXname" | xargs -dX -n2 name name name name
-t 表示先打印命令,然后再执行
-i 或者是-I,将xargs的每项名称,一般是一行一行赋值给{}
$ ls
.txt .txt $ ls *.txt |xargs -t -i mv {} {}.bak
mv .txt .txt.bak
mv .txt .txt.bak $ ls
.txt.bak .txt.bak
更多示例
man find
This primary always evaluates to true. It prints the pathname of the current file to standard output. If none of -exec, -ls, -print, -print0, or -ok is
specified, the given expression shall be effectively replaced by ( given expression ) -print.
-print0
This primary always evaluates to true. It prints the pathname of the current file to standard output, followed by an ASCII NUL character (character code0).
-type t
True if the file is of the specified type. Possible file types are as follows:
b block special
c character special
d directory
f regular file
l symbolic link
p FIFO
s socket
#复制所有图片文件到 /data/images 目录下:
ls *.jpg | xargs -n1 -I cp {} /data/images #xargs结合find使用 用rm 删除太多的文件时候,可能得到一个错误信息:/bin/rm Argument list too long. 用xargs去避免这个问题:
find . -type f -name "*.log" -print0 | xargs - rm -f #xargs -0将\0作为定界符。 统计一个源代码目录中所有php文件的行数:
find . -type f -name "*.php" -print0 | xargs - wc -l #查找所有的jpg 文件,并且压缩它们:
find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz #假如你有一个文件包含了很多你希望下载的URL,你能够使用xargs下载所有链接:
cat url-list.txt | xargs wget -c
出处:
xargs与管道的区别的更多相关文章
- [转帖]xargs命令详解,xargs与管道的区别
xargs命令详解,xargs与管道的区别 https://www.cnblogs.com/wangqiguo/p/6464234.html 之前一直说要学习一下 xargs 到现在为止也没学习.. ...
- xargs命令详解,xargs与管道的区别
为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...
- 【转】xargs命令详解,xargs与管道的区别
为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...
- [转]xargs命令详解,xargs与管道的区别
为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...
- Linux命令:xargs命令详解,xargs与管道的区别
阅读目录 为什么要用xargs,问题的来源 xargs是什么,与管道有什么不同 xargs的一些有用的选项 回到顶部 为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人 ...
- Linux下xargs命令详解及xargs与管道的区别
在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以及与管道的区别.为什么要用xarg ...
- linux下xargs和管道的区别
管道将前面的标准输出作为后面的标准输入,xargs则将标准输入作为命令的参数 一.简介 1.背景 之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了x ...
- xargs、管道、exec区别
作者:ilexwg链接:https://www.zhihu.com/question/27452459/answer/170834758来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- find中的-print0和xargs中-0的区别
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...
随机推荐
- Kiss MySQL goodbye for development and say hello to HSQLDB
The days of using MySQL, DB2, PostgreSQL etc for development is over.. I don’t know why any program ...
- Memcached 总结 启动多个Memcached服务 配置文件详解
一. 1.解压下载的安装包到指定目录. 2.服务安装,使用管理员权限运行以下命令: c:\memcached\memcached.exe -d install 二.同一台Windows机器中启动多个M ...
- springcloud-04-自定义ribbon的配置方式
在dubbo项目中, zookeeper即注册中心帮我们实现了调度和负载均衡的能力, 这种方式被称为服务器端的负载均衡, springcloud中, 使用ribben实现的客户端负载均衡 什么是rib ...
- Writing DynamicTableEntity to Azure Storage Table
There are ample of samples available to show how to insert an object/entity to Azure Storage Table. ...
- android评分条RatingBar自定义设置
RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style.首先是布局文件: 其中android:numStars="5"设置 ...
- redis mongodb mysql 三大数据库的更简单的批量操作。批量任务自动聚合器。
1.redis mongodb mysql的python包都提供了批量插入操作,但需要自己在外部对一个例如1000 001个任务进行分解成每1000个为1个小批次,还要处理整除批次数量后的余数,如果做 ...
- [IR] Concept Search and LSI
基于术语关系的贝叶斯网络信息检索模型扩展研究 LSI 阅读笔记 背景知识 提出一种改进的共现频率法,利用该方法挖掘了索引术语之间的相关关系,将这种相关关系引入信念网络模型,提出了一个具有两层术语节点的 ...
- M0 M4关于库函数的讲解(以时钟为例)
#define CLK_PWRCON_PD_WAIT_CPU_Pos 8 #define CLK_PWRCON_PD_WAIT_CPU_Msk (1ul << CLK_PWRCON_PD_ ...
- Qt编写调试日志输出类带网络转发(开源)
用qt开发商业程序已经九年了,陆陆续续开发过至少几十个程序,除了一些算不算项目的小工具外,大部分的程序都需要有个日志的输出功能,希望可以将程序的运行状态存储到文本文件或者数据库或者做其他处理等,qt对 ...
- 官方文档:Office VBA 参考
https://docs.microsoft.com/zh-CN/office/vba/api/overview/ Office VBA 参考 https://docs.microsoft.co ...