Linux命令-find命令之xargs

find命令之xargs:
    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。  

    find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。  

    在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高; 而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。
---------------------------------------------------------------------------------------------
实例1: 查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件
# ll
总用量 48
-rw-r--r-- 1 root root    32 1月  15 11:18 a.txt
-rw-r--r-- 1 root root    77 1月  15 11:18 b.txt
-rw-r--r-- 1 root root    87 1月  15 11:18 c.txt
-rw-r--r-- 1 root root 14004 1月  15 11:18 d.txt
-rw-r--r-- 1 root root   119 1月  15 11:18 e.txt
-rw-r--r-- 1 root root    80 1月  15 11:18 f.txt
-rw-r--r-- 1 root root   591 1月  15 11:18 g.txt
-rw-r--r-- 1 root root  1016 1月  15 11:18 h.txt
-rw-r--r-- 1 root root   355 1月  15 11:18 tail.txt
[root@lanzhu test1]# find . -type f -print | xargs file
./d.txt:    UTF-8 Unicode text
./g.txt:    UTF-8 Unicode text
./e.txt:    UTF-8 Unicode text
./c.txt:    UTF-8 Unicode text
./f.txt:    UTF-8 Unicode text
./b.txt:    UTF-8 Unicode text
./h.txt:    UTF-8 Unicode text
./a.txt:    UTF-8 Unicode text
./tail.txt: UTF-8 Unicode text
-----------------------------------------------------------------------------------------------
事例2:在整个系统中查找“Dog.java”文件,然后把找到的地址结果保存到/home/dx/test01/test1/1.txt文件中
[root@lanzhu test1]# ls
1.txt  a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  g.txt  h.txt  tail.txt
[root@lanzhu test1]# find / -name "Dog.java" -print
/home/lizhen/Dog.java
[root@lanzhu test1]# find / -name "Dog.java" -print |xargs echo "" >/home/dx/test01/test1/1.txt
[root@lanzhu test1]# ll
总用量 52
-rw-r--r-- 1 root root    23 1月  15 14:30 1.txt
-rw-r--r-- 1 root root    32 1月  15 11:18 a.txt
-rw-r--r-- 1 root root    77 1月  15 11:18 b.txt
-rw-r--r-- 1 root root    87 1月  15 11:18 c.txt
-rw-r--r-- 1 root root 14004 1月  15 11:18 d.txt
-rw-r--r-- 1 root root   119 1月  15 11:18 e.txt
-rw-r--r-- 1 root root    80 1月  15 11:18 f.txt
-rw-r--r-- 1 root root   591 1月  15 11:18 g.txt
-rw-r--r-- 1 root root  1016 1月  15 11:18 h.txt
-rw-r--r-- 1 root root   355 1月  15 11:18 tail.txt
[root@lanzhu test1]# cat 1.txt
 /home/lizhen/Dog.java
-------------------------------------------------------------------------------------------------
实例3:在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限

命令:

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

输出:

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxrwx 2 root root   4096 11-12 19:32 test3

drwxrwxrwx 2 root root   4096 11-12 19:32 test4

[root@localhost test]# find . -perm -7 -print | xargs chmod o-w

[root@localhost test]# ll

总计 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root   4096 10-27 01:58 scf

drwxrwxr-x 2 root root   4096 11-12 19:32 test3

drwxrwxr-x 2 root root   4096 11-12 19:32 test4

[root@localhost test]#

说明:

执行命令后,文件夹scf、test3和test4的权限都发生改变
--------------------------------------------------------------------------------------------
事例4:用grep命令在所有的普通文件中搜索“tail”这个词:
# find . -type f -print | xargs grep "tail"
./tail.txt:tail命令参数:
--------------------------------------------------------------------------------------------
实例5:用grep命令在当前目录下的所有普通文件中搜索"tail"这个词:
(注意,在下面的例子中, \用来取消find命令中的*在shell中的特殊含义。)
# find . -name \* -type f -print | xargs grep "tail"
./tail.txt:tail命令参数:
--------------------------------------------------------------------------------------------
事例6:使用xargs执行mv命令:
# ll test2
总用量 0
[root@lanzhu test01]# find . -name "*.txt" | xargs -i mv {} test2
[root@lanzhu test01]# ll test2
总用量 52
-rw-r--r-- 1 root root    23 1月  15 14:30 1.txt
-rw-r--r-- 1 root root    32 1月  11 11:32 a.txt
-rw-r--r-- 1 root root    77 1月  15 11:18 b.txt
-rw-r--r-- 1 root root    87 1月  15 11:18 c.txt
-rw-r--r-- 1 root root 14004 1月  15 11:18 d.txt
-rw-r--r-- 1 root root   119 1月  15 11:18 e.txt
-rw-r--r-- 1 root root    80 1月  15 11:18 f.txt
-rw-r--r-- 1 root root   591 1月  15 11:18 g.txt
-rw-r--r-- 1 root root  1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root   355 1月  13 14:54 tail.txt
---------------------------------------------------------------------------------------------
事例7: 查找当前目录下当日的普通文件,然后批量删除:
(-l1是一次处理一个;-t是处理之前打印出命令)
[root@lanzhu test2]# find . -type f -atime 0 -print0 | xargs -0 -l1 -t rm -f
rm -f ./d.txt 
rm -f ./1.txt 
rm -f ./g.txt 
rm -f ./e.txt 
rm -f ./c.txt 
rm -f ./f.txt 
rm -f ./b.txt 
rm -f ./h.txt 
rm -f ./a.txt 
rm -f ./tail.txt 
[root@lanzhu test2]# ll
总用量 0
----------------------------------------------------------------------------------------------
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值