一、直观感受

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. CentOS 7 设置静态IP

    cd /etc/sysconfig/network-scripts/ sudo vi ifcfg-eno16777736 BOOTPROTO=static #dhcp改为static(修改) IPAD ...

  2. Oracle清理回收站的方法

    原文链接:http://blog.itpub.net/18841027/viewspace-1057765/ purge DBA_RECYCLEBIN用于删除Oracle数据库回收站的所有数据,需要s ...

  3. 弹出框插件——dialog

    基于jquery和dot.js弹出框插件,兼容IE6+等其他浏览器. 思想:弹出框元素插入body节点中,并在页面垂直居中显示(fixed定位),触发确定和关闭事件绑定. 注意ie6包含两个问题:一. ...

  4. http 请求参数之Query String Parameters、Form Data、Request Payload

    Query String Parameters 当发起一次GET请求时,参数会以url string的形式进行传递.即?后的字符串则为其请求参数,并以&作为分隔符. 如下http请求报文头: ...

  5. xampp+YII搭建网站

    一.安装xampp xampp专为php开发设计,需要的apache,mysql,php已经自带了.特别提醒,请下载PHP版本高于5.4支持Yii2.0的xampp 二.配置环境变量 在系统的环境变量 ...

  6. [SLAM] ***AR Tracking based on which tools?

    SLAM虽然小复杂,但对于开发者而言,ar sdk通常会解决这个问题. 所以相对于识别,跟踪是个看上去高大上但实则不需要关注细节的部分. 识别,要技术深耕:跟踪,需行业深耕. 在此了解下常见的ar s ...

  7. zeromq使用模式实验总结

    zeromq:官网 安装  demo及各语言绑定 golang绑定 实验环境:win10 x64/centos6 x86 zeromq4.0.6 zmq三种模式:push/pull.pub/sub.r ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. twisted 学习笔记一:事件循环

    from twisted.internet import reactor import time def printTime(): print "Current time is", ...

  10. 01List.ashx(班级列表动态页面)

    01List.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...