How to display modified date time with 'find&#

2019-01-31 16:19发布

问题:

With a find command, I can display directories names with multiple levels. The following command display all directories under /var path with a depth of 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 stat command, I can find the modified date time:

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?

回答1:

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.



回答2:

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"


回答3:

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.



回答4:

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 


回答5:

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"


回答6:

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