2

How to change "date modified" of multiple folders to be the same of that of the .mkv that's inside each one.

eg. there's folder1 which contains a file 1.mkv

folder1: 2/17/14
1.mkv: 1/6/12

I want folder1 to get "date modified" 1/6/12

there's also

folder2: 20/9/13
2.mkv: 1/1/10

I want folder2 to get "date modified" 1/1/10

etc

Maybe some modification of this touch command from https://unix.stackexchange.com/a/1526 would help?

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

(I can't understand what it does)

Here are some sample paths (using Cygwin of course):

Kostas@Kostas-pc /cygdrive/f/movies
$ ls -l

drwx------+ 1 Kostas None     0 Feb 18 17:03 Serpico (1973)
drwx------+ 1 Kostas None     0 Feb 21 01:13 The Rite (2011)

eg

Kostas@Kostas-pc /cygdrive/f/movies
$ ls -l The\ Rite\ \(2011\)/
total 5730596
-rwx------+ 1 Kostas ???????? 5867505504 Apr 27  2011 The Rite (2011).mkv
-rwx------+ 1 Kostas ????????      12968 Feb 16 20:03 The Rite (2011).nfo
-rwx------+ 1 Kostas ????????      79757 Apr 26  2011 The Rite (2011).srt
-rwx------+ 1 Kostas ????????     264745 Oct  8  2011 The Rite (2011).tbn
-rwx------+ 1 Kostas ????????     255667 Oct  8  2011 The Rite (2011)-fanart.jpg
darkred
  • 135

2 Answers2

4

The switch to touch, -r uses whatever file you provide it as a reference for what timestamp to use. So for example:

$ touch -d 2012-01-01 file1
$ ll
total 0
-rw-rw-r--. 1 saml saml 0 Jan  1  2012 file1
-rw-rw-r--. 1 saml saml 0 Feb 20 14:37 file2

Will set the date on file1 using the date 2012-01-01. I can the use that file's timestamp to change other files & directories.

$ touch -r file1 file{2..5}
$ ll
-rw-rw-r--. 1 saml saml    0 Jan  1  2012 file1
-rw-rw-r--. 1 saml saml    0 Jan  1  2012 file2

So you could utilize the -r switch to do what you want.

Example

Say I had the following:

$ ll
total 12
drwxrwxr-x. 2 saml saml 4096 Feb 20 14:42 dir1
drwxrwxr-x. 2 saml saml 4096 Feb 20 14:42 dir2
drwxrwxr-x. 2 saml saml 4096 Feb 20 14:42 dir3

$ ll *
dir1:
total 0
-rw-rw-r--. 1 saml saml 0 Jan  1  2012 1.mkv

dir2:
total 0
-rw-rw-r--. 1 saml saml 0 Feb  1  2012 2.mkv

dir3:
total 0
-rw-rw-r--. 1 saml saml 0 Mar  1  2012 3.mkv

Now if we run this command we can take the corresponding .mkv for each of those directories and use it's timestamp, applying it to each parent directory.

$ for i in dir*; do mkv=${i/dir/}.mkv; touch -r "$i/$mkv" "$i" ;done

The results:

$ ll
total 12
drwxrwxr-x. 2 saml saml 4096 Jan  1  2012 dir1
drwxrwxr-x. 2 saml saml 4096 Feb  1  2012 dir2
drwxrwxr-x. 2 saml saml 4096 Mar  1  2012 dir3

EDIT #1

Based on your example file structure I'd use the following command:

$ for i in *;do touch -r "$i/${i}.mkv" "$i";done

Example

$ ll
total 8
drwxrwxr-x. 2 saml saml 4096 Jan  1  2012 Serpico (1960)
drwxrwxr-x. 2 saml saml 4096 Jan  1  2012 The Rite (2011)

$ ll *
Serpico (1960):
total 0
-rw-rw-r--. 1 saml saml 0 Feb 20 18:17 Serpico (1960).mkv

The Rite (2011):
total 0
-rw-rw-r--. 1 saml saml 0 Feb 20 18:17 The Rite (2011).mkv

apply timestamps

$ for i in *;do touch -r "$i/${i}.mkv" "$i";done
$ ll
total 8
drwxrwxr-x. 2 saml saml 4096 Feb 20 18:17 Serpico (1960)
drwxrwxr-x. 2 saml saml 4096 Feb 20 18:17 The Rite (2011)

In this scenario, all the .mkv files would have to have an identical name to the directory that they're inside of. You'd run this script while inside your /cygdrive/f/movies/ directory.

slm
  • 369,824
  • Please, tell me, how in your command to define the path of the folder in which the subfolders containing .mkvs exist. In my case, there are in /cygdrive/f/movies. – darkred Feb 20 '14 at 20:26
  • @user3231411 - can you give me more of a sample of your directory structure? Is it /cygdrive/f/movies/folder*? – slm Feb 20 '14 at 20:54
  • Yes, exactly. eg. /cygdrive/f/movies/movietitle1 (1985) or /cygdrive/f/movies/movietitle2 (1990), etc and each folder contains various files, one of which is the movie_title_something (year).mkv – darkred Feb 20 '14 at 22:54
  • @user3231411 - can you do me a favor and include a couple of these directory paths + file names in your Q? Without seeing this it's next to impossible to provide you a more detailed answer. – slm Feb 20 '14 at 22:57
  • Yes, of course. I just posted some sample paths (it's via Cygwin, because I use Windows) – darkred Feb 20 '14 at 23:11
  • @user3231411 - see updates. LMK if that works. – slm Feb 20 '14 at 23:24
  • It worked like a charm! Thank you so much for your help and all the detailed posts! – darkred Feb 21 '14 at 00:13
2
for dir in /path/dir1 /path/dir2; do
    mkv="${dir}/.mkv"
    if [ -f "$mkv" ]; then
        touch --reference="$mkv" "$dir"
    fi
done
Hauke Laging
  • 90,279