Older == Not Newer
Quite often we need to clean up old files in directory, but the criteria is often based on ctime/atime/mtime in
find
with unit in days. In certain situation, this may not be fine-grain enough to locate files. In find
, you can specify the -newer
flag to locate files newer than the file reference. However, find
does not have -older
. What you can do is to specify 'not newer' to represent 'older':\( ! -newer reference \)
Here I created a lot of files (4320) with file name based on time stamp and tried to use this trick to delete files older than the reference (log-200912301450). See my previous post in how I use the -newer
flag too.
# yr=2009 # for mth in `seq -w 7 12` do for day in `seq -w 1 30` do for hr in `seq -w 2 6 24` do for min in `seq -w 0 10 50` do touch -t $yr$mth$day$hr$min log-$yr$mth$day$hr$min done done done done # ls | wc -l 4320 # ls -lrt | head total 0 -rw-r--r-- 1 root root 0 2009-07-01 02:00 log-200907010200 -rw-r--r-- 1 root root 0 2009-07-01 02:10 log-200907010210 -rw-r--r-- 1 root root 0 2009-07-01 02:20 log-200907010220 -rw-r--r-- 1 root root 0 2009-07-01 02:30 log-200907010230 -rw-r--r-- 1 root root 0 2009-07-01 02:40 log-200907010240 -rw-r--r-- 1 root root 0 2009-07-01 02:50 log-200907010250 -rw-r--r-- 1 root root 0 2009-07-01 08:00 log-200907010800 -rw-r--r-- 1 root root 0 2009-07-01 08:10 log-200907010810 -rw-r--r-- 1 root root 0 2009-07-01 08:20 log-200907010820 # ls -lrt | tail -rw-r--r-- 1 root root 0 2009-12-30 14:20 log-200912301420 -rw-r--r-- 1 root root 0 2009-12-30 14:30 log-200912301430 -rw-r--r-- 1 root root 0 2009-12-30 14:40 log-200912301440 -rw-r--r-- 1 root root 0 2009-12-30 14:50 log-200912301450 -rw-r--r-- 1 root root 0 2009-12-30 20:00 log-200912302000 -rw-r--r-- 1 root root 0 2009-12-30 20:10 log-200912302010 -rw-r--r-- 1 root root 0 2009-12-30 20:20 log-200912302020 -rw-r--r-- 1 root root 0 2009-12-30 20:30 log-200912302030 -rw-r--r-- 1 root root 0 2009-12-30 20:40 log-200912302040 -rw-r--r-- 1 root root 0 2009-12-30 20:50 log-200912302050 # find . \( ! -newer log-200912301450 \) -type f -exec rm -f {} \; # ls -lrt total 0 -rw-r--r-- 1 root root 0 2009-12-30 20:00 log-200912302000 -rw-r--r-- 1 root root 0 2009-12-30 20:10 log-200912302010 -rw-r--r-- 1 root root 0 2009-12-30 20:20 log-200912302020 -rw-r--r-- 1 root root 0 2009-12-30 20:30 log-200912302030 -rw-r--r-- 1 root root 0 2009-12-30 20:40 log-200912302040 -rw-r--r-- 1 root root 0 2009-12-30 20:50 log-200912302050
Labels: shell script
0 Comments:
Post a Comment
<< Home