Mac unix脚本:给定文件路径列表,找到具有最新修改日期的路径?

时间:2013-12-09 02:44:42

标签: macos bash bsd

如果我有一个文件名列表(绝对路径),如何确定在命令行中最后修改哪一个?

谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个:

find . -type f | xargs stat --format '%Y :%y %n' | sort -nr | cut -d' ' -f5- | head


find . -type f # find all FILES only in the current directory
xargs stat --format '%Y :%y %n' # perform a stat on the file 
                                # ( xargs helps in 'stringing together commands')
                                # %Y time of last modification since Epoch
                                # %y time of last modification human readable
                                # %n file name
sort -nr                        # -n, sort according to numerical value, -r reverse
cut -d' ' -f5                   # cut output by ' ' (space) and print column five
head                            # show the first 10 lines

答案 1 :(得分:0)

对于OS X的统计数据:

tr \\n \\0<files.txt|xargs -0 stat -f'%m %N'|sort -rn|head -n1

-c'%Y %n'与GNU stat。

一起使用

查找按修改日期排序的文件的不同方法:

find . -type f -exec stat -f'%m %N' {} +|sort -rn|cut -d' ' -f2-
  for OS X's stat; use -c'%Y %n' with GNU stat
gfind -type f -printf '%T@ %p\n'|sort -rn|cut -d' ' -f2-
  %T@ is absolute modification time and %p is pathname
zsh -o dotglob -c 'printf %s\\n **/*(.om)'
  . is a qualifier for regular files
  om orders files by modification time