Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in linux terminal, how do I show the folder's last modification date, taking its content into consideration?

So here's the deal. Let's say I have a directory named "web", so

$ ls -la  drwx------  4 rimmer rimmer 4096 2010-11-18 06:02 web 

BUT inside this directory, web/php/

$ ls -la  -rw-r--r-- 1 rimmer rimmer 1957 2011-01-05 08:44 index.php 

That means that even though the content of my directory, /web/php/index.php has been last modified at 2011-01-05, the /web/ directory itself is reported as last modified at 2010-11-18.

What I need to do is have my /web/ directory's last modification date reported as the latest modification date of any file/directory inside this directory, recursively.

How do I go about doing this?

like image 534
Frantisek Avatar asked Feb 14 '11 21:02

Frantisek


People also ask

How do you check when was the file last modified Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.

Which command gives information about time of last modification?

ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.

How can I tell when a file was edited Linux?

Use “-mtime n” command to return a list of files that were last modified “n” hours ago. See the format below for a better understanding. -mtime +10: This will find all files that were modified 10 days ago. -mtime -10: It will find all files that were modified in the last 10 days.


2 Answers

Something like:

find /path/ -type f -exec stat \{} --printf="%y\n" \; |       sort -n -r |       head -n 1 

Explanation:

  • the find command will print modification time for every file recursively ignoring directories (according to the comment by IQAndreas you can't rely on the folders timestamps)
  • sort -n (numerically) -r (reverse)
  • head -n 1: get the first entry
like image 84
Paulo Scardine Avatar answered Oct 07 '22 04:10

Paulo Scardine


If you have a version of find (such as GNU find) that supports -printf then there's no need to call stat repeatedly:

find /some/dir -printf "%T+\n" | sort -nr | head -n 1 

or

find /some/dir -printf "%TY-%Tm-%Td %TT\n" | sort -nr | head -n 1 

If you don't need recursion, though:

stat --printf="%y\n" * 
like image 37
Dennis Williamson Avatar answered Oct 07 '22 02:10

Dennis Williamson