Passing sed to a temporary variable

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/'`;
done

The 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/'`;
done

This worked almost but mv started saying an arbitrary message

usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory

I am over thinking something ?

1

1 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}"
done

In this case the shell does its job and removes the ending. This feature is called parameter expansion.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like