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.

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

1 Answer 1

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

You must log in to answer this question.

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