On Fri, Jan 10, 2003 at 01:49:18AM +1030, Rob wrote:
> > If you want to do it for all files in a directory:
> >
> > # for file in *; do mv "$file" `echo $file | sed -e 's/ /_/g'`; done
> >
> > should do the trick. I think Perl is overkill for something this simple.
> > Someone else suggested tr, which probably works, but I've had more
> > success with sed.
> But if you do this, won't the spaces be mistaken for filename separators?

No, he has quotes around his $file, and the `` part replaces spaces,
so this should work. Witness:

$ touch "a b" c
$ for i in *; do echo "arg $i endarg"; done
arg a b endarg
arg c endarg

Another solution is using a find & awk combo, which makes it work
recursively (ie one commandline instead of one per directory):

$ find -d . -name '* *' | awk -F/ '{ OFS = "/"; o = $0; gsub(" ", "_", $NF); print "mv 
\"" o "\" \"" $0 "\"" }' | sh

This can probably be written in a shorter way by awk gurus, I'm just
trying to learn it and this was a good exercise, which is the reason
I'm replying to this post :)



The rain it raineth on the just
        And also on the unjust fella,
But chiefly on the just, because
        The unjust steals the just's umbrella.

Attachment: msg14837/pgp00000.pgp
Description: PGP signature

Reply via email to