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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find - Compare - Rename Files. 1

Status
Not open for further replies.

Cimm

Technical User
Feb 17, 2005
69
US
Hi, a small problem here, and I need some advise how this can be done.

I have a collection directory that is getting files from many different directories, unfortnatly with the same file name, expect for the time-stamp. The time stamp is the only way to find out which directory it came from.

I want to make it easier to see where the file came from.

Here's a file structure layout.

/usr/local/tmp/dir1 = PIN
/usr/local/tmp/dir3 = CC
/usr/local/tmp/dir4 = RP


DIRECTORY STRUCTURE VIEW:

/usr/collection/
FILE.ZIP.01252006-102055
FILE.ZIP.01252006-102555
FILE.ZIP.01252006-103055
FILE.ZIP.01252006-103555
FILE.ZIP.01252006-102655
FILE.ZIP.01252006-103655
FILE.ZIP.01252006-104655
FILE.ZIP.01252006-102155
FILE.ZIP.01252006-102155
FILE.ZIP.01252006-102155


/usr/local/tmp/dir1
FILE.ZIP.01252006-102055
FILE.ZIP.01252006-102555
FILE.ZIP.01252006-103055
FILE.ZIP.01252006-103555


/usr/local/tmp/dir3
FILE.ZIP.01252006-102655
FILE.ZIP.01252006-103655
FILE.ZIP.01252006-104655

/usr/local/tmp/dir4
FILE.ZIP.01252006-102155
FILE.ZIP.01252006-102155
FILE.ZIP.01252006-102155


Wanting results:

/usr/collection/
FILE.ZIP.PIN.01252006-102055
FILE.ZIP.PIN.01252006-102555
FILE.ZIP.PIN.01252006-103055
FILE.ZIP.PIN.01252006-103555
FILE.ZIP.RP.01252006-102655
FILE.ZIP.RP.01252006-103655
FILE.ZIP.RP.01252006-104655
FILE.ZIP.CC.01252006-102155
FILE.ZIP.CC.01252006-102155
FILE.ZIP.CC.01252006-102155

/usr/local/tmp/dir1
FILE.ZIP.01252006-102055
FILE.ZIP.01252006-102555
FILE.ZIP.01252006-103055
FILE.ZIP.01252006-103555


/usr/local/tmp/dir3
FILE.ZIP.01252006-102655
FILE.ZIP.01252006-103655
FILE.ZIP.01252006-104655

/usr/local/tmp/dir4
FILE.ZIP.01252006-102155
FILE.ZIP.01252006-102155
FILE.ZIP.01252006-102155

I am trying to make this to a function in a script, where I can add more directories that has files I want in my /usr/collection directory. A seperate scripts is fine as well.

Any suggestions how this can be done? Or any advises?

Thanks in advance.

Ps, the files from the orginal directory cannot be changed or touched, just copied to /usr/collection
 
Thank you very much PHV, that worked like a charm!.
 
Wouldn't it have be easier to change the the app that produces the file in the first place?

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
mrn, probably. But that isnt an option in this case unfortnatly.

PHV, I am wondering if it possible make this nice code little more dynamic?

There are other files with different names. Like if we could make FILE dynamic, so it will work for all kind of files.

I have come that far to understand that $2 gives me ZIP , $3 gives me time-stamp , $1 gives me the whole path plus FILE.

*.ZIP."Time-stamp"

I guess I am looking to strip off FILE from the path.

Any suggestions?
Thanks
 
Thanks Chacalinc, here's the complete source for those who wonders how the final code looked like.


#!/bin/ksh -x
DIRECTORIES=/usr2/scripts/rename_files
find $(<$DIRECTORIES) -name '*.ZIP.*' | awk -F. '
{dir=""}
/\/dir1\//{dir="PIN"}
/\/dir3\//{dir="CC"}
/\/dir4\//{dir="RP"}
#dir!=""{print "cp "$0" /usr/collection/$NF.ZIP."dir"."$NF}
#dir!=""{print "[ -f /usr/collection/$NS"."ZIP."dir"."$NF" ] || cp "$0" /usr/collection/$NS.ZIP."dir"."$NF}
dir!=""{print "[ -f /usr/collection/`basename "$0"`."dir"."$NF" ] || cp "$0" /usr/collection/`basename "$1"`.ZIP."dir"."$NF}
' | ksh -x
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top