Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename a File 1

Status
Not open for further replies.

longs

Programmer
Jul 27, 2007
31
US
Hello All,

I would like to rename files. Let me example what I am trying to do.

I have files coming in daily;
testfile200803101_508400.ZIP.PGP
testfile200703005_879850.ZIP.PGP

These are example only. one thing is for sure that it will start with "testfile" and ends with ".ZIP.PGP"

I would like to rename each file to two different name e.g.
testfile200803101_508400 and testfile200803101.ZIP

I have tried to use sed
outfile=$(echo $INBOUND/$FILE_NAME | sed s/\.pgp/\.zip/)
but this will only replace pgp with zip

Thanks for your help

 
rename each file to two different name
???

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ok i think i wrote it wrong:

I like to create a different name, shall i say make two different copies of that file.

These are the files coming in
testfile200803101_508400.ZIP.PGP
testfile200703005_879850.ZIP.PGP

I would to make one copy as
testfile200803101_508400 (This I can do with sed s/\.ZIP.PGP//)
and second as
testfile200703005.ZIP

Regards,
Khayam
 
Why not simply use basename ?
basename testfile200803101_508400.ZIP.PGP .ZIP.PGP
basename testfile200803101_508400.ZIP.PGP .PGP


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Also, if disk space is a consideration here, you might want to create a second link to the file rather than copying it (see the ln command. That way you end up with two filenames pointing to the same physical file.

Annihilannic.
 
Well I won't be the same file, actually I am trying to use the name only. I get the and then I have to send out the email and perform different things.

I was able to create first name(testfile200803101_508400) from the files coming in
testfile200803101_508400.ZIP.PGP
testfile200703005_879850.ZIP.PGP

To do that i used
NEW_FILE=$(echo $INBOUND/$outfile | sed s/.ZIP.PGP//)
where outfile=testfile200803101_508400.ZIP.PGP

and second one I am trying to find a way to remove some character and make the name as following;
testfile200703005.ZIP

I hope I am making some sense.

What can I use to do this

Thanks again for your help
 
Did you make sense with my basename suggestion ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Something like this?

[tt]NEW_FILE=$(echo $INBOUND/$outfile | sed 's/.PGP//;s/_[0-9][0-9]*//')[/tt]

Annihilannic.
 
Something like (tested in Korn Shell):

NEW_FILE1="${INBOUND}$(echo $outfile | cut -f1 -d'.')"
or
NEW_FILE1="${INBOUND}$(echo $outfile | sed 's/.ZIP.PGP//')"

and
NEW_FILE2="${INBOUND}$(echo $outfile | cut -f1 -d'_').ZIP"

Note that ${INBOUND} is taken out of the 'character manipulation' of the filename just in case the path contains '.' or '_' characters.


I hope that helps.

Mike
 
Try [tt]basename[/tt] as PHV suggests. It's designed to chop off filename extensions for you. You don't have to worry about [tt]sed[/tt] or [tt]cut[/tt] or characters in the path. It just does it for you.

Something like...
Code:
#!/bin/ksh

for FILE in testfile*.ZIP.PGP
do
    cp $FILE $(basename $FILE .PGP)
    cp $FILE $(basename $FILE .ZIP.PGP)
done
Simple.

(A star for PHV)
 
Unfortunately basename doesn't help for the requirement of removing the _508400 component of the filename, since it doesn't seem to accept wildcards.

Annihilannic.
 
Great Thanks allot PHV & SamBones Basename will do the first part and Thanks to Mike042& Annihilannic for helping me with the second part

Best Regards
DJLongs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top