Operating System - HP-UX
1837090 Members
2192 Online
110112 Solutions
New Discussion

Re: comparing and deleting a files

 
ajayajay
Occasional Advisor

comparing and deleting a files

HI,i am new to perl

I have 2 Directories a & b. I need to compare these 2 Directories if both directories containts same files dont delete a files if one of the directory containts extra files than delete that file.
using PERL script

Can anyone please help me do this.

9 REPLIES 9
Peter Godron
Honored Contributor

Re: comparing and deleting a files

Hi,
and welcome to the forums !

In HPUX it would be a simple diff giving the directories ats parameters (seem "man diff").

In PERL I would look at:
http://search.cpan.org/~gavinc/File-DirCompare-0.02/DirCompare.pm

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
ajayajay
Occasional Advisor

Re: comparing and deleting a files

hi sir,
Comparing two directories is working fine, but now i wanted to delete a file which is not there in another directory....
Peter Godron
Honored Contributor

Re: comparing and deleting a files

Hi,
If you the diff command it will show files that are unique to each directory:
Only in test2: b.lis
Only in test: c.lis

So I would redirect the output of the diff and then grep on the directory you want to 'clean up'. In my example test2.
grep -e'Only in test2' diff.lis | awk -F':' '{print $2}'

ajayajay
Occasional Advisor

Re: comparing and deleting a files

hi sir,
it showing fils perl/temp/a/test2 and perl/temp/test2/b/test2 are common and similar.
file unique only in
perl/temp/a/test1

i am understand that 2 paragraph wat u written.... i am new to perl.

i wanted to delete perl/temp/a/test1
from that directory..
Dennis Handly
Acclaimed Contributor

Re: comparing and deleting a files

Unless you want to be recursive, you could do:
$ ls dir1 > dir1.out
$ ls dir2 > dir2.out
$ comm -23 dir1.out dir2.out # extra files in dir1
$ comm -13 dir1.out dir2.out # extra files in dir2

If you want to remove the extra files in dir1 and you have debugged this script:
$ rm -f $(comm -23 dir1.out dir2.out)

(This doesn't compare the content of the files, just names.)
ajayajay
Occasional Advisor

Re: comparing and deleting a files

hi sir,
i am writting this script in windows.....
Dennis Handly
Acclaimed Contributor

Re: comparing and deleting a files

>i am writing this script in windows.

You are in a HP-UX forum so it is expected that you have a real shell: sh or ksh. If you want some perl help, you could ask under languages and scripting:
http://forums1.itrc.hp.com/service/forums/categoryhome.do?categoryId=150

You can ask the moderator to move it by replying to this thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1114353
ajayajay
Occasional Advisor

Re: comparing and deleting a files

ok thank you sir,
Hein van den Heuvel
Honored Contributor

Re: comparing and deleting a files

>> >i am writing this script in windows.

There is a cute toy avaiable for free download from Microsoft which will do all you scripts might suggest to do and then some...

google: microsoft synctoy download

It will likely lead to:

http://www.microsoft.com/downloads/details.aspx?familyid=E0FC1154-C975-4814-9649-CCE41AF06EB7&displaylang=en

Hein.