find: missing argument to `-exec' & 删除指定时间的文件

来源:互联网 发布:检察院模拟办案软件 编辑:程序博客网 时间:2024/06/05 10:37


在linux下使用find命令时,报错:find: missing argument to `-exec'

具体执行命令为:

Shell代码 复制代码 收藏代码
  1. find /u03 -name server.xml -exec grep '9080' {}\;  

目的是查找/u03目录下名称为server.xml的文件,根据查找的结果进行grep搜索,搜索其中包含9080的文本。 

-exec语法格式为: 

Shell代码 复制代码 收藏代码
  1. -exec command {} \;  

在{}和\之间必须要有空格,否则会报上面的错。 

加上空格之后重新执行命令:

Shell代码 复制代码 收藏代码
  1. find /u03 -name server.xml -exec grep '9080' {} \;  

结果:

Shell代码 复制代码 收藏代码
  1. <Connector port ="9080" redirectPort ="9443" connectionTimeout ="20000" URIEncoding ="utf-8"   
  2.                port="9080" protocol="HTTP/1.1"   
  3.                port="9080" protocol="HTTP/1.1"   
  4. <Connector port ="9080" redirectPort ="9443" connectionTimeout ="20000" URIEncoding ="utf-8"   
  5.                port="9080" protocol="HTTP/1.1"   




删除指定时间的文件必须:

 find /home/user/logs -mtime +10 -type f -exec rm -f {} \;     ------删除10天前的文件




    #find /tmp -mtime +30 -type f -name *.sh[ab] -exec rm -f {} \;
  假如在一个目录中保留最近30天的文件,30天前的文件自动删除
  #find /tmp -mtime +30 -type f -name *.sh[ab] -exec rm -f {} \;
  /tmp  --设置查找的目录;
  -mtime +30 --设置时间为30天前;
  -type f --设置查找的类型为文件;
  -name *.sh[ab] --设置文件名称中包含sha或者shb;
  -exec rm -f {} / --查找完毕后执行删除操作;


注意:

在{}和\之间必须要有空格,否则会报错。 




0 0
原创粉丝点击