操作系统:Linux 文件系统类型:ext3 首选解决方案:Bash(脚本/一行程序)、Ruby或Python

我有几个目录,其中有几个子目录和文件。我需要列出所有这些目录,其构造方式是将每个一级目录列在其中最新创建/修改文件的日期和时间旁边。

为了说明这一点,如果我接触一个文件或修改它的内容向下几级子目录,该时间戳应该显示在第一级目录名旁边。假设我有一个这样的目录:

./alfa/beta/gamma/example.txt

我修改了文件example.txt的内容,我需要在第一级目录alfa旁边以人类可读的形式显示时间,而不是epoch。我已经尝试了一些使用find, xargs, sort和类似的东西,但我不能绕过“alfa”的文件系统时间戳不改变的问题,当我创建/修改文件的几个级别。


当前回答

我缩短了Daniel Böhmer对这句话的精彩回答:

stat --printf="%y %n\n" $(ls -tr $(find * -type f))

如果文件名中有空格,您可以使用以下修改:

OFS="$IFS";IFS=$'\n';stat --printf="%y %n\n" $(ls -tr $(find . -type f));IFS="$OFS";

其他回答

此命令适用于Mac OS X:

find "$1" -type f -print0 | xargs -0 gstat——format '%Y:% Y %n' | sort -nr | cut -d: -f2- | head

在Linux上,正如最初的海报所要求的那样,使用stat而不是gstat。

当然,这个答案是user37078的出色解决方案,从评论晋升为完整答案。我混合了CharlesB在Mac OS x上使用gstat的见解,顺便说一下,我是从MacPorts而不是Homebrew获得的coreutils。

下面是我如何将其打包成一个简单的命令~/bin/ls-recent.sh以供重用:

#!/bin/bash
# ls-recent: list files in a directory tree, most recently modified first
#
# Usage: ls-recent path [-10 | more]
#
# Where "path" is a path to target directory, "-10" is any argument to pass
# to "head" to limit the number of entries, and "more" is a special argument
# in place of "-10" which calls the pager "more" instead of "head".
if [ "more" = "$2" ]; then
   H=more; N=''
else
   H=head; N=$2
fi

find "$1" -type f -print0 |xargs -0 gstat --format '%Y :%y %n' \
    |sort -nr |cut -d: -f2- |$H $N

忽略隐藏文件-良好和快速的时间戳

下面介绍如何查找并列出带有子目录的目录中最新修改的文件。隐藏文件被故意忽略。然而文件名中的空格处理得很好-不是说你应该使用它们!时间格式可以自定义。

$ find . -type f -not -path '*/\.*' -printf '%TY.%Tm.%Td %THh%TM %Ta %p\n' |sort -nr |head -n 10

2017.01.25 18h23 Wed ./indenting/Shifting blocks visually.mht
2016.12.11 12h33 Sun ./tabs/Converting tabs to spaces.mht
2016.12.02 01h46 Fri ./advocacy/2016.Vim or Emacs - Which text editor do you prefer?.mht
2016.11.09 17h05 Wed ./Word count - Vim Tips Wiki.mht

更多的发现可以通过以下链接找到。

我缩短了Daniel Böhmer对这句话的精彩回答:

stat --printf="%y %n\n" $(ls -tr $(find * -type f))

如果文件名中有空格,您可以使用以下修改:

OFS="$IFS";IFS=$'\n';stat --printf="%y %n\n" $(ls -tr $(find . -type f));IFS="$OFS";

这篇文章中的Perl和Python解决方案帮助我解决了Mac OS X上的这个问题:

如何递归地列出按修改日期排序的文件(没有可用的stat命令!)

引自帖子:

Perl:

find . -type f -print |
perl -l -ne '
    $_{$_} = -M;  # store file age (mtime - now)
    END {
        $,="\n";
        print sort {$_{$b} <=> $_{$a}} keys %_;  # print by decreasing age
    }'

Python:

find . -type f -print |
python -c 'import os, sys; times = {}
for f in sys.stdin.readlines(): f = f[0:-1]; times[f] = os.stat(f).st_mtime
for f in sorted(times.iterkeys(), key=lambda f:times[f]): print f'

@anubhava的回答很好,但不幸的是,它不能在BSD工具上工作——也就是说,它不能与macOS上默认安装的find一起工作,因为BSD find没有-printf操作符。

所以这里有一个变体,适用于macOS + BSD(在我的Catalina Mac上测试),它结合了BSD find与xargs和stat:

$ find . -type f -print0 \
      | xargs -0 -n1 -I{} stat -f '%Fm %N' "{}" \
      | sort -rn 

当我在这里时,这里是我喜欢使用的BSD命令序列,它将时间戳设置为ISO-8601格式

$ find . -type f -print0 \
    | xargs -0 -n1 -I{} \
       stat  -f '%Sm %N' -t '%Y-%m-%d %H:%M:%S' "{}" \
    | sort -rn

(注意,与@anubhava的答案不同,我的答案将文件名从find传递给xargs作为单个参数,而不是以\0终止的列表,这将改变在最后输出的内容)

下面是GNU版本(即@anubhava的答案,但是iso-8601格式):

$ gfind . -type f -printf "%T+ %p\0" | sort -zk1nr

相关问:查找缺少选项-printf,现在怎么办?