I was wondering if anyone knows how to change the timestamps of folders recursively based on the latest timestamp found of the files in that folder.

So for example:

jon@UbuntuPanther:/media/media/MP3s/Foo Fighters/(1997-05-20) The Colour and The Shape$ ls -alF
total 55220
drwxr-xr-x  2 jon jon    4096 2010-08-30 12:34 ./
drwxr-xr-x 11 jon jon    4096 2010-08-30 12:34 ../
-rw-r--r--  1 jon jon 1694044 2010-04-18 00:51 Foo Fighters - Doll.mp3
-rw-r--r--  1 jon jon 3151170 2010-04-18 00:51 Foo Fighters - Enough Space.mp3
-rw-r--r--  1 jon jon 5004289 2010-04-18 00:52 Foo Fighters - Everlong.mp3
-rw-r--r--  1 jon jon 5803125 2010-04-18 00:51 Foo Fighters - February Stars.mp3
-rw-r--r--  1 jon jon 4994903 2010-04-18 00:51 Foo Fighters - Hey, Johnny Park!.mp3
-rw-r--r--  1 jon jon 4649556 2010-04-18 00:52 Foo Fighters - Monkey Wrench.mp3
-rw-r--r--  1 jon jon 5216923 2010-04-18 00:51 Foo Fighters - My Hero.mp3
-rw-r--r--  1 jon jon 4294291 2010-04-18 00:52 Foo Fighters - My Poor Brain.mp3
-rw-r--r--  1 jon jon 6778011 2010-04-18 00:52 Foo Fighters - New Way Home.mp3
-rw-r--r--  1 jon jon 2956287 2010-04-18 00:51 Foo Fighters - See You.mp3
-rw-r--r--  1 jon jon 2730072 2010-04-18 00:51 Foo Fighters - Up in Arms.mp3
-rw-r--r--  1 jon jon 6086821 2010-04-18 00:51 Foo Fighters - Walking After You.mp3
-rw-r--r--  1 jon jon 3033660 2010-04-18 00:52 Foo Fighters - Wind Up.mp3

The folder "(1997-05-20) The Colour and The Shape" would have its timestamp set to 2010-04-18 00:52.

Thanks in advance!

  • Don't multipost. Post your question only on the most appropriate site (here, clearly Unix). – Gilles Sep 2 '10 at 23:00
  • Terribly sorry. I posted at the other one first and thought I'd get better luck here (turns out I was wrong). I will not do so in the future. – MonkeyWrench32 Sep 3 '10 at 1:08

Using TOUCH you can change the folder or file timestamp:

touch -t 1004180052 folder #(YYMMDDhhmm)

Options that you may want to know:

-r, --reference=FILE
    use this file's times instead of current time 
    use [[CC]YY]MMDDhhmm[.ss] instead of current time 

With the -r you can specify a file to get teh timestamp from or you can use -t to set it yourself.

if you which to run it against all the files in the dir and using the last updated file update your folder ls would be a better alternative:

touch -r "`ls -1tA /path/to/folder | head -n 1`" /path/to/folder

Looks to me like you'll need to make a script (shell or Perl or whatever, but personally I'd use Perl) to parse the output of an ls -laF command and then invoke a touch command. In pseudo-code, that would look loosey like this:

while (`ls -laF $folder`)
  $tmp = echo $_ | awk '{print $6 $7}'
  if $tmp > $date { $date = $tmp}
touch $date $folder

Again, that is pseudo-code. Its meant to demonstrate the idea that I'm thinking of within 5 minutes. Do not expect the above to be similar to the code you end up producing.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.