I'd like to clean up a server that my webmaster let turn into a mess.

I know how to list all files not accessed within the last x days using find and -atime, but what I'm looking for is to come up with a listing of the last access date for files one level down in directory /foo:

/foo/bar1.txt Dec 11, 2001
/foo/bar2.txt Nov 12, 2008
/foo/bar3.txt Jan 12, 2004

For folders one level down in directory /foo, list the date of the most recently accessed file within the directory (no limit on depth for identifying last access date)

/foo/bar1/ Feb 13, 2012
/foo/bar2/ Oct 11, 2008

Where /foo/bar1/ has a file modified Jan 1, 1998 and Feb 13, 2012 and /foo/bar2/ has 30 files, most recent of which was accessed Oct 11, 2008.

This question is similar to: https://stackoverflow.com/questions/5566310/how-to-recursively-find-and-list-the-latest-modified-files-in-a-directory-with-s but rather than the modification date, the date of interest is the last accessed date.

  • 1
    Was the fs mounted with the "noatime" option? If so, you're sunk.
    – EEAA
    Commented Nov 12, 2012 at 4:07
  • Nay. I was able to list individual files earlier/later than x days using find & -atime.
    – kayaker243
    Commented Nov 13, 2012 at 1:52

1 Answer 1

find /foo/* \( -type d ! -name . -prune \) -exec ls -lu {} \;

find /foo/* \( -type d ! -name . -prune \) | 
while read dir
    find $dir -type f -exec  stat -c "%X %n %x" {} \; |
          sort -rn | head -1 | awk '{print $2, $3, $4}'

Try those.

  • Your script fails in directories that have files with spaces in them. Commented May 26, 2018 at 7:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .