在 linux 终端中,如何显示文件夹的上次修改日期,同时考虑其内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4997242/
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-08-04 00:20:56  来源:igfitidea点击:

in linux terminal, how do I show the folder's last modification date, taking its content into consideration?

linuxbashshellscriptingubuntu

提问by Richard Rodriguez

So here's the deal. Let's say I have a directory named "web", so

所以这就是交易。假设我有一个名为“web”的目录,所以

$ ls -la

drwx------  4 rimmer rimmer 4096 2010-11-18 06:02 web

BUT inside this directory, web/php/

但是在这个目录中,web/php/

$ ls -la

-rw-r--r-- 1 rimmer rimmer 1957 2011-01-05 08:44 index.php

That means that even though the content of my directory, /web/php/index.php has been last modified at 2011-01-05, the /web/ directory itself is reported as last modified at 2010-11-18.

这意味着即使我的目录 /web/php/index.php 的内容最后一次修改是在 2011-01-05,但 /web/ 目录本身被报告为最后一次修改是在 2010-11-18。

What I need to do is have my /web/ directory's last modification date reported as the latest modification date of any file/directory inside this directory, recursively.

我需要做的是将我的 /web/ 目录的最后修改日期报告为该目录中任何文件/目录的最新修改日期,递归。

How do I go about doing this?

我该怎么做?

采纳答案by Paulo Scardine

Something like:

就像是:

find /path/ -type f -exec stat \{} --printf="%y\n" \; | 
     sort -n -r | 
     head -n 1

Explanation:

解释:

  • the find command will print modification time for every file recursively ignoring directories (according to the comment by IQAndreas you can't rely on the folders timestamps)
  • sort -n (numerically) -r (reverse)
  • head -n 1: get the first entry
  • find 命令将递归地忽略目录打印每个文件的修改时间(根据 IQAndreas 的评论,您不能依赖文件夹时间戳)
  • sort -n(数字) -r(反向)
  • head -n 1:获取第一个条目

回答by Martian

If I could, I would vote for the answer by Paulo. I tested it and understood the concept. I can confirm it works. The findcommand can output many parameters. For example, add the following to the --printfclause:

如果可以,我会投票支持保罗的答案。我测试了它并理解了这个概念。我可以确认它有效。该find命令可以输出很多参数。例如,在--printf子句中添加以下内容:

%a for attributes in the octal format
%n for the file name including a complete path

Example:

例子:

find Desktop/ -exec stat \{} --printf="%y %n\n" \; | sort -n -r | head -1
2011-02-14 22:57:39.000000000 +0100 Desktop/new file

Let me raise this question as well: Does the author of this question want to solve his problem using Bash or PHP? That should be specified.

我也提出这个问题:这个问题的作者是想用Bash还是PHP来解决他的问题?这应该被指定。

回答by Paused until further notice.

If you have a version of find(such as GNU find) that supports -printfthen there's no need to call statrepeatedly:

如果您有一个支持的版本find(例如 GNU find),-printf则无需stat重复调用:

find /some/dir -printf "%T+\n" | sort -nr | head -n 1

or

或者

find /some/dir -printf "%TY-%Tm-%Td %TT\n" | sort -nr | head -n 1

If you don't need recursion, though:

但是,如果您不需要递归:

stat --printf="%y\n" *