一、直观感受

echo '--help' | cat

echo的输出通过管道定向到cat的输入, 然后cat从其标准输入中读取待处理的文本内容, 输出结果:

  1. --help

echo '--help' | xargs cat

等价于 cat --help,  xargs将其接受的字符串 --help 变成cat的参数来运行, 输出结果:

  1. Usage: cat [OPTION]... [FILE]...
  2. Concatenate FILE(s), or standard input, to standard output.
  3.  
  4. -A, --show-all equivalent to -vET
  5. -b, --number-nonblank number nonempty output lines
  6. -e equivalent to -vE
  7. -E, --show-ends display $ at end of each line
  8. -n, --number number all output lines
  9. -s, --squeeze-blank suppress repeated empty output lines
  10. -t equivalent to -vT
  11. -T, --show-tabs display TAB characters as ^I
  12. -u (ignored)
  13. -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
  14. --help display this help and exit
  15. --version output version information and exit
  16.  
  17. With no FILE, or when FILE is -, read standard input.
  18.  
  19. Examples:
  20. cat f - g Output f's contents, then standard input, then g's contents.
  21. cat Copy standard input to standard output.

像 cat与grep这些文字处理工具能标准输入中读取待处理的内容,但像kill , rm 这些程序如果命令行参数中没有指定要处理的内容则不会默认从标准输入中读取

有时候需要 ps -ef | grep 'ddd' | kill 这样的效果,筛选出符合某条件的进程pid然后结束。那么应该怎样达到这样的效果呢。有几个解决办法:

  1. . 通过 kill `ps -ef | grep 'ddd'`
  2. #等同于拼接字符串得到的命令,其效果类似于 kill $pid
  3.  
  4. . for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill - $procid; done
  5. #其实与第一种原理一样,只不过需要多次kill的时候是循环处理的,每次处理一个
  6.  
  7. . ps -ef | grep 'ddd' | xargs kill
  8. #xargs命令可以通过管道接受字符串,作为后面命令的命令行参数

xargs的默认命令是echo,空格是默认定界符。

二、xargs命令用法

xargs可以做单行和多行之间的转换

  1. cat test.txt
  2.  
  3. a b c d e f g
  4.  
  5. h i j k l
  6.  
  7. m n o p q r
  8.  
  9. #多行输入单行输出:
  10.  
  11. cat test.txt | xargs
  12.  
  13. a b c d e f g h i j k l m n o p q r
  14.  
  15. #-n 指定每行输出几项:
  16.  
  17. cat test.txt | xargs -n5
  18.  
  19. a b c d e
  20.  
  21. f g h i j
  22.  
  23. k l m n o
  24.  
  25. p q r
  26.  
  27. #-d指定定界符:
  28.  
  29. echo "nameXnameXnameXname" | xargs -dX
  30.  
  31. name name name name
  32.  
  33. #d、n 结合使用:
  34.  
  35. echo "nameXnameXnameXname" | xargs -dX -n2
  36.  
  37. name name
  38.  
  39. name name

-t 表示先打印命令,然后再执行

-i 或者是-I,将xargs的每项名称,一般是一行一行赋值给{}

  1. $ ls
  2. .txt .txt
  3.  
  4. $ ls *.txt |xargs -t -i mv {} {}.bak
  5. mv .txt .txt.bak
  6. mv .txt .txt.bak
  7.  
  8. $ ls
  9. .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

  1. #复制所有图片文件到 /data/images 目录下:
  2. ls *.jpg | xargs -n1 -I cp {} /data/images
  3.  
  4. #xargs结合find使用 用rm 删除太多的文件时候,可能得到一个错误信息:/bin/rm Argument list too long. 用xargs去避免这个问题:
  5. find . -type f -name "*.log" -print0 | xargs - rm -f
  6.  
  7. #xargs -0将\0作为定界符。 统计一个源代码目录中所有php文件的行数:
  8. find . -type f -name "*.php" -print0 | xargs - wc -l
  9.  
  10. #查找所有的jpg 文件,并且压缩它们:
  11. find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz
  12.  
  13. #假如你有一个文件包含了很多你希望下载的URL,你能够使用xargs下载所有链接:
  14. 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. spring与quartz定时器

    参考: http://www.iteye.com/topic/399980 http://www.cnblogs.com/xrab/p/5850186.html

  2. ASP.NET MVC 4 (十三) 基于表单的身份验证

    在前面的章节中我们知道可以在MVC应用程序中使用[Authorize]特性来限制用户对某些网址(控制器/控制器方法)的访问,但这都是在对用户认证之后,而用户的认证则依然是使用ASP.NET平台的认证机 ...

  3. 获取linux服务进程号

    ps -ef | grep "服务名" | grep -v "grep" | awk '{print $2}' # ps -ef|grep "被查询的 ...

  4. PHP利用ImageMagick把PDF转成PNG

    http://blog.csdn.net/longaction2012/article/details/12257867

  5. CVE-2019-8341 Jinja2 RCE漏洞学习

    漏洞简述 漏洞简介 Jinja2.10版本,Environment的实例方法from_string,存在RCE,该函数在内部实现逻辑中,存在exec函数去执行了,from_string函数参数中的ji ...

  6. C# 读写Excel的一些方法,Aspose.Cells.dll

    需求:现有2个Excel,一个7000,一个20W,7000在20W是完全存在的.现要分离20W的,拆分成19W3和7000. 条件:两个Excel都有“登录名”,然后用“登录名”去关联2个Excel ...

  7. Docker简介及Linux下安装

    什么是Docker? Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...

  8. 深度学习中的batch的大小对学习效果的影响

    Batch_size参数的作用:决定了下降的方向 极端一: batch_size为全数据集(Full Batch Learning): 好处: 1.由全数据集确定的方向能够更好地代表样本总体,从而更准 ...

  9. xss 学习记录

    反射型和持久型 一些简单的xss例子: <script>alert(/xss/)</script> 嵌入到textarea的,需要先关闭textarea标签 </text ...

  10. centos7安装zabbix3.4

    一.系统环境 关闭防火墙及selinux systemctl stop firewalld.service systemctl disable firewalld.service sed -i 's/ ...