1849072 Members
7396 Online
104041 Solutions
New Discussion

Re: shell script

 
SOLVED
Go to solution
skravi17
Advisor

shell script

Hi

-rw-r--r-- 1 apptst42 dba 1204 Jul 17 16:51 oo.r (file name)

I want to rename the file oo.r to oo.i. Don't
use copy and delete.

regards

ravi
17 REPLIES 17
Patrick Preuss
Trusted Contributor

Re: shell script

hi,
use mv

hth

Patrick
Goodbye Douglas! Whereever you are now, keep your towel and don't panic.
Sukant Naik
Trusted Contributor

Re: shell script

Hi Ravi,

Do the following


mv oo.r oo.i

-Sukant
Who dares he wins
U.SivaKumar_2
Honored Contributor

Re: shell script

hi
#mv oo.r oo.i

regards,
U.SivaKumar
Innovations are made when conventions are broken
Steve Steel
Honored Contributor

Re: shell script

Hi

Just use mv


mv(1) mv(1)

NAME
mv - move or rename files and directories

SYNOPSIS
mv [-f|-i] [-e extarg] file1 new-file

mv [-f|-i] [-e extarg] file1 [file2 ...] dest-directory

mv [-f|-i] [-e extarg] directory1 [directory2 ...] dest-directory

DESCRIPTION
The mv command moves:

+ One file (file1) to a new or existing file (new-file).


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
skravi17
Advisor

Re: shell script

Hi

i want to move the file oo.i to oo.r(example), actually
i don't know the file name but ending with .i
so how i will move the file *.i to *.r

regards

ravi
kish_1
Valued Contributor

Re: shell script

are you looking for script to move the file, here u find the script
for i in `ls`
do
echo "==== $i ===="
/usr/bin/mv $i oo.i
done

share the power of the knowledge
U.SivaKumar_2
Honored Contributor

Re: shell script

Hi,
If there is a single file *.r this will work
#mv *.r oo.i

regards,
U.SivaKumar
Innovations are made when conventions are broken
Steven Sim Kok Leong
Honored Contributor
Solution

Re: shell script

Hi,

#!/sbin/sh

old=i
new=j

for file in *.$old
do
mv $file `echo $file | cut -d\. -f1`.$new
done

Hope this helps. Regards.

Steven Sim Kok Leong
skravi17
Advisor

Re: shell script

Hi steven


Thanks lot, solved my problem.
i modified your script.

old=r
new=i

for file in `ls *.$old`
do
mv $file `echo $file | cut -d\. -f1`.$new
done


regards

ravi
skravi17
Advisor

Re: shell script

Hi

sorry,

Actually my requirment is i have 2 files

-rw-r--r-- 1 oratst42 dba 0 Jul 17 17:41 AS8@F2.20717.1741.I
-rw-r--r-- 1 oratst42 dba 870 Jul 17 17:50 AS8@10.20717.1750.I

but i ran that script i am getting output
like this

-rw-r--r-- 1 apptst42 dba 870 Jul 17 17:51 AS8@10.M

but i want like this

AS8@10.20717.1750.I to AS8@10.20717.1750.M


regards

ravi





Steven Sim Kok Leong
Honored Contributor

Re: shell script

Hi,

#!/sbin/sh

old=i
new=j

for file in *.$old
do
mv $file `echo $file | awk -F\. '{print $1"."$2"."$3"."}'`.$new
done

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: shell script

Hi,

A better way:

Hi,

#!/sbin/sh

old=I
new=M

for file in *.$old
do
mv $file `echo $file | sed -e 's/.$old$/.$new/g'
done

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: shell script

Hi,

I missed out a grave accent (`) after the sed:

#!/sbin/sh

old=I
new=M

for file in *.$old
do
mv $file `echo $file | sed -e 's/.$old$/.$new/g'`
done

With pattern matching (e.g. using sed), you do not need to worry about the number of dots (.) you have as part of your filename.

Hope this helps. Regards.

Steven Sim Kok Leong
skravi17
Advisor

Re: shell script

Hi steven


Again Thanks lot. All your scripts works fine
thanks. but I did this way.

old=I
new=M

for file in `ls *.$old`
do
mv $file `echo $file | cut -d\. -f1,2,3`.$new
done

regards

ravi
Leif Halvarsson_2
Honored Contributor

Re: shell script

hi
Try the basename command for this

for file in *.I
do
mv $file `basename $file .I`.M
done
perez Ilan
Occasional Advisor

Re: shell script

Hi Ravi,

try the pattern-matching operators.

old=I
new=M

for file in `ls *.$old`
do
newfile=${file%.$old}.$new
mv $file $newfile
done
Tom Maloy
Respected Contributor

Re: shell script

ksh has a construct that you can use instead of basename.

To replace the ending ".I" with ".M", use:

mv $ORIGINAL_FILE ${ORIGINAL_FILE%.I}.M

You do need to use the curly brackets.

Tom
Carpe diem!