10

Is it possible to get the modification date and time of a folder?
I know you can use stat -f "%m" folder, but it doesn't reflect sub-files/folders changes.

Things that doesn't work:

  • ls -l folder - doesn't reflect changes inside the folder
  • stat -f "%m" folder - same as above
  • date -r folder - same again
  • find foo bar baz -printf - the printf option doesn't exist on my version of find

Versions of things:

  • OS: Mac OS X 10.7.1
  • Bash: GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • Try the answers that listed here: http://stackoverflow.com/questions/4997242/in-linux-terminal-how-do-i-show-the-folders-last-modification-date-taking-its – Mohammad Aug 24 '11 at 01:40
  • None, of them work as the `printf` option is not implemented in `find` in Mac OS X 10.7.1 – Tyilo Aug 24 '11 at 01:46
  • Are you looking for the most-recently modified file or directory in a particular directory? How recursive do you want to get? – glenn jackman Aug 24 '11 at 01:53
  • I'm more familiar with the GNU/Linux side of things as opposed to BSD flavor. `find -printf` is often useful, but lacking that maybe you could get by with a `ls -lRt | head -n 1` (`-t` for mtime sort, `-R` for a recursive listing) – jw013 Aug 24 '11 at 02:00
  • 1) Yup 2) Around 6 levels deep – Tyilo Aug 24 '11 at 02:00
  • @jw03 Yeah but this sorts it first by parent-directory and not only date/time – Tyilo Aug 24 '11 at 02:02
  • 1
    @Tyilo yes you are right. `ls` isn't really the best way to go about it anyway. Maybe something like `find -execdir stat -f '%m %N' \{} \; | sort -nr | head -n 1` to replace `find -printf`. This example would still need to add null terminators to handle all file names properly but the general idea should work. – jw013 Aug 24 '11 at 02:12
  • @jw013 didn't see your comment before posting my answer, but we found nearly the same solution. Thank you for your help. – Tyilo Aug 24 '11 at 02:16

4 Answers4

13

Solution:

find . -exec stat -f "%m" \{} \; | sort -n -r | head -1

Explanation:

  1. the find command traverses the current directory (.) and for each file encountered executes (-exec) the command stat -f "%m". stat -f "%m" prints the last modification unix timestamp of the file.
  2. sort -n -r sorts the output of the find command numerically (-n) in reverse order (-r). This will list the latest modification timestamp first.
  3. head -1 then extracts the first line of the output from sort. This is the latest modification unix timestamp of all the files.
Tyilo
  • 28,998
  • 40
  • 113
  • 198
  • would be good to get some explanation what each part is doing, this command crashed my computer. – john-jones Dec 27 '14 at 13:38
  • @HermannIngjaldsson Added an explanation. If you're using Linux-based OS and not BSD-based this will probably not work for you. – Tyilo Dec 27 '14 at 19:29
  • This gives wrong information in case someone deletes a file in a folder. Say for example you are trying to maintain an md5 hash for a folder and would like to know when to recompute it. Then this wouldn't work... – Cookie Nov 26 '15 at 16:49
7

You could try 'date -r folder' to give you a date last modified

ChrisK
  • 1,392
  • 10
  • 12
  • 4
    `-r seconds Print the date and time represented by seconds, where seconds is the number of seconds since the Epoch (00:00:00 UTC, January 1, 1970; see time(3)), and can be specified in decimal, octal, or hex.` – Tyilo Aug 24 '11 at 01:39
  • 1
    +1 @Tylio but to clarify - "date -r folder +'%s'" - will return the seconds since epoch look at "date --help" for more information – ChrisK Aug 24 '11 at 01:44
  • 4
    @ChrisK `-r` means reference file in GNU `date` which seems to be what you are referring to while in BSD `date` (the flavor Mac OS X uses), it behaves as quoted in Tyilo's comment – jw013 Aug 24 '11 at 01:56
0

You could always get it from ls :

ls -ld mydir | awk -F' '  '{ print $6 " "$7 }'
JJ.
  • 5,425
  • 3
  • 26
  • 31
0

if you need to clear cache after build . then you can check the age of the last change and delete it like this

sh("find ~/Library/Developer/Xcode/DerivedData/ -type d -maxdepth 1 -mmin +360 -name 'Cache-folder-*' -print0 | xargs -0 -I {} /bin/rm -rf '{}' || echo 'There is no such folder! Start script execution' ; exit 0")



sh("find ~/Library/Developer/Xcode/DerivedData/ -type d -maxdepth 1 -mtime 0 -name 'Cache-folder-*' -ls -exec rm -r {} \\;")