Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 773  / 3 Years ago, tue, november 30, 2021, 12:22:32

I have a thousand folders containing two files: one of them with a random name (mp4 extension) and the other with a name (srt extension).


I intend to replace the random generated file name by the other file name, keeping extension (mp4, mkv or avi) contained on the same folder. By the way the name of the files use to include () and other foreign characters (ó, ü, etc).


In this situation the script should, in my opinion:



  1. Go into the folder

  2. Get the name with .srt extension

  3. Rename the file with mp4 extension with the new name (keeping whatever the extension)

  4. Get out of the folder

  5. Recursive travel the folders as it may contain other sub-folders


The 5th step may be overridden, though.


I'd appreciate a hand. Can anyone help?


More From » rename

 Answers
5

Try this little snippet,


shopt -s globstar
for s in **/*.srt; do
m=( "${s%/*}"/*.mp4 )
printf '%s --> %s
' "${m[0]}" "${s%.*}.mp4"
#mv "${m[0]}" "${s%.*}.mp4"
done
shopt -u globstar


  • Remove the # in front of mv if the output is as expected

  • If there are multiple mp4s in one dir, it will rename only the first it finds. You could easily use loop to mv all mp4's and include a suffix such as _1 etc.

  • If there are multiple srts in one dir, it will rename the mp4 multiple times, so it will be named like the latest srt it finds.


[#1269] Wednesday, December 1, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peratingcit

Total Points: 253
Total Questions: 122
Total Answers: 94

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;