Answers:
find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
对于一棵大树,可能很难sort将所有内容都保留在内存中。
%T@为您提供修改时间,例如unix时间戳,进行sort -n数字排序,tail -1获取最后一行(最高时间戳),cut -f2 -d" "从输出中删除第一个字段(时间戳)。
编辑:正如-printf可能只有GNU一样,的ajreals用法stat -c也是如此。尽管可以在BSD上执行相同的操作,但是格式化选项有所不同(-f "%m %N"看来)
我错过了复数部分;如果您想要更多而不是最新文件,只需增加tail参数即可。
sort -rn | head -3改为sort -n | tail -3。一个版本将文件从最旧到最新,而另一个版本将文件从最新到最旧。
                    sort会/tmp根据需要在中创建临时文件(在中),因此我认为这不是问题。
                    -printf '%T@ %Tb %Td  %TY %p\n'将给您一个日期戳(如果需要)(类似于ls)
                    find . -type f -printf '%TF %TT %p\n' | sort | tail -1
                    跟随@plundra的答案,以下是BSD和OS X版本:
find . -type f -print0 | xargs -0 stat -f "%m %N" |
sort -rn | head -1 | cut -f2- -d" "
              find支持+而不是\;?因为那做同样的事情(作为参数传递多个文件),而没有-print0 | xargs -0管道。
                    head -1为head -5
                    不用排序结果并只保留最后修改的结果,可以使用awk只打印修改时间最长的结果(以unix时间计):
find . -type f -printf "%T@\0%p\0" | awk '
    {
        if ($0>max) {
            max=$0; 
            getline mostrecent
        } else 
            getline
    } 
    END{print mostrecent}' RS='\0'
如果文件数量足够大,这应该是解决问题的较快方法。
我使用了NUL字符(即'\ 0'),因为从理论上讲,文件名可以包含任何字符(包括空格和换行符)。
如果您的系统中没有这样的病理文件名,则也可以使用换行符:
find . -type f -printf "%T@\n%p\n" | awk '
    {
        if ($0>max) {
            max=$0; 
            getline mostrecent
        } else 
            getline
    } 
    END{print mostrecent}' RS='\n'
此外,这也适用于mawk。
mawk用于Debian标准替代品。
                    我很难在Solaris 10下找到最后修改的文件。find没有该printf选项,stat因此不可用。我发现以下适合我的解决方案:
find . -type f | sed 's/.*/"&"/' | xargs ls -E | awk '{ print $6," ",$7 }' | sort | tail -1
要同时显示文件名,请使用
find . -type f | sed 's/.*/"&"/' | xargs ls -E | awk '{ print $6," ",$7," ",$9 }' | sort | tail -1
说明
find . -type f 查找并列出所有文件sed 's/.*/"&"/' 将路径名括在引号中以处理空格xargs ls -E将加引号的路径发送到ls,该-E选项可确保返回完整的时间戳(格式为year-month-day hour-minute-seconds-nanoseconds)awk '{ print $6," ",$7 }' 仅提取日期和时间awk '{ print $6," ",$7," ",$9 }' 提取日期,时间和文件名sort 返回按日期排序的文件tail -1 仅返回最后修改的文件即使使用子目录,这似乎也可以正常工作:
find . -type f | xargs ls -ltr | tail -n 1
如果文件太多,请优化查找。
-l选项ls似乎没有必要。只是-tr似乎足够了。
                    find . -type f -print0 | xargs -0 ls -ltr | tail -n 1
                    显示带有人类可读时间戳的最新文件:
find . -type f -printf '%TY-%Tm-%Td %TH:%TM: %Tz %p\n'| sort -n | tail -n1
结果如下:
2015-10-06 11:30: +0200 ./foo/bar.txt
要显示更多文件,请替换-n1为更大的数字
我一直都在使用类似的东西,以及最近修改过的文件的前k个列表。对于大型目录树,避免sort可能会更快。对于仅top-1最近修改的文件:
find . -type f -printf '%T@ %p\n' | perl -ne '@a=split(/\s+/, $_, 2); ($t,$f)=@a if $a[0]>$t; print $f if eof()'
在包含170万个文件的目录上,我得到的最新数据为3.4秒,使用sort的25.5s解决方案的速度提高了7.5倍。
lastfile),然后你可以做任何你喜欢用的结果,如ls -l $(lastfile .),或open $(lastfile .)(在Mac)等
                    在Ubuntu 13上,它可以做到以下几点,可能会更快一些,因为它可以反转排序并使用“ head”而不是“ tail”,从而减少了工作量。要在树中显示11个最新文件:
找 。类型的f -printf'%T @%p \ n'| 排序-n -r | 头-11 | 切-f2- -d“” | sed -e's,^。/ ,, | | xargs ls -U -l
这样就给出了完整的ls列表,而无需重新排序,并且省略了“查找”放在每个文件名上的烦人的“ ./”。
或者,作为bash函数:
treecent () {
  local numl
  if [[ 0 -eq $# ]] ; then
    numl=11   # Or whatever default you want.
  else
    numl=$1
  fi
  find . -type f -printf '%T@ %p\n' | sort -n -r | head -${numl} |  cut -f2- -d" " | sed -e 's,^\./,,' | xargs ls -U -l
}
尽管如此,大部分工作还是由plundra的原始解决方案完成的。谢谢plundra。
如果要stat逐个运行每个文件会减慢速度,则可以xargs加快速度:
find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -n | tail -1 | cut -f2- -d" " 
              这会将当前目录中所有目录的修改时间递归更改为每个目录中的最新文件:
for dir in */; do find $dir -type f -printf '%T@ "%p"\n' | sort -n | tail -1 | cut -f2- -d" " | xargs -I {} touch -r {} $dir; done
              $ 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
更多的 find关注链接。
我为此问题写了一个pypi / github包,因为我也需要一个解决方案。
https://github.com/bucknerns/logtail
安装:
pip install logtail
用法:尾部更改文件
logtail <log dir> [<glob match: default=*.log>]
用法2:在编辑器中打开最新更改的文件
editlatest <log dir> [<glob match: default=*.log>]