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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:57:30  来源:igfitidea点击:

Get folder (or sub-files/folders) last modification date and time

macosbash

提问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 folder
  • stat -f "%m" folder- same as above
  • date -r folder- same again
  • find foo bar baz -printf- the printfoption 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:

解释:

  1. the findcommand traverses the current directory (.) and for each file encountered executes (-exec) the command stat -f "%m". stat -f "%m"prints the last modification unix timestamp of the file.
  2. sort -n -rsorts the output of the findcommand numerically (-n) in reverse order (-r). This will list the latest modification timestamp first.
  3. head -1then extracts the first line of the output from sort. This is the latest modification unix timestamp of all the files.
  1. find命令遍历当前目录 ( .) 并对遇到的每个文件执行 ( -exec) 命令stat -f "%m"stat -f "%m"打印文件的最后修改 unix 时间戳。
  2. sort -n -r以相反的顺序 ( )按find数字 ( -n)对命令的输出进行排序-r。这将首先列出最新的修改时间戳。
  3. 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  " " }'