Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I get the last time any of the files in a directory or its subdirectories has changed? e.g

Dir - changed 1/1/1
    Sub Dir 1 - changed 2/1/1
        Sub Dir 2 - changed 3/1/1
            File 1 - changed 10/1/1
    File 2 - change 5/1/1

The output for this for Dir should be 10/1/1 (File 1 was the last modified one). Getting the last file name to be modified is a bonus but isn't necessary.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

find <dir> -type f -printf '%T@\t%p\n' | sort -r -k1 | head -n1 returns a line in the form:

<seconds since epoch[1]><tab><filename>

Alias or put in a script like in the following example to print file name or date.


Let's create a testing tree:

$ date -u; mkdir -p a/{b,c,d}/{e,f,g}
Sat May 28 17:37:52 UTC 2011

$ date -u; touch a/{b,c,d}/{e,f,g}/{foo,bar,baz}; sleep 1; date -u; touch a/c/f/bar
Sat May 28 17:38:17 UTC 2011
Sat May 28 17:38:18 UTC 2011

Get date of a:

$ date -ud @$(find a -type f -printf '%T@\t%p\n' | sort -r -k1 | head -n1 | cut -f1)
Sat May 28 17:38:18 UTC 2011

Get path of the file:

$ find a -type f -printf '%T@\t%p\n' | sort -r -k1 | head -n1 | cut -f2
a/c/f/bar

[1]: See Unix time on Wikipedia for an explanation of "Unix epoch".


Script example:

#!/bin/sh
if test ! -d "${1:-.}"
then
    echo not a directory: ${1:-.} >&2
    exit 1
fi
date -d @$(find "${1:-.}" -type f -printf '%T@\t%p\n' | sort -r -k1 | head -n1 | cut -f1)

Call with or without <dir> as argument. It will use the current directory without.

share|improve this answer
    
... and in zsh you might do ls -l <dir>/**/*(.om[1]). –  user112553 Oct 20 '13 at 11:53

I use FAR Manager to do something similar to this that should solve your problem:

  FAR Manager (free and open source)
  http://www.farmanager.com/

Here's how I do it:

  • Navigate to the directory I want to start searching from
  • Press Alt-F7 (to bring up the Search dialogue)
  • Search for *.* (all files)
  • Choose "Panel" after the search is complete
  • Press F9 then L then L again to show full file listing details
  • Press CTRL-F5 to sort everything by Modification Timestamp

The "Panel" places all search results in one long sortable list. You have other sorting options too (holding the CTRL key will reveal these to you along the bottom of the screen), and pressing the same sort key combination again will cause sorting to work in the opposite direction (you can keep switching between Ascending and Descending this way).

Although the directory itself may not have the newest timestamp, at least you'll be able to find the newest file this way and then you'll see the full path in the listing. Hopefully that will help you to get the information you need?

enter image description here

share|improve this answer
    
Thanks to @peth to pointing out that this is about Linux. If FAR Manager doesn't work on Linux for you, there's always Midnight Commander which I believe should provide similar functionality since it is, like FAR Manager, also inspired by Norton Commander. –  Randolf Richardson May 28 '11 at 16:04

In DOS this command might work for you, albeit not perfectly due to inherent limitations with DOS's DIR command:

  • DIR /A /OD /S /TW

The /TW switch is the most interesting to you because it means "show last written-to date" (which is as close as I could find with "DIR /?" to a last-modified date).

The /OD switch causes output to be sorted by date. The DIR command should pick up on the fact that you're using the /TW switch and sort by this date automatically.

The /S switch enables recursively searching in subdirectories.

The /A switch includes all types of files, including Hidden files. If you don't want to include Hidden [and other such] files, then omit this switch.

share|improve this answer
    
@peth: Whoops! I missed the tag (I wonder if it was added later?), and the word "Dir" threw me off onto the DOS track. –  Randolf Richardson May 28 '11 at 16:02

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.