Find & Replace

James Gray james at grayonline.id.au
Tue Feb 21 03:24:40 GMT 2006


On Tuesday 21 February 2006 10:15, Dave wrote:
> I guess this is more of a general Linux question, and not specific to
> Kubuntu. When I install Kubuntu I had copied my hard drive from a previous
> installation of Mepis, which in turn was taken directly from Windows. to
> make a long story short, it's been several years now and I still have
> Windows-style names for all my files. meaning they use a lot of CAPS and
> SPACES.
>
> 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:])

eg,
/path/to/dewindowize.sh
will fix up the current directory

/path/todewindowize.sh ~/music/
will fix up the files in the ~/music/ directory (but nothing beneath it!)

To recurse a directory tree, you do something like this:
find ~/music/ -type d -exec /path/to/dewindowize.sh {} \;

The script will NOT follow symbolic links, but WILL lowercase+de-space the 
link's name.

All the work is actually done at the bottom of the script in the "for" loop.
man bash  (or "help for" without the "")
man sed
man tr

> Additionally I would like confirmation that what I'm trying to do won't
> cause problems, most of the files I want renamed are music and picture
> files. 

Shouldn't cause any major problems, but Linux is a case sensitive system that 
will barf if you have symlinks pointing to the old files - the links will 
break.  Also, many programs will think you've removed all your old files and 
replaced them with totally different files - this may cause media players to 
rescan your collection, backup scripts to backup them ALL up (recent 
ctime/atime/mtime) etc.  No biggie, but something you mightn't think of 
immediately.

Feel free to play with that script - it's not very efficient, it's not very 
well thought out but it's free and thrown together off the top of my head 
offline on the train...

Cheers,

> I currently run Kubuntu 5.1 Breezy in KDE.

Me too :) Both i686 and an AMD64 version :P

Cheers,

James
-- 
Never trust anybody whose arm is bigger than your leg.



More information about the kubuntu-users mailing list