一、直观感受

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

-print

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

find

xargs与管道的区别的更多相关文章

  1. [转帖]xargs命令详解,xargs与管道的区别

    xargs命令详解,xargs与管道的区别 https://www.cnblogs.com/wangqiguo/p/6464234.html 之前一直说要学习一下 xargs 到现在为止也没学习.. ...

  2. xargs命令详解,xargs与管道的区别

    为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...

  3. 【转】xargs命令详解,xargs与管道的区别

    为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...

  4. [转]xargs命令详解,xargs与管道的区别

    为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...

  5. Linux命令:xargs命令详解,xargs与管道的区别

    阅读目录 为什么要用xargs,问题的来源 xargs是什么,与管道有什么不同 xargs的一些有用的选项 回到顶部 为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人 ...

  6. Linux下xargs命令详解及xargs与管道的区别

    在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以及与管道的区别.为什么要用xarg ...

  7. linux下xargs和管道的区别

    管道将前面的标准输出作为后面的标准输入,xargs则将标准输入作为命令的参数 一.简介 1.背景 之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了x ...

  8. xargs、管道、exec区别

    作者:ilexwg链接:https://www.zhihu.com/question/27452459/answer/170834758来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...

  9. find中的-print0和xargs中-0的区别

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...

随机推荐

  1. Ubuntu 中的VI和vim

    转载出处:http://blog.csdn.net/xiajun07061225/article/details/7039413 或功能键[Home]:移动到这一行的最前面字符处. $或功能键[End ...

  2. Mysql系列七:分库分表技术难题之分布式全局唯一id解决方案

    一.前言 在前面的文章Mysql系列四:数据库分库分表基础理论中,已经说过分库分表需要应对的技术难题有如下几个: 1. 分布式全局唯一id 2. 分片规则和策略 3. 跨分片技术问题 4. 跨分片事物 ...

  3. linux安装中文输入法

    CentOS英文系统安装中文输入法,简单说说在CentOS 6.3下用yum安装中文输入法的过程.   1.需要root权限,所以要用root登录 ,或su root 2.yum install &q ...

  4. jxl和POI的区别

    最近两个项目中分别用到jxl和POI,因为用的都是其中的简单的功能,所以没有觉得这其中有太大的区别.有人针对他们做了比较,这里也拿出来展示一下. 首先从优缺点上来说 一.jxl 优点: Jxl对中文支 ...

  5. CALayer 知识:创建带阴影效果的圆角图片图层和创建自定义绘画内容图层

    效果如下: KMLayerDelegate.h #import <UIKit/UIKit.h> @interface KMLayerDelegate : NSObject @end KML ...

  6. [Laravel] 05 - Controller

    大纲 PHP章节貌似有类似功能,回头看下. 请求 URL 一.URL 处理方法 请求使用的是:symfony/http-foundation组件 $_GET, $_POST, $_COOKIE, $_ ...

  7. WCF ChannelFactory<T> WCF Channel and ChannelFactory Caching

    https://stackoverflow.com/questions/3200197/creating-wcf-channelfactoryt?rq=1 https://stackoverflow. ...

  8. 获取HttpServletRequest请求Body中的内容

    在实际开发过程中,经常需要从 HttpServletRequest 中读取HTTP请求的body内容,俗话说的好”好记性不如烂笔头“,特在此将其读取方法记录一下. import java.io.Buf ...

  9. C# 数组转json

    ////////试题信息///////////////// // 已知 // h.d两个数组分别表示表头和内容 // 要求 // 编写函数一:rs2Array // 返回一个数组,每一项为一个对象. ...

  10. Pyramid Analytics宣布无缝集成BI Office和微软Power BI Desktop

    全球领先的企业商业智能(BI)软件提供商Pyramid Analytics与微软联手,凭借完善的分析平台BI Office强化Power BI Desktop的个人生产力功能.新的“Publish t ...