Chinaunix首页 | 论坛 | 博客
  • 博客访问: 868138
  • 博文数量: 179
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1546
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-27 11:05
个人简介

MySQL工程师 QQ:1815357042

文章分类

全部博文(179)

文章存档

2015年(179)

分类: LINUX

2015-03-04 15:36:27

转载自:
http://www.cnblogs.com/wdpp/archive/2012/02/28/2386683.html
http://blog.csdn.net/zhangfn2011/article/details/6776925


xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。
通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。
xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。


xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令,下面是一些如何有效使用xargs 的实用例子。

xargs 可以读入 stdin 的资料,并且以空白字元或断行字元作为分辨,将 stdin 的资料分隔成为 arguments 。
因为是以空白字元作为分隔,所以,如果有一些档名或者是其他意义的名词内含有空白字元的时候, xargs 可能就会误判了~他的用法其实也还满简单的!就来看一看先!

选项解释
-0 当sdtin含有特殊字元时候,将其当成一般字符,像/'空格等
例如:
  1. [root@steven ~]# echo "//"|xargs echo
  2. //
  3. [root@steven ~]# echo "//"|xargs -0 echo
  4. //


-a file 从文件中读入作为sdtin
-e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
  1. root@localhost:~/test#cat txt
  2. /bin tao shou kun
  3. root@localhost:~/test#cat txt|xargs -E 'shou' echo
  4. /bin tao


-p  prompt询问 当每次执行一个argument的时候询问一次用户。
  1. root@localhost:~/test#cat txt|xargs -p echo
  2. echo /bin tao shou kun ff ?...y


-n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。
  1. root@localhost:~/test#cat txt|xargs -n1 echo
  2. /bin
  3. tao
  4. shou
  5. kun
  6. root@localhost:~/test3#cat txt|xargs echo
  7. /bin tao shou kun


-t 表示先打印命令,然后再执行。
  1. root@localhost:~/test#cat txt|xargs -t echo
  2. echo /bin tao shou kun
  3. /bin tao shou kun


-i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。
  1. $ ls | xargs -t -i mv {} {}.bak


-r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。
  1. root@localhost:~/test#echo ""|xargs -t mv
  2. mv
  3. mv: missing file operand
  4. Try `mv --help


-s num 命令行的最好字符数,指的是xargs后面那个命令的最大命令行字符数。
  1. root@localhost:~/test#cat test |xargs -i -x -s 14 echo "{}"
  2. exp1
  3. exp5
  4. file
  5. xargs: argument line too long
  6. linux-2
  7. root@localhost:~/test#

 
-L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。
-l  同-L
-d delimiter分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符
  1. root@localhost:~/test#cat txt |xargs -i -p echo {}
  2. echo /bin tao shou kun ?...y
  3. root@localhost:~/test#cat txt |xargs -i -p -d " " echo {}
  4. echo /bin ?...y
  5. echo tao ?.../bin
  6. y
  7. echo shou ?...tao
  8. 再如:
  9. root@localhost:~/test#cat test |xargs -i -p -d " " echo {}
  10. echo exp1
  11. exp5
  12. file
  13. linux-2
  14. ngis_post
  15. tao
  16. test
  17. txt
  18. xen-3
  19.  ?...y
  20. root@localhost:~/test#cat test |xargs -i -p echo {}
  21. echo exp1 ?...y
  22. echo exp5 ?...exp1
  23. y
  24. echo file ?...exp5
  25. y


-x exit的意思,主要是配合-s使用。
-P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。



1. 当你尝试用rm 删除太多的文件,你可能得到一个错误信息:/bin/rm Argument list too long. 用xargs 去避免这个问题
find ~ -name ‘*.log’ -print0 | xargs -0 rm -f



2. 获得/etc/ 下所有*.conf 结尾的文件列表,有几种不同的方法能得到相同的结果,下面的例子仅仅是示范怎么实用xargs ,在这个例子中实用 xargs将find 命令的输出传递给ls -l
# find /etc -name "*.conf" | xargs ls –l


3. 假如你有一个文件包含了很多你希望下载的URL, 你能够使用xargs 下载所有链接
# cat url-list.txt | xargs wget –c




4. 查找所有的jpg 文件,并且压缩它
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz



5. 拷贝所有的图片文件到一个外部的硬盘驱动 
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory



阅读(914) | 评论(0) | 转发(0) |
0

上一篇:curl命令(转载)

下一篇:col命令(转载)

给主人留下些什么吧!~~