110

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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Purres
  • 1,271
  • 2
  • 11
  • 12

6 Answers6

175

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"
kzar
  • 2,981
  • 2
  • 17
  • 13
  • 5
    A much better solution. It also works with the `find` from msys running on Windows, which lacks a `stat` command. – Snorfalorpagus Oct 29 '14 at 10:02
  • 10
    On macs, 'brew install findutils' and then gfind has the -printf option. – WeakPointer Mar 29 '17 at 15:07
  • 1
    `%Tc` prints "locales's date and time", allowing the format string "%p %Tc" – Andreas May 11 '18 at 11:28
  • 1
    More helpful links for explanation of % formatters. https://unix.stackexchange.com/a/215236/216480 or on the [man page](http://man7.org/linux/man-pages/man1/find.1.html) search for "-printf format" – styks May 18 '18 at 15:05
  • 1
    While the format string "%p %Tc" does work, it formats the output slightly differently. E.g. "/var/spool Mon 29 Sep 2014 09:05:54 BST" instead of "/var/spool 2014-09-29 09:05:54.000000000 +0100". – kzar May 24 '18 at 09:42
  • Documentation on more `printf` date/time formats: `man find | less -p "Time fields"` – wisbucky Mar 07 '19 at 23:06
  • To truncate the fractional part of the seconds, use `%.2TS` instead of `%TS` – wisbucky Mar 08 '19 at 00:47
  • Another advantage of this solution is that it is easy to separate the name from the date fields with a tab using \t. This is necessary for importing into a spreadsheet when some file names have space characters. – atmelino Oct 17 '20 at 01:28
77

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.

rc0r
  • 18,631
  • 1
  • 16
  • 20
  • 1
    it's better than my answer, +1 – Kent Jan 02 '14 at 23:11
  • The `-printf` option below avoids calling `stat` for every file found. In my test the command yields almost identical output, just an extra digit's precision on the seconds. – mwfearnley Nov 28 '18 at 11:27
  • 5
    For MacOS the format arg character for `stat` is `-f`. `find /var -maxdepth 2 -type d -exec stat -f "%t%Sm %N" {} \;` – toddcscar Jul 28 '20 at 00:47
24

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"
David Brossard
  • 13,584
  • 6
  • 55
  • 88
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • `%c` is the last _change_ time, so you may need to use `%t` for the last _modification_ time (mtime). These are two different timestamps in the file metadata - change time relates to changes to the inode (permissions, ownership, filename, etc), and mtime relates to changes to the contents of the file. – davidA Aug 31 '23 at 02:17
18

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.

Mark
  • 597
  • 5
  • 8
  • Please add an explanation for why this answers the question. In general, any time you post example code in an answer on StackOverflow, the culture is to always provide an explanation for it. To do otherwise risks having the moderators delete your answer as a low-quality answer due to its short length and lack of explanation. – sideshowbarker Sep 12 '15 at 03:29
  • 4
    This one you can achieve with `find /var -maxdepth 2 -type d -ls` which is just simpler. – Grzegorz Krauze Jan 22 '17 at 12:09
9

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 
user666
  • 1,104
  • 12
  • 20
  • 1
    This is the simplest, easiest to remember, and quickest to type. (I use it without the -mtime and -type args, instead using a `-name '*.ext'` specification. `ls -lt` sorts on the modification time.) – Brent Faust Nov 19 '18 at 07:44
  • 2
    This doesn't work when the filename contains spaces. Better is to use `-print0` instead of `-print` and then use `xargs -0` instead of `xargs`. – emk2203 Jan 03 '19 at 11:49
  • `xargs` may invoke the command (`ls` here) several times, depending on the number of parameters, to prevent the command from being invoked with an excessive number of parameters. Therefore, sorting (whatever the criterion, including by date) may give a "wrong" result (=> list of sorted subsets, instead of the full set sorted). – syme Jan 10 '21 at 12:44
3

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
....
Kent
  • 189,393
  • 32
  • 233
  • 301