Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2363140
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: LINUX

2012-03-27 01:05:31

Find命令及xargs的使用(2-结合xargs

 

转自:

 

在使用f i n d命令的- e x e c选项处理匹配到的文件时, f i n d命令将所有匹配到的文件一起传递给e x e c执行。但有些系统对能够传递给e x e c的命令长度有限制,这样在f i n d命令运行几分钟之后,就会出现溢出错误。错误信息通常是参数列太长参数列溢出。这就是x a rg s命令的用处所在,特别是与f i n d命令一起使用。

       F i n d
命令把匹配到的文件传递给x a rg s命令,而x a rg s命令每次只获取一部分文件而不是全部,不像- e x e c选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

      
在有些系统中,使用- e x e c选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;
      
而使用x a rg s命令则只有一个进程。另外,在使用x a rg s命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

      
来看看x a rg s命令是如何同f i n d命令一起使用的,并给出一些例子。
1下面的例子查找系统中的每一个普通文件,然后使用x a rg s命令来测试它们分别属于哪类文件

1.   #find . -type f -print | xargs file

2.   ./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text

3.   ./.kde/Autostart/.directory:      ISO-8859 text\

4.   ......



2)在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:

1.   $ find / -name "core" -print | xargs echo "" >/tmp/core.log


上面这个执行太慢,我改成在当前目录下查找

1.   #find . -name "file*" -print | xargs echo "" > /temp/core.log

2.   # cat /temp/core.log

3.   ./file6


3)在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限:

1.   # ls -l

2.   drwxrwxrwx    2 sam      adm          4096 10 30 20:14 file6

3.   -rwxrwxrwx    2 sam      adm             0 10 31 01:01 http3.conf

4.   -rwxrwxrwx    2 sam      adm             0 10 31 01:01 httpd.conf

5.    

6.   # find . -perm -7 -print | xargs chmod o-w

7.   # ls -l

8.   drwxrwxr-x    2 sam      adm          4096 10 30 20:14 file6

9.   -rwxrwxr-x    2 sam      adm             0 10 31 01:01 http3.conf

10.  -rwxrwxr-x    2 sam      adm             0 10 31 01:01 httpd.conf


4)用g r e p命令在所有的普通文件中搜索hostname这个词:

1.   # find . -type f -print | xargs grep "hostname"

2.   ./httpd1.conf:#     different IP addresses or hostnames and have them handled by the

3.   ./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames

4.   on your


5)用g r e p命令在当前目录下的所有普通文件中搜索hostnames这个词:

1.   # find . -name \* -type f -print | xargs grep "hostnames"

2.   ./httpd1.conf:#     different IP addresses or hostnames and have them handled by the

3.   ./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames

4.   on your

 

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