I am trying to bulk change some files in bash that have spaces and unwanted endings to their extension.
For example from
abc.pdf.!ut.!ut
to
abc.pdf
I will show whole progress in case I made something harder than it should actually be. So at first I started with something like
for file in `find /Users/phwd/Desktop/Film\ Sheet\ Music\ Scores\ 2 *.\!ut.\!ut`;
do mv $file `echo $file | sed 's/\(.*\)\.\!\ut\.\!\ut/\1/'`;
doneThe above did not work for spaces when sending for mv so I changed to this
find /Users/phwd/Desktop/Film\ Sheet\ Music\ Scores\ 2 *.\!ut.\!ut | while read file
do mv $file `echo $file | sed 's/\(.*\)\.\!\ut\.\!\ut/\1/'`;
doneThis worked almost but mv started saying an arbitrary message
usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directoryI am over thinking something ?
11 Answer
So if you just want to remove the ending !ut.!ut I would suggest something like this:
for file in `find ...`; do mv "$file" "${file%.\!ut.\!ut}"
doneIn this case the shell does its job and removes the ending. This feature is called parameter expansion.