mv command not working as expected
Posted on 2012-03-12
I have a need to replace spaces with underbars in all the directory tree from a certain sub-directory. The files were copied on an external drive from a family member that uses Windows. I prefer to use *nix things, so for Windows I use Cygwin.
Given that I have a testing file "My Family" in the current working directory, issuing the following command does the expected:
mv -T "My Family" My_Family
I want to use find to sweep all the files and directories from my starting directory, and pass each file/directory name to a script that does the same thing, but allows for the passing of arguments -- So the entire script (called sp2underbar) is:
targ=`echo $1 | tr ' ' '_'`
/usr/bin/mv -T \'$1\' $targ
The execution of the script gives this:
$ sp2underbar "My Family"
/usr/bin/mv: extra operand `My_Family'
Try `/usr/bin/mv --help' for more information.
If I substitute for the variables as they would appear in the script and run the result manually on the command line, the rename works as expected.
I tried the same test exercise on an actual Linux box with the same results, so please don't advise that there is "something wrong with Cygwin"....
So what else is it? What am I missing?