Question

I'm trying to create a batch file in linux that will allow me to change extensions of files in multiple subdirectories. After much searching and experimenting i've found what seems to be a solution:

find /volume1/uploads -name "*.mkv" -exec rename .mkv .avi {} +

When running the script i get the following error:

find: -exec CMD must end by ';'

I've tried adding ; and \; (with or without +) but to no avail. What's wrong with the command and how can I fix it?

Edit: Running on a Synology NAS with DSM 4.2

Was it helpful?

Solution

you have to escape all characters that would be interpreted by bash. in your case these are the semicolon and the curly braces (you forgot to escape the latter in your code):

find /volume1/uploads -name "*.mkv" -exec rename .mkv .avi \{\} \;

the {} (in our case \{\}) is expanded to the filename, so the actual call would look like rename .mkv .avi /volume1/uploads/foo/bla.mkv (which is not the exact syntax the /usr/bin/rename needs, at least on my system). instead it would be something like:

find /volume1/uploads -name "*.mkv" -exec rename 's/\.mkv$/.avi/' \{\} \;

UPDATE

if you don't want to (or cannot) use perl's rename script, you could use the following simple bash script and save it as /tmp/rename.sh

#!/bin/sh
INFILE=$1
OUTFILE="${INFILE%.mkv}.avi"
echo "moving ${INFILE} to ${OUTFILE}"
mv "${INFILE}" "${OUTFILE}"

make it executable (chmod u+x /tmp/rename.sh) and call:

find /volume1/uploads -name "*.mkv" -exec /tmp/rename.sh \{\} \;

UPDATE2

it turned out that this question is really not about bash but about busybox.

with a limited shell interpreter like busybox, the simplest solution is just to append the new file extension:

find /volume1/uploads -name "*.mkv" -exec mv \{\} \{\}.avi \;

OTHER TIPS

Not sure how different find and rename commands are on your DSM 4.2 OS so try something like:

find /volume1/uploads -name "*.mkv" | while read filename; 
do mv -v "${filename}" "$(echo "${filename}" | sed -e 's/\.mkv$/\.avi/')"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top