Linux 如何使用“查找”命令显示修改后的日期时间?

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

How to display modified date time with 'find' command?

linuxcommand-linegrepfindstat

提问by Purres

With a findcommand, I can display directories names with multiple levels. The following command display all directories under /varpath with a depth of 2:

使用find命令,我可以显示多个级别的目录名称。以下命令显示/var路径下深度为 2 的所有目录:

find /var -maxdepth 2 -type d;

The result shows:

结果显示:

/var
/var/log
/var/log/sssd
/var/log/samba
/var/log/audit
/var/log/ConsoleKit
/var/log/gdm
/var/log/sa

With a statcommand, I can find the modified date time:

使用stat命令,我可以找到修改后的日期时间:

stat /var/log/samba | grep 'Modify:'

The result is:

结果是:

Modify: 2014-01-02 11:21:27.762346214 -0800 

Is there a way to combine the two commands so that directories will be listed with modified date time?

有没有办法组合这两个命令,以便列出带有修改日期时间的目录?

采纳答案by rc0r

You could use the -execswitch for findand define the output format of statusing the -cswitch as follows:

您可以使用-exec的开关find和定义的输出格式stat使用-c如下开关:

find /var -maxdepth 2 -type d -exec stat -c "%n %y" {} \;

find /var -maxdepth 2 -type d -exec stat -c "%n %y" {} \;

This should give the filename followed by its modification time on the same line of the output.

这应该在输出的同一行给出文件名,后跟其修改时间。

回答by Kent

try this line:

试试这一行:

find /var -maxdepth 2 -type d|xargs stat|grep -E 'File|Modi'

here I ran it, it outputs:

我在这里运行它,它输出:

....
  File: ‘/var/cache/cups'
Modify: 2013-12-24 00:42:59.808906421 +0100
  File: ‘/var/log'
Modify: 2014-01-01 12:41:50.622172106 +0100
  File: ‘/var/log/old'
Modify: 2013-05-31 20:40:23.000000000 +0200
  File: ‘/var/log/journal'
Modify: 2013-12-15 18:56:58.319351603 +0100
  File: ‘/var/log/speech-dispatcher'
Modify: 2013-10-27 01:00:08.000000000 +0200
  File: ‘/var/log/cups'
Modify: 2013-12-22 00:49:52.888346088 +0100
  File: ‘/var/opt'
Modify: 2013-05-31 20:40:23.000000000 +0200
....

回答by kzar

The accepted answer works but it's slow. There's no need to exec stat for each directory, find provides the modification date and you can just print it out directly. Here's an equivalent command that's considerably faster:

接受的答案有效,但速度很慢。不需要为每个目录都执行 stat,find 提供修改日期,您可以直接打印出来。这是一个相当快的等效命令:

 find /var -maxdepth 2 -type d -printf "%p %TY-%Tm-%Td %TH:%TM:%TS %Tz\n"

回答by Mark

find /var -maxdepth 2 -type d | xargs ls -oAHd

find /var -maxdepth 2 -type d | xargs ls -oAHd

This is a way to get your basic lscommand to display the full directory path. While lshas the -Rparameter for recursive search, paths won't be displayed in the results with the -lor -ooption (in OSX, at least), for ex with: ls -lR.

这是一种让您的基本ls命令显示完整目录路径的方法。虽然ls具有-R递归搜索的参数,但路径不会显示在带有-lor-o选项的结果中(至少在 OSX 中),例如:ls -lR

回答by user666

Another one that I use to print modified files in last day . ls -ltr gives me more detailed like modification time , user etc

我在最后一天用来打印修改过的文件的另一个。ls -ltr 为我提供了更详细的信息,例如修改时间、用户等

find <my_dir> -mtime -1 -type f -print | xargs ls -ltr 

回答by Mr. Llama

Recent GNU versions of findalso include a -printfoption which includes date fields. If you need to print the file's name and modification time in the standard "C" format, you can use -printf "%c %p\n".

最近的 GNU 版本find还包括一个-printf包含日期字段的选项。如果需要以标准的“C”格式打印文件名和修改时间,可以使用-printf "%c %p\n".

If you want the date in a specific format, you can use the %Cfollowed by a field character. For example, 4-digit year would be %CY, with Ybeing the character for 4-digit year.
Note that if you need multiple fields, you'll need to specify %Cmultiple times. For example, YYYY-MM-DD format would look like %CY-%Cm-%Cd.

如果需要特定格式的日期,可以使用%C后跟字段字符。例如,4位年会%CY,同Y为4位数字年份的字符。
请注意,如果您需要多个字段,则需要多次指定%C。例如,YYYY-MM-DD 格式看起来像%CY-%Cm-%Cd.

Check the man pages or online documentationfor additional details.

查看手册页或在线文档以获取更多详细信息。

Here is a working example:

这是一个工作示例:

find . -name favicon.ico -printf "%c %p\n"