Chinaunix首页 | 论坛 | 博客
  • 博客访问: 231529
  • 博文数量: 131
  • 博客积分: 259
  • 博客等级: 二等列兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-21 21:15
文章分类

全部博文(131)

文章存档

2013年(3)

2011年(128)

分类:

2011-04-29 15:14:38

原文地址:xargs命令实例分析 作者:zyd_cu

Man page of xargs

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command  (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.  Blank lines on the standard input are ignored.

 

我的理解:xargs从标准输入读取由空格或者换行符分隔的条目,并将其作为xargs后面所跟命令参数的一部分(默认使用/bin/echo,所接的命令可以带参数),然后执行命令。

 

常用参数:

-f file 从文件中读取条目而不是从标准输入;

-0 读取的条目以null作为结束,而不是以空格或换行符进行分隔;

-d delim 指定分隔符;

--verbose 执行命令前显示所执行的命令;

-n max-args 指定参数个数

 

1xargs  –verbose       #从标准输入读,使用-f可从文件读参数

输入:hello world (Ctrl + D)

输出:/bin/echo hello world   #--verbose显示的执行命令

hello world

 

2ls /home/ydzhang/xargs | xargs rm –rf  #从管道读

输入:/home/ydzhang/xargs目录下的条目

输出:rm –rf a b c a,b,c/home/ydzhang/xargs下的文件)

 

3find /home/ydzhang | xargs grep “hello” #从管道读

输入:/home/ydzhang下的所有文件名

输出:/home/ydzhang目录下所有包含hello的文件对应的行

 

4find /home/ydzhang –print0 | xargs -0 grep “hello” #从管道读

输入:以null作为字符串结束符的文件名序列(-print0),将这些参数传给xargs时,如果使用默认参数,xargs将不能正确解析,必须告诉xargs这个信息,即使用-0参数。

输出:/home/ydzhang目录下所有包含hello的文件对应的行

 

阅读(455) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~