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.

  • Are you working on a filesystem that even stores a creation/birth timestamp on files?
    – Kusalananda
    Feb 6 '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 6 '20 at 13:45

Below script can be used to get newest srt file

srtnametmp=`ls -t1 $srt_dir | grep $file_type | head -1`
echo $srtnametmp

Happy Scripting
  • 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 6 '20 at 13:04
  • @JoBe If you use unzip to unzip, add option -DD to skip restoration of timestamps.
    – user391836
    Feb 6 '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 6 '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'
    – user391836
    Feb 6 '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 7 '20 at 4:21

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.