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 biv343 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
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
may be with [tt]find[/tt] and [tt]awk[/tt] in order to copy the files adding the desired PIN, RP or CC to the file name (using awk of course).

something like:

find -f -name "FILE.ZIP.*" | awk 'FS="." {system("cp $0 /usr/collection/$1.$2.PIN.$3"}'

in /usr/local/tmp/dir1



Chacal, Inc.[wavey]
 
PHV, not very much to show as far as code.
#!/bin/ksh -x
DIRECTORIES=/usr2/scripts/rename_files

for files in `cat $DIRECTORIES`
do

find $files -name FILE.ZIP.*

done

I am just trying to figure out a way to "know" when to implement a CC, RP or PIN. Still working on that.

Chacalinc,
I am getting errors using that command. But thanks, gives a hint to work on.


 
#!/bin/ksh
DIRECTORIES=/usr2/scripts/rename_files

for name in `cat $DIRECTORIES`
do
for files in `ls -1 $name`
do
find /usr/collection -name $files
if [ $? -eq 0 ]
then

fi
echo $files
done
done

This will get me to a point to find out which files that needs to be renamed. Only problem is how do I split the file into FILE.ZIP.01252006-102055 to $1.$2.$3 and insert txt between $2 & $3?

$1 = FILE
$2 = ZIP
$3 = 01252006-102055
 
Only problem is how do I split the file into FILE.ZIP.01252006-102055 to $1.$2.$3 and insert txt between $2 & $3?

Using [tt]awk[/tt].

Chacal, Inc.[wavey]
 
CHANGE=`echo $files | awk '{ FS = "." ; print $1"." $2 ".CC." $3}'`

This works, but this code is gonna get ugly hehe.
 
find -f -name "FILE.ZIP.*" | awk 'FS="." {system("cp $0 /usr/collection/$1.$2.PIN.$3"}'
find: path-list predicate-list
awk: syntax error near line 1
awk: bailing out near line 1

I did find in the code its missing a ) at the end of the code.
Ive been playing around with but with no luck.
 
oooh... the problem is the find command, not awk.. test this:

[tt]find [red]/usr/local/tmp/dir1[/red] -f -name "FILE.ZIP.*" | awk 'FS="." {[red]print[/red]("cp $0 /usr/collection/$1.$2.PIN.$3"}'[/tt]



Chacal, Inc.[wavey]
 
Typed, untested:
#!/bin/ksh
DIRECTORIES=/usr2/scripts/rename_files
find $(<$DIRECTORIES) -name 'FILE.ZIP.*' | awk -F. '
{dir=""}
/\/dir1\//{dir="PIN"}
/\/dir3\//{dir="CC"}
/\/dir4\//{dir="RP"}
dir!=""{print "cp "$0" /usr/collection/FILE.ZIP."dir"."$NF}
' | ksh -x

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
find /usr/local/tmp/dir1 -f -name "FILE.ZIP.*" | awk 'FS="." {print("cp $0 /usr/collection/$1.$2.PIN.$3"}'
find: bad option -f
find: path-list predicate-list
awk: syntax error near line 1
awk: bailing out near line 1


Here's the result
Im using Solaris 9
I have looked in the man pages, there is no such options as -f
 
umm.. ok, -f is for "files only", you can get it out from the command.

Chacal, Inc.[wavey]
 
Or use the -type f predicate.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Very nice PHV.
Just one thing, Everytime this script runs, it updates the the files in /usr/collection - means it does a copy.

Are there any nifty way not to do the copy if the the find command can't find a match in /usr/collection?

in other words, if found file in /dir1 but doesnt exist in /usr/collection = File has already been re-named and exist
 
Replace this:
dir!=""{print "cp "$0" /usr/collection/FILE.ZIP."dir"."$NF}
with something like this:
dir!=""{print "[ -f /usr/collection/FILE.ZIP."dir"."$NF" ] || cp "$0" /usr/collection/FILE.ZIP."dir"."$NF}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
you should add an if statment before the copy command. I fmy memory serves me, it should be [tt]if -a /target/path/file_name[/tt] (if the file exists then...).

Cheers.

Chacal, Inc.[wavey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top