bash-如何删除所有一个或多个月前最后一次修改的文件

分享于 

4分钟阅读

ubuntu

  繁體

问题:

我找到了这样的解决方案:


find . -type f ! -newermt '04/29/2018 16:00:00' -exec rm -f {} ;



但我需要动态日期。


答案1:

根据man find,可以使用date命令。

-newerXY reference
 Succeeds if timestamp X of the file being considered is 
 newer than timestamp Y of the file reference. The letters X 
 and Y can be any of the following letters:
 a The access time of the file reference
 B The birth time of the file reference
 c The inode status change time of reference
 m The modification time of the file reference
 t reference is interpreted directly as a time
 Some combinations are invalid; for example, it is invalid 
 for X to be t. Some combinations are not implemented on 
 all systems; for example B is not supported on all systems. 
 If an invalid or unsupported combination of XY is 
 specified, a fatal error results. Time specifications 
 are interpreted as for the argument to the -d option of GNU 
 date. If you try to use the birth time of a reference file, 
 and the birth time cannot be determined, a fatal error 
 message results. If you specify a test which refers to 
 the birth time of files being examined, this test will fail 
 for any files where the birth time is unknown.

根据man date,你可以使用人类可读的短语,如"a month ago",有关详细信息,请参阅GNU Coreutils手册:日期输入格式

DATE STRING
 The --date=STRING is a mostly free format human readable date
 string such as"Sun, 29 Feb 2004 16:21:42 -0800" or 
"2004-02-29 16:21:42" or even"next Thursday". A date string 
 may contain items indicating calendar date, time of day, time 
 zone, day of week, relative time, relative date, and numbers. 
 An empty string indicates the beginning of the day. The date 
 string format is more complex than is easily documented here but 
 is fully described in the info documentation.
-delete
 Delete files; true if removal succeeded. If the 
 removal failed, an error message is issued. If -delete 
 fails, find's exit status will be nonzero (when it 
 eventually exits). Use of -delete automatically turns 
 on the `-depth' option.
 Warnings: Don't forget that the find command line is 
 evaluated as an expression, so putting -delete first will 
 make find try to delete everything below the starting 
 points you specified. When testing a find command line 
 that you later intend to use with -delete, you should 
 explicitly specify -depth in order to avoid later 
 surprises. Because -delete implies -depth, you cannot 
 usefully use -prune and -delete together.
 Together with the -ignore_readdir_race option, find 
 will ignore errors of the -delete action in the case the 
 file has disappeared since the parent directory was read: 
 it will not output an error diagnostic, and the return 
 code of the -delete action will be true.

你需要的命令可能类似于:


find -type f ! -newermt "last month" -delete




答案2:

你可以使用


find . -type f -mtime +30 -exec rm -f {} ;



或者更简单


find . -type f -mtime +30 -delete



将会删除30天前修改的所有文件。


答案3

我的解决方案:


find . -type f ! -newermt "$(date -d 'now - 1 month' +'%D %T')" -exec rm -f {} ;





文件  files  BASH  MODI  
相关文章