linux之xargs详解

xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。

xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的,这意味着通过管道传递给 xargs 的输入将被分隔成为 arguments 。所以,如果有一些文件记录或者是其它意义的名词内含有空白字符的时候,xargs 可能就会误判了。

xargs 也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。

xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。

之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了 xargs 命令,例如:

find /sbin -perm +700 |ls -l       #这个命令是错误的
find /sbin -perm +700 |xargs ls -l   #这样才是正确的

xargs 一般是和管道一起使用。

命令格式:

somecommand |xargs -item  command

xargs的默认命令是echo,空格是默认定界符,默认替换符号是{}。

参数:

  • -a file 从文件中读入作为sdtin
  • -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
  • -p 当每次执行一个argument的时候询问一次用户。
  • -t 表示先打印命令,然后再执行。
  • -n num 后面加次数,表示命令在执行的时候每次传递给command命令的参数个数,默认是用所有的。参数以空白字符或<newline>换行符分割,-L 和 -n 标志是互相排斥的,以最后指定的标志生效。
  • -i 或者是-I,这得看linux支持了,选项告诉 xargs 可以使用{}代替传递过来的参数,建议使用-I,其符合POSIX标准。
  • -l 格式: xargs  -I  rep-str  comand  rep-srt ,rep-str 为代替传递给的xargs参数,可以使 {} $ @ 等符号,其主要作用是当xargs   command 后有多个参数时,调整参数位置。例如:find  . -name  "*.txt "  |xargs -I {}  cp {} /tmp   
  • -L num 从标准输入一次读取num行送给Command命令 ,-L num和-l num功能一样,不建议使用。
  • -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。
  • -s num 命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数。
  • -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符。
  • -x exit的意思,主要是配合-s使用。。
  • -P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

实例

xargs 用作替换工具,读取输入数据重新格式化后输出。

定义一个测试文件,内有多行文本数据:

# cat test.txt

a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z

多行输入单行输出:

# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z

-n 选项多行输出:

# cat test.txt | xargs -n3

a b c
d e f
g h i
j k l
m n o
p q r
s t u
v w x
y z

-d 选项可以自定义一个定界符:

# echo "nameXnameXnameXname" | xargs -dX

name name name name

结合 -n 选项使用:

# echo "nameXnameXnameXname" | xargs -dX -n2

name name
name name

读取 stdin,将格式化后的参数传递给命令

假设一个命令为 sk.sh 和一个保存参数的文件 arg.txt:

#!/bin/bash
#sk.sh命令内容,打印出所有参数。

echo $*

arg.txt文件内容:

# cat arg.txt

aaa
bbb
ccc

xargs 的一个选项 -I,使用 -I 指定一个替换字符串 {},这个字符串在 xargs 扩展时会被替换掉,当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次:

# cat arg.txt | xargs -I {} ./sk.sh -p {} -l

-p aaa -l
-p bbb -l
-p ccc -l

复制所有图片文件到 /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 将 \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

xargs 其他应用

假如你有一个文件包含了很多你希望下载的 URL,你能够使用 xargs下载所有链接:

# cat url-list.txt | xargs wget -c

多行内容的单输出且每行3个

1

[root@localhost ftl]# cat /home/omc/ftl/logs.txt |xargs -n3

image

查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

1

[root@localhost log]# find /home/omc/ -maxdepth 1 -user root -type f | xargs file {}

image

在/var/log/下查找log文件,复制文件到/home/omc/ftl且把结果保存到/home/omc/ftl/logs.txt文件中

1

2

3

4

[root@localhost log]# find /var/log/*.log -type f | xargs -i cp {} /home/omc/ftl

[root@localhost log]# ll -ltr /home/omc/ftl

[root@localhost log]# find /var/log/*.log -type f > /home/omc/ftl/logs.txt

[root@localhost log]# ll /home/omc/ftl/logs.txt

image

删除 /home/omc/ftl/下的log文件

1

2

[root@localhost ftl]# ll *.log |xargs rm -rf {}     【错误】

[root@localhost ftl]# ls *.log |xargs rm -rf {}     【正确】

image

在当前目录下查找所有用户权限644的文件,并更改权限600

1

2

3

[root@localhost ftl]# ll *.txt

[root@localhost ftl]# find /home/omc/ftl -perm 644 | xargs chmod 600

[root@localhost ftl]# ll *.txt

image

 

用grep命令在当前目录下的所有普通文件中搜索omc这个词

1

2

find /etc/ -name \* -type f |xargs grep "omc"  >/tmp/ftl

    ==>find /etc/ -name "*" -type f |xargs grep "omc"  >/tmp/ftl

image

使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[]

find /var/ -name "*.log" |xargs -I [] cp [] /tmp/ 【xargs 默认用是i表示{},用I可以替换符号】

ll -ltr /tmp/ | more 5

image

xargs的-p参数的使用

find . -name "*.log" |xargs -p -i cp {} ../ltesig/

【-p参数会提示让你确认是否执行后面的命令,y执行,n不执行】

image

利用for循环实现和xargs同样的效果

1

2

find /home/omc/ -name *.txt | xargs    -i cp {} /home/omc/h

cat logs.txt |while read line;do echo $line >> logs_bak.txt; done;

image

利用xargs关闭不常用的系统启动软件

1

chkconfig | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | xargs -I{} chkconfig {} off

 

image

xargs总结示范:

1

2

3

4

5

find . -name "*.log" |xargs -p -i cp {} ../ltesig/ 【正确】

find . -name "*.log" |xargs -p cp {} ../ltesig/    【错误】

find . -type f -print | xargs file                 【正确】

find . -type f -print | xargs rm {}     【错误】

总结一下:如果命令后面可以跟内容,且没有目的路径的时候,可以省略-i,否则得加上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章