Find & Replace
James Gray
james at grayonline.id.au
Wed Feb 22 20:58:40 UTC 2006
On Thursday 23 February 2006 01:38, Art Alexion wrote:
> James Gray wrote:
> >On Tuesday 21 February 2006 10:15, Dave wrote:
> >>Is there a bash command (similar to awk) that would allow me to search
> >> out all the spaces in my file names and replace them with underscores
> >> (recursive from home folder)? Or perhaps one of you already has a
> >> script that could help with this task? I have Googled this a number of
> >> times and feel like I have exhausted all other avenues, even a point in
> >> the right direction would be greatly appreciated.
> >
> >How about a script like this:
> >
> >---- SCRIPT: de-windowize.sh ----
> >#!/bin/bash
> >
> ># See if a command line arg was passed
> >if [ ! -z $1 ]; then
> > # We have a command line arg - was it a directory??
> > if [ -d $1 ]; then
> > # Wonderful! We were given a directory to clean up!
> > echo -n "Changing to $1 - "
> > cd $1 2&>/dev/null
> > if [ $? = 0 ]; then
> > # YAY! We're in the directory
> > echo "DONE"
> > else
> > # Couldn't change to the directory...WTF?!
> > echo "Couldn't change to $1! ERROR!!"
> > exit 1
> > fi
> > else
> > # It wasn't a directory - bail out!
> > echo "That's NOT a directory (idiot!) :P"
> > exit 1
> > fi
> >fi
> >
> ># RIGHT! Now that we've handled all the command line fru, do the work!
> >for OLD in *
> > do
> > NEW=`echo $OLD|sed s/\ /\_/g | tr [:upper:] [:lower:]`
> > echo "OLD=\"$OLD\""
> > echo "NEW=\"$NEW\""
> > echo -e "mv \"$OLD\" \"$NEW\"\n"
> > done
> >---- SCRIPT: de-windowize.sh ----
> >(then make it executeable: chmod a+x dewindowize.sh)
> >
> >This will do the following to ALL files in the CURRENT directory, or the
> >directory specified on the command line:
> >1. Replace ALL spaces with "_" (sed s/\ /\_/g)
> >2. Convert ALL UPPER case characters to lower case (tr [:upper:]
> > [:lower:])
>
> James. This is a very useful script for converting batches of space
> embedded files, not just old windows installs, but stuff people send you
> or you download.
>
> Problem on my system, though is that the echo stuff sends the right
> message to the console, but the actual renaming (moving) never takes place.
>
> I read the echo man page and it didn't seem to me that the actual mv
> command was being executed by the script, so I added the line
>
> mv $OLD $NEW
>
> as the last line in the loop, before done.
>
> Now when I run it, it complains that "mv: when moving multiple files,
> last argument must be a directory", but $NEW doesn't actually exist, so
> multiple files are not really being moved. What could be wrong?
Hi Art (and Dave),
Yeh, my bad. I simply cut-n-paste the script without "fixing it" so it would
actually DO the move (rename). OOPS!! I usually just echo actual changes to
make sure they're OK before making them "live". Good testing technique but
embarrassing when you post it as a "working" script! Sorry.
The echo -e "mv \"$OLD\" \"$NEW\"\n" simply display's what WOULD be done, but
doesn't actually do anything - as you correctly discovered. Your solution is
actually pretty darn close! The fix is to enclose the shell variables in
quotes:
mv "$OLD" "$NEW"
The problem is that the "mv" command takes two arguments: the source and the
destination, which are separated by a space. So when you have a filename
with a space in it you need to either escape the space or enclose the file
name in quotes :) Imagine I have the following file:
Breath no More.mp3 (the song I'm listening to now - Evanescence)
to rename it you would have to take one of the following approaches:
mv Breath\ no\ More.mp3 Breath_no_More.mp3 (escaping the spaces)
mv "Breath no More.mp3" Breath_no_More.mp3 (using quotes)
Without explicitly telling the shell how to handle the spaces, the "mv"
command interprets it like this:
mv Breath no
Then complains that "Breath" doesn't exist - correct! Enclosing file name
operations in quotes is easily scriptable and safe way to handle weird file
names from a script...unless the file name CONTAINS quotes....and that's a
whole other kettle of fish!
I'll leave it there coz the corner cases (like quotes in filenames) are rare
and not particularly easy to handle. I hope you've enjoyed "Shell Scripting
101" :P
Cheers,
James
--
It gets late early out there.
-- Yogi Berra
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/kubuntu-users/attachments/20060223/af31c858/attachment.sig>
More information about the kubuntu-users
mailing list