Change spaces or dots in file name to dashes

das paagol at gmail.com
Fri May 11 11:06:40 UTC 2007


On Fri, 2007-05-11 at 13:19 +0300, OOzy Pal wrote:
> How can change spaces or dots in a file name to dashes using the
> Console?

This I use to change any filename in a directory from upper case to
lower case, and to delete all the blankspaces.

<<
#!/bin/bash

for i in * 
do 
oldname=`echo $i`
newname=`echo $i | tr [A-Z] [a-z] | tr -d [:blank:]`
if [ "$newname" != "$oldname" ]
then mv "$oldname" "$newname"
fi
done
>>

I saved the file under the name 'changename' in my bin. This is nothing
but a simple script based on 'tr'. Read 'man tr' and you can customize
the script to suit yours:

<<
#!/bin/bash

for i in * 
do 
oldname=`echo $i`
newname=`echo $i | tr [:blank:] - | tr . - `
if [ "$newname" != "$oldname" ]
then mv "$oldname" "$newname"
fi
done
>>

Copy the whole thing within << and >> to a file, say, chnm to your bin,
that is /home/op/bin, if your username is op. If there is no bin
directory, create it with:
mkdir /home/op/bin

Now issue:
chmod +x /home/op/bin/chnm

Now if /home/op/bin in not in your 'echo $PATH', then from within a
directory run it as:
/home/op/bin/chnm

I created two files as a trial in my /home/dd/temp/t, with this command:
touch "oh dear .dear .ana" "my .very john"

This created these two files. Then I ran chnm from /home/dd/bin (I am dd
on my system). I think it did exactly the thing you wanted it to do.
Pasting the result:

dd at mahammad:~/temp/t$ touch "oh dear .dear .ana" "my .very john"
dd at mahammad:~/temp/t$ ls -1
my .very john
oh dear .dear .ana
dd at mahammad:~/temp/t$ chnm
dd at mahammad:~/temp/t$ ls -1
my--very-john
oh-dear--dear--ana
dd at mahammad:~/temp/t$ 

---
das











More information about the ubuntu-users mailing list