Operating System - HP-UX
1835346 Members
3131 Online
110078 Solutions
New Discussion

Get differences between files and removing oldest if different

 
Dave Walley
Frequent Advisor

Get differences between files and removing oldest if different

Hi. I need some help, I have a job which generates reports on a regular basis, what I need to do is loop through the files and compare each one with the newest one and if identical I would like to remove the older version. There will be an unknown number of files. Here is an example listing

/pfile/init_dba18_15491713042005.ora newest
/pfile/init_dba18_15320613042005.ora
/pfile/init_dba18_15251313042005.ora
/pfile/init_dba18_15191313042005.ora

I would like to remove the files with no changes and only keep the newest of the unchanged ones.

Thanks... Dave
why do i do this to myself
3 REPLIES 3
Steven E. Protter
Exalted Contributor

Re: Get differences between files and removing oldest if different

I would suggest the diff command.

diffs=$(diff file1 file2 | wc -l)

You will need a loop to process all the files.

if [ $diffs -ge 1 ]
then
rm file2
fi

Its a start.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Peter Godron
Honored Contributor

Re: Get differences between files and removing oldest if different

Dave,
use ls -lt to get the files in creation order
Use the first (newest file) with head and do a diff aginst the next file (second newest).
If the result is no diff then rm the second file, repeat until all files are done.

Regards
Peter Godron
Honored Contributor

Re: Get differences between files and removing oldest if different

Dave,
use ls -lt to get the files in creation order
Use the first (newest file) with head and do a diff aginst the next file (second newest).
If the result is diff then rm the second file, repeat until all files are done.

Regards