Question

I want it to recursively look through everything in the current directory (/data/trac) and list only the parent items (test, test2, project1) with the timestamp of the newest updated file that is inside each of those directories next to it, and sort it by that timestamp.

Here is the scenario:

$ pwd
$ /data/trac
$ ls -lht
drwxrwxr-x  9 www-data www-data 4.0K Apr 30  2012 test
drwxrwxr-x  9 www-data www-data 4.0K Apr 30  2013 test2
drwxrwxr-x  9 www-data www-data 4.0K Apr 30  2013 project1
$ cd test
$ ls -lht
drwxrwxr-x 2 www-data www-data 4.0K Feb  4 16:12 db
drwxrwxr-x 2 www-data www-data 4.0K Dec 13 13:16 conf
drwxrwxr-x 4 www-data www-data 4.0K Jan 11  2013 attachments
drwxrwxr-x 2 www-data www-data 4.0K Apr 30  2012 templates

We have a directory called "test" which was last updated April 30th 2012. For example, in this case there is a db folder inside that directory which has a file in it which was updated Feb 4th 2014. I want to use this date as the timestamp for the main parent folder "test".

What I want to do is display only the parent folders (test, test2, and project1) sort them by the last updated date (recursively) and display that last updated date.

So the output should be:

$ awesome-list-command
 Feb  4  2014 test
 Feb  2  2014 test2

I have scoured the Internet for hours trying to find this, and even messing about myself to no avail. I have tried:

find . -exec stat -f "%m" \{} \; | sort -n -r | head -1

find $1 -type f | xargs stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head

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

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

stat --printf="%y %n\n" $(ls -tr $(find * -type f))

None of which have worked.

Was it helpful?

Solution

My testcase is a tree like this:

$ tree -t .
.
├── test2
│   └── db
│       ├── foo
│       └── bar
└── test
    └── db
        ├── foo
        └── bar

foo is the newest file in each directory.

#/bin/bash
# awesome-list-command

for dir in */; do
    timestamp=$(find ./$dir -type f -printf "%T@ %t\\n" | sort -nr -k 1,2 | head -n 1)
    printf "%s %s\n" "$timestamp" "$dir"
done | sort -nr -k 1,2 | awk '{$1=""; print}'

Output:

$ ./awesome-list-command
 Tue Feb 4 23:29:41.0766864265 2014 test2/
 Tue Feb 4 23:29:40.0026788568 2014 test/

for comparison:

$ stat -c "%y" test*/db/foo
2014-02-04 23:29:41.766864265 +0100
2014-02-04 23:29:40.026788568 +0100
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top