0

I'm trying to write a script that renames the subtitles to the same name as the movie file is.

I'm currently stuck at getting the .srt filename into a variable. Currently I'm seeking the smallest file in the directory with:

srtnametmp="$(basename $(find . -name '*.srt' -maxdepth 1 -type f  -printf "%s\t%p\n" | sort -n -r| tail -1 | awk '{print $NF}'))"

However, it can be more than one .srt in the same directory, so I was thinking about finding the newest .srt (by creation time). Now I've been searching for 5-ish hours, tested several solutions, but I never got the result (only the filename) stuck to the variable $srtnametmp Now I'm little lost and looking for help.

JoBe
  • 397
  • 5
  • 17
  • Are you working on a filesystem that even stores a creation/birth timestamp on files? – Kusalananda Feb 06 '20 at 13:28
  • Isn't all doing that? I can at least see the information on the desktop, and since the desktop can find when a file is created, so should the bash be able to do so... At least I think so.. – JoBe Feb 06 '20 at 13:45

1 Answers1

0

Below script can be used to get newest srt file

#!/bin/bash
srt_dir="/home/$USER/Downloads/"
file_type="srt"
srtnametmp=`ls -t1 $srt_dir | grep $file_type | head -1`
echo $srtnametmp

Happy Scripting
r_D
  • 111
  • Thx, but that script gets the last modified file, not the last created. When unpacking from zip Linux only sets the created parameter, last modified is kept from the original. – JoBe Feb 06 '20 at 13:04
  • @JoBe If you use unzip to unzip, add option -DD to skip restoration of timestamps. –  Feb 06 '20 at 13:37
  • Why didn't I think about that solution.. That solved my issue, but is there anyway to get the creation date? – JoBe Feb 06 '20 at 13:50
  • I don't know about creation time. Maybe what you really want is change time? For that you could use find with -printf '%CY%Cj%CT %f\n' –  Feb 06 '20 at 14:01
  • Went through some more posts, it says that creation time is not stored on many filesystems in Unix. I think last modified time changed with -DD will work for you. – r_D Feb 07 '20 at 04:21
  • Strange, because when you look in the properties on the desktop, there is a creation date see https://dropbox.gr1.se/crdate.png or am I completely wrong? Your solution works, if I use the -DD during extraction – JoBe Feb 07 '20 at 08:27
  • @JoBe What's the output when you stat the file? Does the Change time from stat correspond to to that "Created" time? What program is that in the screenshot? –  Feb 07 '20 at 08:54
  • Stat says: birth: - the screenshot is from the desktop, menu/properties – JoBe Feb 07 '20 at 13:37
  • @JoBe Does the following do what you want?: srtnametmp=$(find . -maxdepth 1 -name '*.srt' -type f -printf '%CY%Cj%CT %f\n' | sort -nr | sed '1{s/^[^ ]* //;q}') –  Feb 07 '20 at 14:25
  • Sheezus, that's a long string, but it appears to do the job! – JoBe Feb 07 '20 at 16:10