Command to replace character

I've been googling for the following for the past few weeks several times, but haven't yet come across something that I could easily grasp. Can someone point me in the right direction please?

I'm trying to replace a character in file names, i.e. the character is a period, and I want to replace it with nothing. So, if a file name is file.name, I want to be able to execute a command from the shell that will change it to filename. And if this can be scripted so that whenever the script is executed, it'll take out the periods in file names in whatever directory is specified, even better.

Please point me towards whatever resources that will help me figure this out.

Here is basic script.

#! /bin/sh
# mv.sh
FILE1=file1.ext
FILE2=file2.ext

for file in $FILE1 $FILE2
do
        FILE=${file/\./ }
        echo $file -- $FILE
done
[~/temp]$ ./mv.sh 
file1.ext -- file1 ext
file2.ext -- file2 ext

You can use this a starting point.

Vino

Can you explain the different components of the script? I'm trying to make sense of it (interpreting the script's contents to English) but I'm having trouble understanding it. Also, what are the differences of the code section from the script section?

Hi,

Try this code. This will get the directory name as the parameter and rename all the filenames with "." in it. I hope the script will be easy for you to understand.

 
#!/bin/ksh
#Rename the filenames with . in it
if [[ $# != 1 ]]
then
    print "Full Directory path is required"
    exit 1
fi
for i in `ls $1`
do
    newfilename=`print $i | sed 's/\.//g'`
    mv $1/$i $1/$newfilename
done

-Mons

Thanks! I copied and pasted the contents of the script and tested it out with several d*o*t* files I touched, and had fun with it. Thanks!

I do have several questions though ...
1) Can the shell that's used be replaced as sh? I am not sure if the server that has the Unix operating system on has the ksh, although I figure it should. If it can be replaced, will any of the contents have to be modified?

2) Although I have questions about lines 3, 6 - 8, and 10 - 11, 1 being the #!/.../ line, (that's like 99.% of the script's contents ... :eek: ) can you clarify your statement "This will get the directory name as the parameter and rename all the filenames with "." in it."? Does it mean that the directory specified will be the criteria in which the script executes its magic on?

I will be manning commands like fi and sed among others that is included in the script to be able to interpret it to English so I can understand it (that seems to be the only way I can comprehend the Unix commands). Whatever summaries you can provide though would help expediate this process though! :smiley: Just kidding, of course, kind of.

Again, thanks, many times over!

The main line that you need to know is the one in bold.

FILE1=file1.ext
FILE2=file2.ext

for file in $FILE1 $FILE2
do
        FILE=${file/\./ }
        echo $file -- $FILE
done

file contains the value file1.ext and file2.ext held one at a time. For each value replace the . with a space

And then move the file as 'mv $file $FILE'

${file/\./ } is a shell builtin for the sed command sed 's/\./ /g'

Read through the man pages of sh.

Vino