列出基于时间戳的目录中的文件

时间:2013-08-28 19:29:22

标签: bash shell

在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

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

更新:

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

6 个答案:

答案 0 :(得分:4)

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

find ./ -mtime -60

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

60表示不到60天。

60如果你跳过+或 - 则意味着正好60天。

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

尝试此命令:

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

此外,如果您的find命令支持-newerXY,您可以拥有此信息:

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

答案 3 :(得分:1)

触摸带有时间戳的文件,找到所有旧文件。

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

答案 4 :(得分:1)

我喜欢纯粹的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

结果不会被排序,因为您没有提到您希望它们按排序顺序排列。

答案 5 :(得分:1)

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

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

然后,使用find查找至少minimum_age_in_minutes岁的所​​有文件。

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