我该如何使用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' 通常会对应于创建日期。
使用此命令在/home/
上搜索文件和文件夹,并根据您的需求添加时间段:
find /home/ -ctime time_period
time_period示例:
30天前以上:-ctime +30
30天前以下:-ctime -30
正好30天前:-ctime 30
ctime
与“创建时间”无关,它是指inode更改时间。 - Stephane Chazelas这是一个两步操作,但我喜欢这样做:
首先创建一个具有特定日期/时间的文件。在本例中,文件为2008年10月1日午夜创建。
touch -t 0810010000 /tmp/t
find / -newer /tmp/t
find / -not -newer /tmp/t
touch -t 0810010000 /tmp/t1
touch -t 0810011000 /tmp/t2
find / -newer /tmp/t1 -and -not -newer /tmp/t2
你可以这样做:
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
find -printf
,可以使用stat
。 - tripleeefind ./ -type f -mtime -60 -ls | grep '10 Sep'
。 - mgutt你不能。-c选项告诉你最后更改权限的时间,-a选项测试最近的访问时间,-m选项测试修改时间。Linux大多数版本使用的文件系统(ext3)不支持“创建时间”记录。抱歉!
@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
谓词,则此方法可行。cp `ls -ltr | grep 'Jun 14' | perl -wne 's/^.*\s+(\S+)$/$1/; print $1 . "\n";'` /some_destination_dir
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等参数会对您有所帮助。
-newerBt
将匹配文件的 _birth_(创建)时间。 - Stephane Chazelas-newerxt
自 2001 年起在 FreeBSD 上可用(最初在1998年作为补丁 提供),还有其他一些 BSD 和 GNU find(自 2007 年的 4.3.3 版本开始),它们基于 HP/UX find,该版本引入了-newerXY
(但其中 Y == t 不受支持)。 - Stephane Chazelas