Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get folder (or sub-files/folders) last modification date and time

Tags:

bash

macos

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)
like image 672
Tyilo Avatar asked Aug 24 '11 01:08

Tyilo


2 Answers

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.
like image 159
Tyilo Avatar answered Oct 13 '22 01:10

Tyilo


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

like image 39
ChrisK Avatar answered Oct 13 '22 00:10

ChrisK