删除文件命令格式:

find 对应目录 -mtime +n -name "文件名" -exec rm -rf {} \; 

强行删除对应目录下修改时间在n天以前,满足指定文件名条件的所有文件

1. 关于find的官方man信息:

NAME

       find - search for files in a directory hierarchy

SYNOPSIS

       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

  Numeric arguments can be specified as

       +n     for greater than n,

       -n     for less than n,

       n      for exactly n.

       -amin n

              File was last accessed n minutes ago.

       -anewer file

              File  was  last accessed more recently than file was modified.  If file is a symbolic link and

              the -H option or the -L option is in effect, the access time of  the  file  it  points  to  is

              always used.

       -atime n

              File was last accessed n*24 hours ago.  When find figures out how many 24-hour periods ago the

              file was last accessed, any fractional part is ignored, so to match -atime +1, a file  has  to

              have been accessed at least two days ago.

2.参数说明

-mtime(Modify time) 修改时间

-atime(Access time) 访问时间

-ctime(Change time) 状态变更时间

 -anewer file  如上所示,表示访问时间比指定文件file的修改时间更新的文件(有什么用?)

 -cnewer file  表示状态变更时间比指定文件file的修改时间更新的文件(有什么用?)

没有-mnewer file 参数有-newer file参数,官方介绍如下:

 -newer file

              File was modified more recently than file. 

以上参数后面计算单位是n*24小时,及以天为单位计算。以下参数则以分钟数计算。

-amin -cmin -mmin

关于参数n,真心复杂(为方便描述,以天计算)

n   没有+-符号,表示从现在算起向前推第n天当天;

+n 表示从现在算起向前推第n天之前所有的,不包括第n天当天;

-n  表示从现在算起向前推第n天之后所有的(到今天),不包括第n天当天;

n=0 表示今天;

n=+0 表示今天之前所有天,不包含今天;

n=-0 表示今天之后所有天,不包含今天,所以最终结果肯定是空白,此值无意义。

n=-1 也是表示今天哦。

n用于表示天数时,是以n*24小时计算。

注意:其实n可以是小数,这样计算出来的情况就复杂多了。如果n为整数,则计算出来以天为单位,0*24小时,表示今天,1*24小时,表示昨天;如果n是小数后一位(如0.6*24)则以小时为单位,不足1小时忽略;如果n是小数点后两位(如0.65*24),则以分钟为单位,不足一分钟则忽略。

find 后对应目录如果以/*结尾,则只操作该目录下文件,不对该目录操作,如果以“/”或者不接“/*”,则该目录也会一同被操作。

-name 如果省略此参数,则对指定目录下所有文件操作。

-exec find其中一个参数,后面可以跟除rm以外的其他命令。

{} \; :固定写法

3.举例说明

find /backup/ncbackup/ctlfile/* -atime 30 -exec rm -rf {} \;

删除目录/backup/ncbackup/ctlfile/*下所有的30天以前过到目前为止没有访问的文件。

4.写入自动执行内

0 21 * * 0 /bin/find /backup/ncbackup/ctlfile/* -atime 30 -exec rm -rf {} \;

每周日21点自动 删除目录/backup/ncbackup/ctlfile/*下所有的30天以前过到目前为止没有访问的文件。


注:

通过命令 stat + 文件路径  查看访问、修改、状态更新时间

参考:http://www.cnblogs.com/peida/archive/2013/03/25/2980121.html

        http://blog.sina.com.cn/s/blog_6bde63fd0101mvaa.html

http://www.blogjava.net/decode360/archive/2009/09/18/287743.html