Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there any way to get the last modified date of a directory, including sub directories? either via shell script or php?

Thanks!

share|improve this question
1  
What exactly are you after: the last time a file was added or removed from the directory tree? Or the last time a file in the directory tree was modified? Try to explain what you want as precisely as you can, give examples if necessary. –  Gilles Aug 13 '10 at 19:37
    
@Gilles: the last time any file in a directory tree has been modified is what I'm after. the single most recently modified date/time from inside the directory tree. –  dcmoody Aug 13 '10 at 21:34
add comment

5 Answers

up vote 0 down vote accepted

Here's a shell solution that assumes Linux (or more generally GNU find). For simplicity's sake, file names containing newlines are not supported.

find /path/to/directory -type f -printf '%T@:%p\n' |
sort -t : -k 1 -nr |
head -n 1 |
sed -e 's/^[^:]*://'

Quick explanation:
  • List all the regular files in the tree; for each file, print its modification time, a colon and the name.
  • Sort by decreasing leading number.
  • Keep only the first line.
  • Strip the leading number and :.

share|improve this answer
add comment

The Unix "stat" command will provide this info, as well as the PHP function by the same name.

share|improve this answer
    
find -type d | xargs stat --printf='Dir:%n\tModified:%y\n' should do the trick; uses the stat command with a list of DIR's from find. –  GruffTech Aug 15 '10 at 16:00
add comment

i 'm not sure what you are asking, but have you tried ls -la ?

or even ... find ./ -exec ls -lah {} \; to see the last date of everything.

(It can be deployed in *nix systems)

share|improve this answer
    
see my comment @Gilles on the original question for more clarification. –  dcmoody Aug 13 '10 at 21:35
add comment

A shell script sounds like it might complicate this more then having to, however forgive me if I am not fully understanding the question.

tree would be the command I would use for this

syntax tree -dfsD

share|improve this answer
add comment

If you are wanting to see a list of all directories/subdirectories (and not any files) in a location sorted by (and showing) their last modified date, you can use the following command:

ls -lRt | grep ^d

Update: You could take it a step further and limit the output to just the last modified date and name of the directory by using

ls -lRt | grep ^d | awk '{print $6" "$7" "$8, $9}'

The only problem with either of these listings is that you don't get a full path for each directory, so it can be difficult to tell what directories are subdirectories.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.