compare the file in a directoryA with another directoryB


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare the file in a directoryA with another directoryB
# 1  
Old 06-26-2009
compare the file in a directoryA with another directoryB

Hi experts

i got a problem

currently i want to compare files in two different directories
eg
inside A got
1
2
3
4

inside B got
1
2
3

after compare, i wish to remove 1,2,3 inside the folder A

i try to google some code, like using diff, but i still cannot make up a success script to solve my problem. please help... =)
# 2  
Old 06-26-2009
Can you show us what have you tried so far and where you are stuck?

Regards
# 3  
Old 06-27-2009
md5sum are maybe different.
Code:
#!/usr/bin/ksh
dir1=xxx
dir2=yyy
now=$PWD
cd $dir2
for f in *
do
     cat $f  |  md5sum | read strA1 strA2
     [ ! -f $now/$dir1/$f  ] && print "$f not exist in dir1 " && continue
     cat $now/$dir1/$f | md5sum | read strB1 strB2
     [  "$strB1"  = "$strA1" ] && print "$f files are maybe same" && continue
     print "$f are not same"
done

If you like to use bash, then read must done using HERE. Not so readable.
# 4  
Old 06-29-2009
Quote:
Originally Posted by kshji
md5sum are maybe different.
Code:
#!/usr/bin/ksh
dir1=xxx
dir2=yyy
now=$PWD
cd $dir2
for f in *
do
     cat $f  |  md5sum | read strA1 strA2
     [ ! -f $now/$dir1/$f  ] && print "$f not exist in dir1 " && continue
     cat $now/$dir1/$f | md5sum | read strB1 strB2
     [  "$strB1"  = "$strA1" ] && print "$f files are maybe same" && continue
     print "$f are not same"
done

If you like to use bash, then read must done using HERE. Not so readable.
Hi kshji

very thanks for ur help,
i modify a little bit the script as below
Code:
#!/usr/bin/ksh
dir1=/A
dir2=/B
now=$PWD
cd $dir1
for f in *
do

     [ ! -f $now/$dir2/$f  ] && echo "$f not exist in B " && continue
     [  "$strA1"  = "$strB1" ] && rm -r $f && continue

done

This script work perfect for me...
besides that, may i know how $strA1 know it need to refer in dir1 and $strB! refer to dir2 ?
[ "$strA1" = "$strB1" ]

anyway really thanks for ur help...
me actually stuck in the diff command.. =)

---------- Post updated at 12:06 PM ---------- Previous update was at 11:59 AM ----------

Quote:
Originally Posted by Franklin52
Can you show us what have you tried so far and where you are stuck?

Regards

Hi Franklin52,

actually, me got try the for command for lopping
#!/bin/sh
for i in ls /A/*
do echo "looping ... $i
done

and google some script
like using diff

i am stuck when using
/usr/bin/find /A -name "*" -eq /B/* -exec rm -f {} \;

i tot can do it like that. Smilie

Last edited by SmartAntz; 06-29-2009 at 03:39 AM..
# 5  
Old 06-29-2009
below may help you some

Code:
function sub
{
	file=${1##*/}
	echo "File $1 exist, would like to Merger(1)/delete(2)?"
	read opt
	if [ $opt -eq "1" ];then
	  cat $1 >> $2/$file
	else
		rm $1
	fi
}
for file in `ls dir1`;do
  tmp=dir2/${file}
  if [ -f $tmp ];then
  	sub $tmp 'dir1'
  fi
done

# 6  
Old 06-29-2009
Quote:
Originally Posted by summer_cherry
below may help you some

Code:
function sub
{
    file=${1##*/}
    echo "File $1 exist, would like to Merger(1)/delete(2)?"
    read opt
    if [ $opt -eq "1" ];then
      cat $1 >> $2/$file
    else
        rm $1
    fi
}
for file in `ls dir1`;do
  tmp=dir2/${file}
  if [ -f $tmp ];then
      sub $tmp 'dir1'
  fi
done


Thanks summer_cherry.
I modified it for fulfill my requirement.
Code:
#!/bin/sh               
for file in `ls /B`;do         
tmp=/A/${file}         
if [ -f $tmp ];then               
rm -r $tmp         
fi       
done

This help me a lot. =)
thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

3. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

4. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

5. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. UNIX for Dummies Questions & Answers

Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help File 1 123 232 W343... (6 Replies)
Discussion started by: KevinRidley
6 Replies

9. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question