如何使用UNIX命令find搜索在特定日期创建的文件?
当前回答
正如Max所指出的,你不能,但检查修改或访问的文件并不是那么难。我写了一个关于这个的教程,直到今天。其本质是使用-newerXY和!-newerXY:
示例:查找2007年6月7日修改过的所有文件:
$ find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08
查找2008年9月29日访问的所有文件:
$ find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30
或者,在同一天更改权限的文件:
$ find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30
如果不更改文件的权限,'c'通常对应于创建日期。
其他回答
@Max:关于创建时间是正确的。
但是,如果您想为-atime, -ctime, -mtime参数之一计算经过的天数参数,您可以使用以下表达式
ELAPSED_DAYS=$(( ( $(date +%s) - $(date -d '2008-09-24' +%s) ) / 60 / 60 / 24 - 1 ))
将“2008-09-24”替换为您想要的任何日期,ELAPSED_DAYS将被设置为从那时到今天之间的天数。(更新:从结果中减去1以与find的日期舍入对齐。)
因此,要找到2008年9月24日修改的任何文件,命令将是:
find . -type f -mtime $(( ( $(date +%s) - $(date -d '2008-09-24' +%s) ) / 60 / 60 / 24 - 1 ))
如果你的find版本不支持@Arve:的答案中提到的-newerXY谓词,这将有效。
我在一个删除所有超过14天的文件的脚本中找到了这个脚本:
CNT=0
for i in $(find -type f -ctime +14); do
((CNT = CNT + 1))
echo -n "." >> $PROGRESS
rm -f $i
done
echo deleted $CNT files, done at $(date "+%H:%M:%S") >> $LOG
我认为一点额外的“man find”和查找-ctime / -atime等参数将对您有帮助。
使用-atime、-ctime和-mtime开关进行查找,您可以接近您想要实现的目标。
你可以这样做:
find ./ -type f -ls |grep '10 Sep'
例子:
[root@pbx etc]# find /var/ -type f -ls | grep "Dec 24"
791235 4 -rw-r--r-- 1 root root 29 Dec 24 03:24 /var/lib/prelink/full
798227 288 -rw-r--r-- 1 root root 292323 Dec 24 23:53 /var/log/sa/sar24
797244 320 -rw-r--r-- 1 root root 321300 Dec 24 23:50 /var/log/sa/sa24
cp `ls -ltr | grep 'Jun 14' | perl -wne 's/^.*\s+(\S+)$/$1/; print $1 . "\n";'` /some_destination_dir