bash 根据时间戳列出目录中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18496649/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:25:08  来源:igfitidea点击:

list files in a directory based on timestamp

bashshell

提问by iamauser

In bash, is there a command line to list all the files in a directory based on a timestamp. e.g.,

在 bash 中,是否有一个命令行可以根据时间戳列出目录中的所有文件。例如,

\ls -ltr dir/file*

-rw-r--r-- 1 anon  root   338 Aug 28 12:30 g1.log
-rw-r--r-- 1 anon  root  2.9K Aug 28 12:32 g2.log
-rw-r--r-- 1 anon  root  2.9K Aug 28 12:41 g3.log
-rw-r--r-- 1 anon  root  2.9K Aug 28 13:03 g4.log
-rw-r--r-- 1 anon  root  2.9K Aug 28 13:05 g5.log

I want to list all the files that have timestamp before Aug 28 13:00.

我想列出所有具有时间戳之前的文件Aug 28 13:00

UPDATE :

更新 :

]$ find -version
GNU find version 4.2.27
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX 

回答by sr01853

You can use find command if you know the number of days

如果您知道天数,则可以使用 find 命令

find ./ -mtime -60

+60 means you are looking for a file modified 60 days ago.

+60 表示您正在查找 60 天前修改的文件。

60 means less than 60 days.

60 表示少于 60 天。

60 If you skip + or - it means exactly 60 days.

60 如果您跳过 + 或 -,则表示正好是 60 天。

回答by hek2mgl

The time displayed by ls -lais the last modification date. To list all the files in a directory which have been last modified before 2013/08/28 13:00:00, use the following findcommand:

显示的时间ls -la为最后修改日期。要列出目录中最后修改过的所有文件2013/08/28 13:00:00,请使用以下find命令:

find -maxdepth 0 -type f -newermt '2013-08-28 13:00:00'

回答by konsolebox

Try this command:

试试这个命令:

read T < <(exec date -d 'Aug 28 13:00' '+%s') && find /dir -type f | while IFS= read -r FILE; do read S < <(exec stat -c '%Y' "$FILE") && [[ S -lt T ]] && echo "$FILE"; done

Also, if your findcomand support -newerXYyou could have this:

另外,如果你的find命令支持-newerXY你可以有这个:

find /dir -type f -not -newermt 'Aug 28 13:00'

回答by ceving

Touch a file with the time stamp and find all older files.

触摸带有时间戳的文件并查找所有较旧的文件。

touch -d 'Aug 28 13:00' /tmp/timestamp
find . ! -newer /tmp/timestamp

回答by Aleks-Daniel Jakimenko-A.

I love pure bash solutions (well, not considering dateand stat):

我喜欢纯 bash 解决方案(好吧,不考虑datestat):

dateStr='Aug 28 13:00'

timestamp=$(date -d "$dateStr" +%s)
for curFile in *; do
    curFileMtime=$(stat -c %Y "$curFile")
    if (( curFileMtime < timestamp )); then
        echo "$curFile"
    fi
done

The results are not going to be sorted, because you didn't mention that you want them in sorted order.

结果不会被排序,因为你没有提到你想要它们按排序顺序。

回答by chepner

First, find out how old a file must be to have a timestamp earlier than (e.g.) Aug 28 at 13:00.

首先,找出文件的时间戳必须早于(例如)8 月 28 日 13:00。

now=$(date +%s)
then=$(date +%s --date "2013-08-28 13:00")
minimum_age_in_minutes=$(( (now-then)/60 ))

Then, use findto find all files that are at least minimum_age_in_minutesold.

然后,用于find查找至少minimum_age_in_minutes旧的所有文件。

find "$dir" -mmin "+$minimum_age_in_minutes"