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

Bash 根据时间戳列出目录中的文件,bash,shell,Bash,Shell,在bash中,是否有基于时间戳列出目录中所有文件的命令行。e、 g \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.

在bash中,是否有基于时间戳列出目录中所有文件的命令行。e、 g

\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
我想列出8月28日13:00之前具有时间戳的所有文件

更新:

]$ find -version
GNU find version 4.2.27
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX 
请尝试以下命令:

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
ls-la显示的时间是最后一次修改日期。要列出2013/08/28 13:00:00之前最后一次修改的目录中的所有文件,请使用以下查找命令:


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

find ./ -mtime -60
+60表示您正在查找60天前修改的文件

60意味着不到60天


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

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

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

我很喜欢纯bash解决方案,不考虑日期和状态:


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

首先,了解文件的时间戳必须有多大,才能早于8月28日13:00

now=$(date +%s)
then=$(date +%s --date "2013-08-28 13:00")
minimum_age_in_minutes=$(( (now-then)/60 ))
然后,使用“查找”查找所有文件,这些文件的最小保存时间(以分钟为单位)


不知怎的-newermt对于我正在使用的find版本不可用。它抱怨说,find:invalid predicate'-newermt'你能把find-version的输出添加到你的问题中吗,或者一个pastebin?-mtime-60表示不到60天?
touch -d 'Aug 28 13:00' /tmp/timestamp
find . ! -newer /tmp/timestamp
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
now=$(date +%s)
then=$(date +%s --date "2013-08-28 13:00")
minimum_age_in_minutes=$(( (now-then)/60 ))
find "$dir" -mmin "+$minimum_age_in_minutes"