On Tue, Mar 11, 2008 at 09:21:25PM -0700, Tyrion wrote:
Andrew Lentvorski wrote:
Tyrion wrote:
I use the following in a script to remove spaces from filenames:
for i in *; do mv "$i" `echo $i | tr " " "_"`; done
Better to do
for i in "* *"; ...
so that you only try to rename files that do have spaces in them.
This of course only works in the current directory, how would I make it
do all files in subdirectories as well?
Try "find" with -exec
-a
Tried all sorts of variations of find with -exec and couldn't get
anything to work, can someone give me an example?
Here's the one I thought would work, but tr doesn't seem to work this way
find -type f -exec mv '{}' `echo '{}' | tr " " "_"` \;
I've never found -exec to be very friendly, especially since for most
things it forks for every invocation. You can't really get around that
here, if you want to use the shell. But, how about something like:
find . -depth -name '* *' -print |
sed -e "s/.*/mv '&' '&'/" -e 's/\(\/.*\) /\1_/g' | sh
The find finds all names that have at least one space in them, and the
'-depth' option makes sure that we rename files before directories.
Ok, this isn't quite right, because the sed line only changes the last
space in each line to an underscore. You could just keep running it until
there aren't any names left to change :-)
I'm not sure that there is a regular expression to change all of the
spaces, although you could repeat the expression on sed numerous times.
Another option would be to cd to each directory that you find, and then do
a rename operation there.
David
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list