bash 如何使用“查找”来搜索在特定日期创建的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/158044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use 'find' to search for files created on a specific date?
提问by sverrejoh
How do I use the UNIX command find
to search for files created on a specific date?
如何使用 UNIX 命令find
搜索在特定日期创建的文件?
回答by Arve
As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a tutorialabout this, as late as today. The essence of which is to use -newerXY
and ! -newerXY
:
正如 Max 指出的那样,您不能,但检查修改或访问的文件并不是那么难。我写了一篇关于这个的教程,直到今天。其本质是使用-newerXY
和! -newerXY
:
Example: To find all files modified on the 7th of June, 2007:
示例:要查找 2007 年 6 月 7 日修改的所有文件:
$ find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08
To find all files accessed on the 29th of september, 2008:
要查找在 2008 年 9 月 29 日访问的所有文件:
$ find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30
Or, files which had their permission changed on the same day:
或者,在同一天更改了权限的文件:
$ find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30
If you don't change permissions on the file, 'c' would normally correspond to the creation date, though.
但是,如果您不更改文件的权限,“c”通常对应于创建日期。
回答by Chris
find location -ctime time_period
find location -ctime time_period
Examples of time_period:
time_period 的示例:
More than 30 days ago:
-ctime +30
Less than 30 days ago:
-ctime -30
Exactly 30 days ago:
-ctime 30
30多天前:
-ctime +30
不到 30 天前:
-ctime -30
正好 30 天前:
-ctime 30
回答by Mark Biek
It's two steps but I like to do it this way:
这是两个步骤,但我喜欢这样做:
First create a file with a particular date/time. In this case, the file is 2008-10-01 at midnight
首先创建一个具有特定日期/时间的文件。在这种情况下,文件是 2008-10-01 午夜
touch -t 0810010000 /tmp/t
Now we can find all files that are newer or older than the above file (going by file modified date. You can also use -anewerfor accessed and -cnewerfile status changed).
现在我们可以找到比上述文件更新或旧的所有文件(按文件修改日期。您也可以使用-anewer来访问和-cnewer文件状态已更改)。
find / -newer /tmp/t
find / -not -newer /tmp/t
You could also look at files between certain dates by creating two files with touch
您还可以通过使用触摸创建两个文件来查看特定日期之间的文件
touch -t 0810010000 /tmp/t1
touch -t 0810011000 /tmp/t2
This will find files between the two dates & times
这将在两个日期和时间之间找到文件
find / -newer /tmp/t1 -and -not -newer /tmp/t2
回答by Jeff MacDonald
You could do this:
你可以这样做:
find ./ -type f -ls |grep '10 Sep'
Example:
例子:
[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
回答by Max Cantor
You can't. The -c switch tells you when the permissions were last changed, -a tests the most recent access time, and -m tests the modification time. The filesystem used by most flavors of Linux (ext3) doesn't support a "creation time" record. Sorry!
你不能。-c 开关告诉您上次更改权限的时间,-a 测试最近的访问时间,而 -m 测试修改时间。大多数 Linux (ext3) 版本使用的文件系统不支持“创建时间”记录。对不起!
回答by yukondude
@Max: is right about the creation time.
@Max:关于创建时间是正确的。
However, if you want to calculate the elapsed days argument for one of the -atime
, -ctime
, -mtime
parameters, you can use the following expression
但是,如果要计算-atime
, -ctime
,-mtime
参数之一的经过天数参数,可以使用以下表达式
ELAPSED_DAYS=$(( ( $(date +%s) - $(date -d '2008-09-24' +%s) ) / 60 / 60 / 24 - 1 ))
Replace "2008-09-24" with whatever date you want and ELAPSED_DAYS will be set to the number of days between then and today. (Update: subtract one from the result to align with find
's date rounding.)
将“2008-09-24”替换为您想要的任何日期,ELAPSED_DAYS 将设置为从那时到今天之间的天数。(更新:从结果中减去一以与find
的日期舍入对齐。)
So, to find any file modified on September 24th, 2008, the command would be:
因此,要查找 2008 年 9 月 24 日修改的任何文件,命令将是:
find . -type f -mtime $(( ( $(date +%s) - $(date -d '2008-09-24' +%s) ) / 60 / 60 / 24 - 1 ))
This will work if your version of find
doesn't support the -newerXY
predicates mentioned in @Arve:'s answer.
如果您的版本find
不支持-newerXY
@Arve: 的回答中提到的谓词,这将起作用。
回答by ayaz
With the -atime, -ctime, and -mtime switches to find, you can get close to what you want to achieve.
使用 -atime、-ctime 和 -mtime 开关进行查找,您可以接近您想要实现的目标。
回答by Tintin
cp `ls -ltr | grep 'Jun 14' | perl -wne 's/^.*\s+(\S+)$//; print . "\n";'` /some_destination_dir
回答by Georgi
I found this scriplet in a script that deletes all files older than 14 days:
我在删除所有早于 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
I think a little additional "man find" and looking for the -ctime / -atime etc. parameters will help you here.
我认为一些额外的“man find”并寻找 -ctime / -atime 等参数会在这里帮助你。