0

I found around 50 mkv files with modification time in the future (year 2098 and 2097). I see that .srt files with the same name carry the right date and are located in the same subfolder as each mkv file.

How would you issue a touch command to read the oldest srt and change the last modified date of the mkv in the same subfolder to that date?

home/incoming/media/berlin.festival.2019/berlin.festival.2019.mkv 
home/incoming/media/Exit.festival.2022/Exit.festival.2022.mkv
$ for f (*/.mkv) touch -r $f:r.srt

-- $f doesn't catch them only catch them in the root of media folder

I tried something like

$ for file in $(find /home/loop/test/*.mkv -type f) do touch -r $f:r.srt -- $f

in my test folder but it's not working.

Ubuntu is the operating system.

From Change the last modification time of subfolders to that of the oldest file inside, I fixed the dates of folder with

for dir in /home/loop/test/*(NF); do
  oldest=( $dir/*.(|srt|)(N.Om[1]) )
  if (( $#oldest )) touch -r $oldest -- $dir
done

ls -l has perfect date now for the folders because I already fixed it by using dates from srt files.

But I would like to change date of mkv files inside of subfolders based on date of srt files.

/home/loop/test # ls -l  
drwxrwxrwx 2 loop loop 4096 Jul 23 2019 berlin.festival.2019 
drdrwxrwx 2 loop loop 4096 Jan 5 2019 Belgum.2018
drwxrwxrwx 2 loop loop 4096 Feb 21 2020 Denmark.2019 
drwxrwxrwx 2 loop loop 4096 Oct 3 2020 Exit.festival.2022
10
  • 1
    Have you tried anything at all? Care to share? Because it's not clear which part you cannot solve, unless you are asking somebody to code it for you. Commented Oct 16, 2023 at 13:11
  • for f (*/.mkv) touch -r $f:r.srt -- $f doesn't catch them only catch them in the root of media folder I tried something like for file in $(find /home/loop/test/*.mkv -type f) do touch -r $f:r.srt -- $f in my test folder but its not working.
    – Loop1
    Commented Oct 16, 2023 at 13:22
  • 1
    Thanks. I copied the the code from the comments to the question where most people will be able to see it right away. Please edit the question if needed and instead of not working it might be better to paste the actual error code, it's usually helpful information. Commented Oct 16, 2023 at 13:36
  • Please also tell us what operating system you are using, and show us the output of ls -l on the good and bad files. Do you want us to change the modification date only?
    – terdon
    Commented Oct 16, 2023 at 13:38
  • 1
    Please edit your question to answer comments and include extra information. If you put it in a comment, you cannot format it (as you can see above) and it is very easy to miss.
    – terdon
    Commented Oct 16, 2023 at 14:06

1 Answer 1

1

In zsh, from within the /home/loop/test directory:

for mkv in **/*.mkv(Nms-1); do
  srt=( $mkv:r(.*|).srt(N.Om[1]) )
  if (( $#srt )) touch -r $srt -- $mkv
done

Would loop through all the files last modified less than 1 second ago (the idea being to get those with a modification time in the future), find the srt files (with an optional .xx before it) with the same root name, take the oldest (Om to Order by modification time) and copy its mtime to that of the mkv.

(BTW, your oldest=( $dir/*.(|srt|)(N.Om[1]) ) should rather be oldest=( $dir/*.srt(N.Om[1]) ). (|srt|) means nothing or srt or nothing).

3
  • As I said in other thread its not working, for f (*/.mkv) touch -r $f:r.srt -- $f touch: failed to get attributes of 'Exit.festival.2022/Exit.festival.2022.srt': No such file or directory. To edit my post I found out why many subtitles have code suffices like .hr.srt on the end or sr.srt or br.srt and its because of that
    – Loop1
    Commented Oct 16, 2023 at 21:49
  • @Loop1, see edit. Commented Oct 17, 2023 at 7:30
  • Great your edited script working like a charm.
    – Loop1
    Commented Oct 17, 2023 at 11:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .