How do I change folder timestamps recursively to the newest file?

You can use touch -r to use another file's timestamp instead of the current time (or touch --reference=FILE)

Here are two solutions. In each solution, the first command changes the modification time of the directory to that of the newest file immediately under it, and the second command looks at the whole directory tree recursively. Change to the directory (cd '.../(1997-05-20) The Colour and The Shape') before running any of the commands.

In zsh (remove the D to ignore dot files):

touch -r *(Dom[1]) .
touch -r **/*(Dom[1]) .

On Linux (or more generally with GNU find):

touch -r "$(find -mindepth 1 -maxdepth 1 -printf '%T+=%p\n' |
            sort |tail -n 1 | cut -d= -f2-)" .
touch -r "$(find -mindepth 1 -printf '%T+=%p\n' |
            sort |tail -n 1 | cut -d= -f2-)" .

However note that those ones assume no newline characters in file names.


That's not "recursively", it's just changing all the timestamps in a folder. If that's what you mean, there's two steps.

stat -c '%Y' filename will output the timestamp of filename, and stat -c '%Y %n' * will output the timestamp and filename of every file in the folder, so this will find the filename of the most recently modified file in the current folder:

mostrecent="`stat -c '%Y %n' * | sort -n | tail -n1 | cut -d ' ' -f '2-'`"

On second thought, there's a way easier way to get the highest timestamp in the folder:

mostrecent="`ls -t | head -n1`"

Then you want to change all the files in the folder to have the same timestamp as that file. touch -r foo bar will change bar to have the same modified timestamp as foo, so this will change all the files in the folder to have the same modified timestamp as your most recently modified file:

touch -r "$mostrecent" *

So, the one-liner is:

touch -r "`ls -t | head -n1`" *

just use

find . -exec touch {} \;