How to change file extension in Linux shell script

2020-07-17 15:52发布

I've found some examples of doing this in similar situations, but this is the only shell-script I've written that does anything besides run commands verbatim, so I am struggling to apply the examples to my own situation and need some hand-holding <3

I'm just trying to batch rip audio from MP4s. This script works:

for f in *.mp4; 
    ffmpeg -i "$f" -f mp3 -ab 192000 -vn "mp3s/$f.mp3"
done

But the files all end with .mp4.mp3. How can I get rid of the mp4 part?

标签: linux shell
2条回答
狗以群分
2楼-- · 2020-07-17 16:22

Following command to change file extension for all files under the current directory.

find . -depth -name "*.c" -exec sh -c 'dname=$(dirname {}) && fname=$(basename {} .c) && mv {} $dname/$fname.h' ";"
查看更多
看我几分像从前
3楼-- · 2020-07-17 16:32

If you're using bash

${f%%.mp4}

will give the filename without the .mp4 extension.

Try using it like this:

for f in *.mp4; do
    ffmpeg -i "$f" -f mp3 -ab 192000 -vn "mp3s/${f%%.mp4}.mp3"
done

... and don't forget the do keyword as in the example given.

Explanation

The bash Manual(man bash) states:

${parameter%word} ${parameter%%word}

Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the %'' case) or the longest matching pattern (the%%'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

This is just one of many string manipulations you can perform on shell variables. They all go by the name of Parameter Expansion.

That's as well the section label given in the bash manual. Thus man bash /paramter exp should bring you there fast. `

查看更多
登录 后发表回答