Linux 查找在日期范围内创建的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18339307/
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
Find files in created between a date range
提问by sanjuro8998
I use AIX via telnet here at work, and I'd like to know how to find files in a specific folder between a date range. For example: I want to find all files in folder X that were created between 01-Aug-13 and 31-Aug-13.
我在工作中通过 telnet 使用 AIX,我想知道如何在日期范围之间的特定文件夹中查找文件。例如:我想在文件夹 X 中查找在 01-Aug-13 和 31-Aug-13 之间创建的所有文件。
Observations:
观察:
- The
touch
trick (where you create two empty files to use the -newer option) does not work for me, once the user roles that I have on the server does not allow me to create files. - I need to find between specific dates, not days (like: files that were created more than 30 days ago, etc...)
touch
一旦我在服务器上拥有的用户角色不允许我创建文件,这个技巧(创建两个空文件以使用 -newer 选项)对我不起作用。- 我需要在特定日期之间查找,而不是几天(例如:30 多天前创建的文件等...)
采纳答案by Alex Atkinson
You can use the below to find what you need.
您可以使用以下内容找到您需要的内容。
Find files older than a specific date/time:
查找早于特定日期/时间的文件:
find ~/ -mtime $(echo $(date +%s) - $(date +%s -d"Dec 31, 2009 23:59:59") | bc -l | awk '{print / 86400}' | bc -l)
Or you can find files between two dates. First date more recent, last date, older. You can go down to the second, and you don't have to use mtime. You can use whatever you need.
或者您可以在两个日期之间查找文件。第一次约会较近,最后一次约会较旧。可以下到第二个,不用mtime。您可以使用任何您需要的东西。
find . -mtime $(date +%s -d"Aug 10, 2013 23:59:59") -mtime $(date +%s -d"Aug 1, 2013 23:59:59")
回答by choroba
Use stat
to get the creation time. You can compare the time in the format YYYY-MM-DD HH:MM:SS
lexicographically.
使用stat
来获取创建时间。您可以按YYYY-MM-DD HH:MM:SS
字典顺序比较格式中的时间。
This work on Linux with modification time, creation time is not supported. On AIX, the -c
option might not be supported, but you should be able to get the information anyway, using grep
if nothing else works.
Linux 上的这项工作有修改时间,不支持创建时间。在 AIX 上,该-c
选项可能不受支持,但无论如何您都应该能够获取信息,grep
如果没有其他方法可以使用。
#! /bin/bash
from='2013-08-01 00:00:00.0000000000' # 01-Aug-13
to='2013-08-31 23:59:59.9999999999' # 31-Aug-13
for file in * ; do
modified=$( stat -c%y "$file" )
if [[ $from < $modified && $modified < $to ]] ; then
echo "$file"
fi
done
回答by codebeard
If you use GNU find
, since version 4.3.3 you can do:
如果您使用 GNU find
,从 4.3.3 版开始,您可以执行以下操作:
find -newerct "1 Aug 2013" ! -newerct "1 Sep 2013" -ls
It will accept any date string accepted by GNU date -d
.
它将接受 GNU 接受的任何日期字符串date -d
。
You can change the c
in -newerct
to any of a
, B
, c
, or m
for looking at atime/birth/ctime/mtime.
您可以更改c
在-newerct
任何的a
,B
,c
,或m
为寻找的atime /生/的ctime /修改时间。
Another example - list files modified between 17:30 and 22:00 on Nov 6 2017:
另一个例子 - 列出 2017 年 11 月 6 日 17:30 到 22:00 之间修改的文件:
find -newermt "2017-11-06 17:30:00" ! -newermt "2017-11-06 22:00:00" -ls
Full details from man find
:
完整详情来自man find
:
-newerXY reference
Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one of its timestamps is used
for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select
which time belonging to how reference is used for the comparison.
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.
回答by frank42
Try this:
尝试这个:
find /var/tmp -mtime +2 -a -mtime -8 -ls
find /var/tmp -mtime +2 -a -mtime -8 -ls
to find files older than 2 days but not older than 8 days.
查找早于 2 天但不早于 8 天的文件。
回答by Jason Slobotski
Some good solutions on here. Wanted to share mine as well as it is short and simple.
这里有一些很好的解决方案。想分享我的,因为它简短而简单。
I'm using find (GNU findutils) 4.5.11
我正在使用 find (GNU findutils) 4.5.11
$ find search/path/ -newermt 20130801 \! -newermt 20130831
回答by Dr Beco
Script oldfiles
脚本旧文件
I've tried to answer this question in a more complete way, and I ended up creating a complete script with options to help you understand the find
command.
我试图以更完整的方式回答这个问题,最后我创建了一个完整的脚本,其中包含帮助您理解find
命令的选项。
The script oldfiles
is in this repository
该脚本oldfiles
在此存储库中
To "create" a new find command you run it with the option -n
(dry-run), and it will print to you the correct find
command you need to use.
要“创建”一个新的查找命令,您可以使用选项-n
(dry-run)运行它,它会向您打印find
您需要使用的正确命令。
Of course, if you omit the -n
it will just run, no need to retype the find
command.
当然,如果省略-n
它只会运行,无需重新键入find
命令。
Usage:
用法:
$ oldfiles [-v...] ([-h|-V|-n] | {[(-a|-u) | (-m|-t) | -c] (-i | -d | -o| -y | -g) N (-\> | -\< | -\=) [-p "pat"]})
$ oldfiles [-v...] ([-h|-V|-n] | {[(-a|-u) | (-m|-t) | -c] (-i | -d | -o| -y | -g) N (-\> | -\< | -\=) [-p "pat"]})
- Where the options are classified in the following groups:
- Help & Info:
-h, --help : Show this help.
-V, --version : Show version.
-v, --verbose : Turn verbose mode on (cumulative).
-n, --dry-run : Do not run, just explain how to create a "find" command - Time type (access/use, modification time or changed status):
-a or -u : access (use) time
-m or -t : modification time (default)
-c : inode status change - Time range (where N is a positive integer):
-i N : minutes (default, with N equal 1 min)
-d N : days
-o N : months
-y N : years
-g N : N is a DATE (example: "2017-07-06 22:17:15") - Tests:
-p "pat" : optional pattern to match (example: -p "*.c" to find c files) (default -p "*")
-\> : file is newer than given range, ie, time modified after it.
-\< : file is older than given range, ie, time is from before it. (default)
-\= : file that is exactly N (min, day, month, year) old.
- Help & Info:
- 其中选项分为以下几组:
- 帮助和信息:
-h, --help :显示此帮助。
-V, --version : 显示版本。
-v, --verbose : 打开详细模式(累积)。
-n, --dry-run :不运行,只解释如何创建“查找”命令 - 时间类型(访问/使用、修改时间或更改状态):
-a 或 -u :访问(使用)时间
-m 或 -t :修改时间(默认)
-c :inode 状态更改 - 时间范围(其中 N 为正整数):
-i N :分钟(默认,N 等于 1 分钟)
-d N :天
-o N :月
-y N :年
-g N :N 是日期(例如:“2017-07-06 22:17: 15") - 测试:
-p "pat" :匹配的可选模式(例如:-p "*.c" 查找 c 文件)(默认 -p "*")
-\> :文件比给定范围新,即,在它之后修改时间.
-\< : 文件比给定范围更旧,即时间是从它之前的。(默认)
-\= :正好是 N(分钟、日、月、年)旧的文件。
- 帮助和信息:
Example:
例子:
- Find C source files newer than 10 minutes (access time) (with verbosity 3):
- 查找更新超过 10 分钟(访问时间)的 C 源文件(详细程度为 3):
$ oldfiles -a -i 10 -p"*.c" -\> -nvvv
Starting oldfiles script, by beco, version 20170706.202054...
$ oldfiles -vvv -a -i 10 -p "*.c" -\> -n
Looking for "*.c" files with (a)ccess time newer than 10 minute(s)
find . -name "*.c" -type f -amin -10 -exec ls -ltu --time-style=long-iso {} +
Dry-run
$ oldfiles -a -i 10 -p"*.c" -\> -nvvv
Starting oldfiles script, by beco, version 20170706.202054...
$ oldfiles -vvv -a -i 10 -p "*.c" -\> -n
Looking for "*.c" files with (a)ccess time newer than 10 minute(s)
find . -name "*.c" -type f -amin -10 -exec ls -ltu --time-style=long-iso {} +
Dry-run
- Find H header files older than a month (modification time) (verbosity 2):
- 查找超过一个月的 H 头文件(修改时间)(详细程度 2):
$ oldfiles -m -o 1 -p"*.h" -\< -nvv
Starting oldfiles script, by beco, version 20170706.202054...
$ oldfiles -vv -m -o 1 -p "*.h" -\< -n
find . -name "*.h" -type f -mtime +30 -exec ls -lt --time-style=long-iso {} +
Dry-run
$ oldfiles -m -o 1 -p"*.h" -\< -nvv
Starting oldfiles script, by beco, version 20170706.202054...
$ oldfiles -vv -m -o 1 -p "*.h" -\< -n
find . -name "*.h" -type f -mtime +30 -exec ls -lt --time-style=long-iso {} +
Dry-run
- Find all (*) files within a single day (Dec, 1, 2016; no verbosity, dry-run):
- 在一天内查找所有 (*) 文件(2016 年 12 月 1 日;不冗长,试运行):
$ oldfiles -mng "2016-12-01" -\=
find . -name "*" -type f -newermt "2016-11-30 23:59:59" ! -newermt "2016-12-01 23:59:59" -exec ls -lt --time-style=long-iso {} +
$ oldfiles -mng "2016-12-01" -\=
find . -name "*" -type f -newermt "2016-11-30 23:59:59" ! -newermt "2016-12-01 23:59:59" -exec ls -lt --time-style=long-iso {} +
Of course, removing the -n
the program will run the find
command itself and save you the trouble.
当然,删除该-n
程序将运行find
命令本身并为您省去麻烦。
I hope this helps everyone finally learn this {a,c,t}{time,min}
options.
我希望这可以帮助每个人最终了解这个{a,c,t}{time,min}
选项。
the LS output:
LS 输出:
You will also notice that the "ls" option ls OPT
changes to match the type of time you choose.
您还会注意到“ls”选项会ls OPT
更改以匹配您选择的时间类型。
Link for clone/download of the oldfiles
script:
克隆/下载oldfiles
脚本的链接:
回答by avivamg
Explanation: Use unix command find
with -ctime
(creation time) flag
说明:使用find
带有-ctime
(创建时间)标志的unix 命令
The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the 'primaries' and 'operands') in terms of each file in the tree.
find 实用程序针对列出的每个路径递归地降低目录树,根据树中的每个文件计算表达式(由“主要”和“操作数”组成)。
Solution: According to documenation
解决方案:根据文档
-ctime n[smhdw]
If no units are specified, this primary evaluates to true if the difference
between the time of last change of file status information and the time find
was started, rounded up to the next full 24-hour period, is n 24-hour peri-
ods.
If units are specified, this primary evaluates to true if the difference
between the time of last change of file status information and the time find
was started is exactly n units. Please refer to the -atime primary descrip-
tion for information on supported time units.
Formula: find <path> -ctime +[number][timeMeasurement] -ctime -[number][timeMeasurment]
公式:find <path> -ctime +[number][timeMeasurement] -ctime -[number][timeMeasurment]
Examples:
例子:
1.Find everything that were created after 1 week ago ago and before 2 weeks ago
1.查找1周前和2周前创建的所有内容
find / -ctime +1w -ctime -2w
find / -ctime +1w -ctime -2w
2.Find all javascript files (.js
) in current directory that were created between 1 day ago to 3 days ago
2..js
在当前目录中查找1天前到3天前创建的所有javascript文件( )
find . -name "*\.js" -type f -ctime +1d -ctime -3d
find . -name "*\.js" -type f -ctime +1d -ctime -3d
回答by hizlan erpak
List files between 2 dates
列出两个日期之间的文件
find . -type f -newermt "2019-01-01" ! -newermt "2019-05-01"
找 。-type f -newermt "2019-01-01" !-newermt "2019-05-01"
or
或者
find path -type f -newermt "2019-01-01" ! -newermt "2019-05-01"
find path -type f -newermt "2019-01-01" !-newermt "2019-05-01"