Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9246771
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类:

2011-11-30 17:45:16

Linux find 命令 -exec 参数说明
分类: Linux 2551人阅读 评论(2) 举报

 

       经常使用一条命令,find + -exec参数,但是没有仔细研究过。 今天要写个脚本,又用到这个,故小研究了下了。 如:

       find /usr/local/backups -mtime +10 -name "*.*" -exec rm -rf {} /;

 

先看find 命令的帮助文档:man find

其中的exec选项解释如下:      

1-exec command ;

       Execute  command;  true if 0 status is returned.  All following arguments to find are taken to be arguments to the command until an  argument  consisting of ';' is encountered.  The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to  the  command,  not just  in  arguments where it is alone, as in some versions of find.  Both of  these constructions might need to be escaped (with a '/') or quoted to  protect  them  from expansion by the shell.  See the EXAMPLES section for examples of the use of the '-exec' option.  The specified command  is  run  once for  each  matched file.  The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec option; you should use the -execdir option instead.

 

2-exec command {} +

       This  variant of the -exec option runs the specified command on the selected files, but the command line is built by appending each selected file name at  the  end;  the  total number of invocations of the command will be much less than the number of matched files.  The command line is  built  in  much  the same  way that xargs builds its command lines.  Only one instance of '{}' is allowed within the command.  The command is executed in the starting directory.

 

1; (分号)表示command命令参数的结束,特别强调,对于不同的系统,直接使用分号可能会有不同的意义, 所以使用转义符/在分号前明确说明。

2{}表示文件名,也就是find前面处理过程中过滤出来的文件,用于command命令进行处理。

示例:

删除所有临时文件

       find / -name "*.tmp" -exec rm -rf {} /;

 

查找10天前的dmp文件,并将文件copy/root/davetmp/目录:

       find /root/py -mtime +10 -name "*.dmp" -exec cp {} /root/davetmp/  /;

 

 

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