Find & Replace
Art Alexion
art.alexion at verizon.net
Wed Feb 22 14:38:05 UTC 2006
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?
--
_______________________________________
Art Alexion
Arthur S. Alexion LLC
PGP fingerprint: 52A4 B10C AA73 096F A661 92D2 3B65 8EAC ACC5 BA7A
The attachment -- signature.asc -- is my electronic signature; no need for alarm.
Info @ http://mysite.verizon.net/art.alexion/encryption/signature.asc.what.html
Key for signed PDFs available at
http://mysite.verizon.net/art.alexion/encryption/ArthurSAlexion.p7c
The validation string is TTJY-ZILJ-BJJG.
________________________________________
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 374 bytes
Desc: OpenPGP digital signature
URL: <https://lists.ubuntu.com/archives/kubuntu-users/attachments/20060222/fb0cdc18/attachment.sig>
More information about the kubuntu-users
mailing list