Dan Crosta on 22 Feb 2006 14:54:54 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] bash script help needed


Art Alexion wrote:
I am trying to iron out a script that renames all files in a current
directory to replace embedded spaces with underscores, and makes
everything lower case.

The suggestion that I was given was (in pertinent part):

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

But while the messages sent to the console indicated what I wanted to do
was happening, and there were no errors reported, no changes were
actually being made to the filenames.

I read the echo man page and it seemed that the line

echo -e "mv \"$OLD\" \"$NEW\"\n"

didn't actually execute the mv command, so I added the line

mv $OLD $NEW

after it, so it now reads

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"
    mv $OLD $NEW
  done

But this results in the error

mv: when moving multiple files, last argument must be a directory

even though $NEW does not yet exist, so I am not really moving multiple
files.

What am I doing wrong?

I think what it might need is

mv "$OLD" "$NEW".


The way I usually handle things like this is with ls -1 and sed... but that may not be appropriate for this job. Also, don't forget about python :)


dsc
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug