Linux – List files with last access date in linux

bashlinuxshell

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.

Best Answer

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


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

Try those.