Purres
Purres

Reputation: 1431

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

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?

Upvotes: 125

Views: 125606

Answers (7)

isapir
isapir

Reputation: 23572

I find this format of -printf to the be the best: %TF %TH:%TM:%.2TS %p\n

It shows the date in YYYY-MM-DD format, followed by the time in HH:MM:SS format, followed by the filename and a new line

For example, this command which only shows directories that were modified since midnight (via the -newermt switch):

find /var -maxdepth 2 -type d -newermt $(date +%Y-%m-%d) -printf '%TF %TH:%TM:%.2TS %p\n'

Returns the following on my machine right now:

2024-07-24 01:05:00 /var/lib/vnstat
2024-07-24 00:39:29 /var/lib/snapd
2024-07-24 00:59:47 /var/lib/smartmontools
2024-07-24 00:38:40 /var/lib/logrotate
2024-07-24 00:38:40 /var/backups
2024-07-24 00:38:40 /var/cache/cups
2024-07-24 00:38:40 /var/log
2024-07-24 00:38:40 /var/log/cups
2024-07-24 00:39:20 /var/tmp

Upvotes: 1

Mr. Llama
Mr. Llama

Reputation: 20909

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"

Upvotes: 30

user666
user666

Reputation: 1172

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 

Upvotes: 11

Mark
Mark

Reputation: 607

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.

Upvotes: 19

kzar
kzar

Reputation: 3209

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"

Upvotes: 198

rc0r
rc0r

Reputation: 19327

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.

Upvotes: 80

Kent
Kent

Reputation: 195209

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

Upvotes: 3

Related Questions