寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
当前回答
递归地:
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
其他回答
查找/排序解决方案工作得很好,直到文件数量变得非常大(比如整个文件系统)。使用awk来跟踪最近的文件:
find $DIR -type f -printf "%T@ %p\n" |
awk '
BEGIN { recent = 0; file = "" }
{
if ($1 > recent)
{
recent = $1;
file = $0;
}
}
END { print file; }' |
sed 's/^[0-9]*\.[0-9]* //'
递归地:
find $1 -type f -exec stat --format '%Y :%y %n' "{}" \; | sort -nr | cut -d: -f2- | head
使用R递归选项..你可以认为这是对好的答案的增强
ls -arRtlh | tail -50
这是一个递归版本(即,它在某个目录或其任何子目录中查找最近更新的文件)
find /dir/path -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1
命令行简单的外行解释:
find /dir/path -type f finds all the files in the directory -printf "%T@ %p\n" prints a line for each file where %T@ is the float seconds since 1970 epoch and %p is the filename path and \n is the new line character for more info see man find | is a shell pipe (see man bash section on Pipelines) sort -n means to sort on the first column and to treat the token as numerical instead of lexicographic (see man sort) cut -d' ' -f 2- means to split each line using the character and then to print all tokens starting at the second token (see man cut) NOTE: -f 2 would print only the second token tail -n 1 means to print the last line (see man tail)
ls -Art | tail -n 1
这将返回最新修改的文件或目录。不是很优雅,但是很好用。
使用国旗:
-A列出所有文件。和. .
-r在排序时倒序
-t按时间排序,latest在前