fre*_*rik 390 linux filesystems recursion time
操作系统:Linux
文件系统类型:ext3
首选解决方案:bash(script/oneliner),ruby,python
我有几个目录,其中包含几个子目录和文件.我需要列出所有这些目录,这些目录的构造方式使得每个第一级目录都列在其中最新创建/修改的文件的日期和时间旁边.
为了澄清,如果我触摸文件或将其内容修改为几个子目录级别,那么该时间戳应该显示在第一级目录名称旁边.假设我有一个像这样结构化的目录:
./alfa/beta/gamma/example.txt
Run Code Online (Sandbox Code Playgroud)
我修改了文件的内容example.txt,我需要alfa以人类可读的形式显示在第一级目录旁边的时间,而不是epoch.我试着用find,有些事xargs,sort和喜欢,但我不能解决该问题得到,当我创建/修改文件几级向下"阿尔法"的文件系统时间戳不会改变.
小智 453
试试这个:
#!/bin/bash
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
Run Code Online (Sandbox Code Playgroud)
与路径,它应该递归开始扫描(它支持文件名带空格)的目录执行它.
如果有很多文件,它可能需要一段时间才能返回任何内容.如果我们改用,性能可以提高xargs:
#!/bin/bash
find $1 -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head
Run Code Online (Sandbox Code Playgroud)
这有点快.
ima*_*man 182
要在N分钟前找到上次更改文件状态的所有文件:
find -cmin -N
例如:
find -cmin -5
小智 38
GNU Find(请参阅参考资料man find)有一个-printf参数,用于显示文件EPOC mtime和相对路径名.
redhat> find . -type f -printf '%T@ %P\n' | sort -n | awk '{print $2}'
Run Code Online (Sandbox Code Playgroud)
sla*_*tir 34
我缩短了光环对这个单线的真棒答案
stat --printf="%y %n\n" $(ls -tr $(find * -type f))
Run Code Online (Sandbox Code Playgroud)
更新:如果文件名中有空格,则可以使用此修改
OFS="$IFS";IFS=$'\n';stat --printf="%y %n\n" $(ls -tr $(find . -type f));IFS="$OFS";
Run Code Online (Sandbox Code Playgroud)
Dan*_*mer 17
试试这个
#!/bin/bash
stat --format %y $(ls -t $(find alfa/ -type f) | head -n 1)
Run Code Online (Sandbox Code Playgroud)
它用于find收集目录中的所有文件,ls列出按修改日期排序的head文件,用于选择第一个文件,最后stat以漂亮的格式显示时间.
此时,对于名称中包含空格或其他特殊字符的文件,这是不安全的.如果它还不能满足您的需求,请写一个表扬.
Jim*_*unt 10
此命令适用于Mac OS X:
find "$1" -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head
在Linux上,正如原始海报所要求的那样,使用stat而不是gstat.
当然,这个答案是user37078的杰出解决方案,从评论推广到完整答案.我把CharlesB的洞察力混合在gstatMac OS X 上使用.顺便说一句,我从MacPorts获得了coreutils而不是自制软件.
以下是我将其打包成一个简单~/bin/ls-recent.sh的重用命令的方法:
#!/bin/bash
# ls-recent: list files in a dir tree, most recently modified first
#
# Usage: ls-recent path [-10 | more]
#
# Where "path" is a path to target directory, "-10" is any arg to pass
# to "head" to limit the number of entries, and "more" is a special arg
# 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
Run Code Online (Sandbox Code Playgroud)
这篇文章中的perl和Python解决方案都帮助我在Mac OS X上解决了这个问题:https://unix.stackexchange.com/questions/9247/how-to-list-files-sorted-by-modification-date-recursively -no-stat-command-avail.
从帖子引用:
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
}'
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
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'
Run Code Online (Sandbox Code Playgroud)
这是一个适用于可能包含空格、换行符和全局字符的文件名的版本:
find . -type f -printf "%T@ %p\0" | sort -zk1nr
Run Code Online (Sandbox Code Playgroud)
find ... -printf打印文件修改时间(纪元值),后跟空格和\0终止文件名。sort -zk1nr读取以 NUL 结尾的数据并按数字反向排序由于问题是用 Linux 标记的,我假设GNU 核心实用程序可用。
您可以使用以下命令对上述内容进行管道传输:
xargs -0 printf "%s\n"
Run Code Online (Sandbox Code Playgroud)
打印修改时间和按修改时间排序的文件名(最近的在前),以换行符结尾。
很好地处理文件名中的空格-并非您应该使用它们!
$ find . -type f -not -path '*/\.*' -printf '%TY.%Tm.%Td %THh%TM %Ta %p\n' |sort -nr |head -n 10
2017.01.28 07h00 Sat ./recent
2017.01.21 10h49 Sat ./hgb
2017.01.16 07h44 Mon ./swx
2017.01.10 18h24 Tue ./update-stations
2017.01.09 10h38 Mon ./stations.json
Run Code Online (Sandbox Code Playgroud)
这就是我正在使用的(非常有效):
function find_last () { find "${1:-.}" -type f -printf '%TY-%Tm-%Td %TH:%TM %P\n' 2>/dev/null | sort | tail -n "${2:-10}"; }
Run Code Online (Sandbox Code Playgroud)
优点:
用法:
find_last [dir [number]]
Run Code Online (Sandbox Code Playgroud)
在哪里:
dir - 要搜索的目录 [当前目录]number - 要显示的最新文件数 [10]输出find_last /etc 4如下所示:
2019-07-09 12:12 cups/printers.conf
2019-07-09 14:20 salt/minion.d/_schedule.conf
2019-07-09 14:31 network/interfaces
2019-07-09 14:41 environment
Run Code Online (Sandbox Code Playgroud)