bash 获取文件夹(或子文件/文件夹)上次修改日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7169509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get folder (or sub-files/folders) last modification date and time
提问by Tyilo
Is it possible to get the modification date and time of a folder?
I know you can use stat -f "%m" folder
, but it doesn't reflect sub-files/folders changes.
是否可以获取文件夹的修改日期和时间?
我知道您可以使用stat -f "%m" folder
,但它不反映子文件/文件夹的更改。
Things that doesn't work:
不起作用的事情:
ls -l folder
- doesn't reflect changes inside the folderstat -f "%m" folder
- same as abovedate -r folder
- same againfind foo bar baz -printf
- theprintf
option doesn't exist on my version of find
ls -l folder
- 不反映文件夹内的更改stat -f "%m" folder
- 同上date -r folder
- 又一样find foo bar baz -printf
-printf
我的 find 版本中不存在该选项
Versions of things:
事物的版本:
- OS: Mac OS X 10.7.1
- Bash: GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
- 操作系统:Mac OS X 10.7.1
- Bash:GNU bash,版本 3.2.48(1)-release (x86_64-apple-darwin11)
回答by Tyilo
Solution:
解决方案:
find . -exec stat -f "%m" \{} \; | sort -n -r | head -1
Explanation:
解释:
- the
find
command traverses the current directory (.
) and for each file encountered executes (-exec
) the commandstat -f "%m"
.stat -f "%m"
prints the last modification unix timestamp of the file. sort -n -r
sorts the output of thefind
command numerically (-n
) in reverse order (-r
). This will list the latest modification timestamp first.head -1
then extracts the first line of the output fromsort
. This is the latest modification unix timestamp of all the files.
- 该
find
命令遍历当前目录 (.
) 并对遇到的每个文件执行 (-exec
) 命令stat -f "%m"
。stat -f "%m"
打印文件的最后修改 unix 时间戳。 sort -n -r
以相反的顺序 ( )按find
数字 (-n
)对命令的输出进行排序-r
。这将首先列出最新的修改时间戳。head -1
然后从 中提取输出的第一行sort
。这是所有文件的最新修改unix时间戳。
回答by ChrisK
You could try 'date -r folder' to give you a date last modified
你可以试试'date -r folder'给你一个上次修改的日期
回答by JJ.
You could always get it from ls
:
你总是可以从ls
:
ls -ld mydir | awk -F' ' '{ print " " }'