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然后结束。那么应该怎样达到这样的效果呢。有几个解决办法:

1. 通过 kill `ps -ef | grep 'ddd'`    
#等同于拼接字符串得到的命令,其效果类似于  kill $pid

2. for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill -9 $procid; done   
#其实与第一种原理一样,只不过需要多次kill的时候是循环处理的,每次处理一个

3. 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
1.txt  2.txt  


$ ls *.txt |xargs -t -i mv {} {}.bak
mv 1.txt 1.txt.bak 
mv 2.txt 2.txt.bak 


$ ls
1.txt.bak  2.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 -0 rm -f 

#xargs -0将作为定界符。 统计一个源代码目录中所有php文件的行数:
find . -type f -name "*.php" -print0 | xargs -0 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

原文地址:https://www.cnblogs.com/yuyutianxia/p/7766104.html