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

122,564

Solution 1

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

Solution 2

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" *

Solution 3

If I could, I would vote for the answer by Paulo. I tested it and understood the concept. I can confirm it works. The find command can output many parameters. For example, add the following to the --printf clause:

%a for attributes in the octal format
%n for the file name including a complete path

Example:

find Desktop/ -exec stat \{} --printf="%y %n\n" \; | sort -n -r | head -1
2011-02-14 22:57:39.000000000 +0100 Desktop/new file

Let me raise this question as well: Does the author of this question want to solve his problem using Bash or PHP? That should be specified.

Solution 4

It seems to me that simply: ls -lt mydirectory does the job...

Share:
122,564

Related videos on Youtube

Frantisek
Author by

Frantisek

An aspiring writer hoping to one day write a video game.

Updated on July 05, 2022

Comments

  • Frantisek
    Frantisek almost 2 years

    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?

  • Frantisek
    Frantisek about 13 years
    Good question, I thought it was obvious though, as my tags say "bash, scripting, shell". I want to solve this problem using Bash, yes. It's for backing up server files. I think the problem is solved now, the solutions work perfect :)
  • sappjw
    sappjw over 11 years
    According to the coreutils documentation, you might consider head -n 1 or sed 1q instead of the obsolete syntax.
  • Paulo Scardine
    Paulo Scardine about 9 years
    So head -1 to return just the first line is now obsolete... Man, I remember when it used to be the new flag - I'm that old!!! :o)
  • Ternary
    Ternary over 8 years
    This is such a great example of the strength and weakness of the linux command line.
  • IQAndreas
    IQAndreas about 8 years
    In most cases, I would add the -type f flag, so it only checks the modification time of files. I can't remember which program it was I used, but when I copied files from one location to another, it kept the timestamps of the files, but marked the directories as brand new, throwing off the results.
  • tripleee
    tripleee over 6 years
    You seem to be using both stat and the built-in -printf flag of find, and then basically throwing away one of the results. I guess you should prefer the latter over the former, as outlined in a separate answer (no need to spawn a separate process for each file when find already does it all in one process).
  • dshaw
    dshaw over 3 years
    It does not, but it's easy to think that. ls -t (or the --time option) on a directory will show when that directory was created, but it will not reflect the last-modified time of files within that directory.
  • Rodolfo Medina
    Rodolfo Medina over 3 years
    `ls -lt mydirectory' shows to me the last-modified time of files and subdirs within that dir...
  • yagus
    yagus almost 3 years
    This only works for files inside mydirectory, not for files inside any subdir in mydirectory. See this discussion.
  • EugZol
    EugZol over 2 years
    What a user-friendly OS