Whenever I convert from one file format to another (for example, flac → ogg), I use find and ffmpeg. However, that leaves me with files like song_1.flac.ogg which is really annoying. Since my music library is distributed among multiple folders, it would have been annoying to use Thunar's Bulk Renamer. That's why I put together the following script:
#!/bin/bash
find -name *.$1 | while read f
do
mv ./"$f" "${f%$1}$2"
done
echo "Successful!"
(Yes, the last line is completely extraneous and completely meaningless in the context of error messages
)
The script takes two arguments - the first is the extension you want to remove and the second is the extension you want to replace it with. For example:
To rename a bunch of files like song_1.flac.ogg, I'd call the script like this:
rename.sh flac.ogg ogg
Notice that I did not include a leading dot for either the first or second arguments.
That's it! Enjoy!
The shell may try to interpret the asterisk in find's -name option, so it is safer to enclose it in quotes. It's possible to avoid piping the output of find to read, by using the -exec option. (usage same as above)
#!/bin/bash
find -name "*.$1" -exec bash -c 'mv "$1" "${1%$2}$3"' _ '{}' "$1" "$2" \;
NB The $1 $2 and $3 inside the miniscript 'mv “$1” “${1%$2}$3”' are independent from the script's $1 and $2. See Using Find for more details, and much general advice about find.