I want to put together a small script that changes the file extension of all the files in a directory that have a certain extension, such as .png
or .gif
.
My thought is that the script just take 2 arguments at execution. One being the extension to change and the second being the extension to change it to.
Originally I was thinking I would use read
to get the target directory but that makes this too complicated so instead I am just making it work in the current directory. I am running Debian 9 so the rename
command is not available.
This works with fixed content (txt to rdf):
$ find -depth -name "*.txt" -exec sh -c 'mv "$1" "${1%.txt}.rdf"' _ {} \;
however, I cannot figure out how to change the extensions to the content of variables that inherit the passed arguments. these will be $1 $2 by default but when I change it as below, it does not work.
$ find -depth -name "*.$1" -exec sh -c 'mv "$1" "${1%.$1}.$2"' _ {} \;
This example just appends a . to the end of all the matching files. What am I missing here?