0

I have a script to encode video files. When I run this script, I would like to keep the basename of the file, add a 'modified' string to the filename and also changing the extension.

The following for loop does that with the exception of changing the extension:

for file in *.mkv; do encode $file "${file%%.*}_modified.${i#*.}"; done 

I'd like that my_file.mkv will become my_file_modified.mp4. The previous loop just converts my_file.mkv into my_file_modified.mkv

How to change also the extension from .mkv to .mp4?

Thanks in advance.

1 Answer 1

2

I'm not totally sure if I got your question right, and probably not; but if I did, you should just do this:

for file in *.mkv; do encode $file "${file%.*}_modified.mp4"; done 

The ${i#*.} part in your previous command actually took the original extension from the file name; you can just omit it and set your own extension instead.

Also, as @M.NejatAydin pointed out in the comment, you should use ${file%.*} instead of ${file%%.*}, to keep the entire original filename if it has a dot inside it.

For example:

$ file="test.file.mkv" $ echo "${file%%.*}_modified.mp4" test_modified.mp4 # This is probably NOT what you want $ echo "${file%.*}_modified.mp4" test.file_modified.mp4 # This is probably what you want 
Sign up to request clarification or add additional context in comments.

3 Comments

Sometimes I'm just blind. Thx a lot!
I think the OP might have intended to use ${file%.*} in place of ${file%%.*}
@M.NejatAydin thanks, I updated the answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.