寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
寻找返回目录中最新文件的命令。
没有看到ls的limit参数…
当前回答
如果你想在/apps/test目录中找到最后修改的文件夹名称,那么你可以在批处理脚本中放入下面的代码片段并执行它,它将打印最后修改的文件夹名称。
#!/bin/bash -e
export latestModifiedFolderName=$(ls -td /apps/test/*/ | head -n1)
echo Latest modified folder within /apps/test directory is $latestModifiedFolderName
其他回答
我个人更喜欢使用尽可能少的非内置bash命令(以减少昂贵的fork和exec系统调用的数量)。要按日期排序,需要调用ls。但使用头部是没有必要的。我使用以下一行代码(只适用于支持名称管道的系统):
read newest < <(ls -t *.log)
或者获取最古老的文件的名称
read oldest < <(ls -rt *.log)
(注意两个“<”符号之间的空格!)
如果还需要隐藏文件,可以添加一个参数。
我希望这能有所帮助。
ls -t | head -n1
这个命令实际上给出了当前工作目录中最新修改的文件或目录。
ls -Art | tail -n 1
这将返回最新修改的文件或目录。不是很优雅,但是很好用。
使用国旗:
-A列出所有文件。和. .
-r在排序时倒序
-t按时间排序,latest在前
这是一个递归版本(即,它在某个目录或其任何子目录中查找最近更新的文件)
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 -Aru | tail -n 1
如果你想要最后一个文件的内容有变化(修改时间):
ls -Art | tail -n 1