一、直观感受

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. Writing DynamicTableEntity to Azure Storage Table

    There are ample of samples available to show how to insert an object/entity to Azure Storage Table. ...

  2. [Full-stack] 快速上手开发 - React

    故事背景 [1] 博客笔记结合<React快速上手开发>再次系统地.全面地走一遍. [2] React JS Tutorials:包含了JS --> React --> Red ...

  3. word中替换【换行符】与【回车符】

  4. Web 文件上传 目录

    0. SpringMVC -- 梗概--源码--贰--上传 1. Web上传文件的原理及实现 2. Web文件上传方法总结大全 3. SpringMVC 文件上传配置,多文件上传,使用的Multipa ...

  5. akka pubsub example

    测了一个小时的 Pubsub 模式,发现这个模式和自己预期的不太一样,具体表现在: 1. 当 subscriber 订阅了某个 topic 并附带 groupName 时,如果 publish 发布的 ...

  6. ASP.NET MVC与ASP.NET Web Form简单区别

    概论: Asp.net  微软 提供web开发框架或者技术.分Web Form和ASP.NET MVC.下面简单说明各自优缺点及使用场景. Web Form 优点: 1.支持丰富的服务器控件.如:Gr ...

  7. case when then end

    当 a>b获取a,否则获取b,当a>c获取a,否则获取c,b大于c获取b否则获取c SELECT id,(CASE  WHEN a>b THEN a WHEN  a>c THE ...

  8. Servlet3.0 multipart 文件上传技术

    Servlet3.0 javaConfig配置 传统的servlet都是在web.xml中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,允许使用代 ...

  9. scala 可变集合与内存清理的关系

    留坑待填 使用scala.collection.mutable._期间,发现了当程序运行内存开销较多时,使用系统工具进行内存清理,然后程序报出了变量找不到.内存无法访问.数组访问越界,堆栈溢出等多种错 ...

  10. FastJson 自定义Serialize、Parser

    FastJson 自定义Serialize.Parser 今天在处理Json反序列化时,在C#传过来的JSON字符串中枚举类型为int类型,FastJson对于枚举的处理有两种类型,一种是字符串一种是 ...