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

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

find /var -maxdepth 2 -type d;

结果显示:

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

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

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

结果是:

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

有没有一种方法可以组合这两个命令,以便目录将被列出与修改的日期时间?

101491 次浏览

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
....

You could use the -exec switch for find and define the output format of stat using the -c switch as follows:

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.

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:

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

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

This is a way to get your basic ls command to display the full directory path. While ls has the -R parameter for recursive search, paths won't be displayed in the results with the -l or -o option (in OSX, at least), for ex with: ls -lR.

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

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

Recent GNU versions of find also include a -printf option 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".

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

Check the man pages or online documentation for additional details.

Here is a working example:

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