Comparing 2 UNIX directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing 2 UNIX directories
# 1  
Old 03-27-2016
Comparing 2 UNIX directories

Hello,

I'd want to compare the content of 2 directories in unix.
I use the diff command like this:

diff /home/user/AAAAA /home/user/BBBBB

It works fine, but when a same file is in both directories and they are diferents, I'd want to see only that it is diferent and not all diferencies.

I tried:

diff -c but it doesn't works.

Could anyone help me please ?

thanks in advance
# 2  
Old 03-27-2016
If GNU diff is available to you, you could use the --brief option to do what you want.
# 3  
Old 03-27-2016
Would
Code:
cmp -s

do?
# 4  
Old 03-27-2016
Or the long way.
Code:
for file in `ls dir1`
do
if [ -r dir2/$file ]
then
        diff $file dir2/$file >/dev/null
        if [ $? -eq 1 ]
                echo $file is diff
        else
                echo $file is same in both directories
        fi
else
        echo $file does not exist id dir2
fi
done

then reverse dir1 and dir2 to find files that exist in dir2 that do not exist in dir1
# 5  
Old 03-27-2016
Quote:
Originally Posted by jgt
Or the long way.
Code:
for file in `ls dir1`
do
if [ -r dir2/$file ]
then
        diff $file dir2/$file >/dev/null
        if [ $? -eq 1 ]
                echo $file is diff
        else
                echo $file is same in both directories
        fi
else
        echo $file does not exist id dir2
fi
done

then reverse dir1 and dir2 to find files that exist in dir2 that do not exist in dir1
This could be a poor choice, since it will break if file names contain spaces.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two directories with diff

Hi all, I have 2 directories on two different servers. I am trying to find out what is missing from directory X and what is missing from directory Y. they should both have the same exact files in them. I understand some files may be missing from both directories on each server. I am not sure... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. UNIX for Dummies Questions & Answers

Comparing 2 UNIX files

Hi, I am planning to automate the comparison of data between few tables in 2 different databases ( Teradata and sql server). Below is the approach which I think of. Please suggest any improvements/Modification : 1) The sql server file is having more records and I have to eliminate the... (5 Replies)
Discussion started by: Rahul Raj
5 Replies

3. Shell Programming and Scripting

Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (4 Replies)
Discussion started by: shadow_fawkes
4 Replies

4. Shell Programming and Scripting

**URGENT ** : Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (1 Reply)
Discussion started by: shadow_fawkes
1 Replies

5. Shell Programming and Scripting

Script for Comparing directories and file from a text file

Hello all, I need to write a script which has following requirement: Need to read the filenames from text file and then search for the above read files in the required directory and if match found backup them in a backup folder. And also need to compare and verify whether the files in the... (7 Replies)
Discussion started by: saurau
7 Replies

6. Shell Programming and Scripting

Comparing the modified dates of files in two directories

Hi Is it possible to compare the modified dates of all the files in two directories using shell script? I would like to take a backup of a directory in production server regularly. Instead of copying all the files in the directory, is it possible to list only the files that are... (2 Replies)
Discussion started by: ashok.k
2 Replies

7. Homework & Coursework Questions

comparing 2 directories

i have been asked to write a bash shell script comparing two directories and sed or awk should not be used in this assignment. compdir will compare filenames in two directories, and list information about filenames that are in one directory but not the other. The information listed will be a long... (1 Reply)
Discussion started by: soccerball
1 Replies

8. Shell Programming and Scripting

Comparing directories on different unix servers

Is it possible to use the diff command to compare two directories on different Unix (AIX) servers? We have two regions set up and we want to be able to compare if the scripts directory in both regions contain the same files? I want to figure out if its possible.. Have been messing around by... (10 Replies)
Discussion started by: Jazmania
10 Replies

9. UNIX for Dummies Questions & Answers

comparing the content of two directories

Hello I want to compare the content of two directories recursively to check if the two directories have the same files. How can I do that? (2 Replies)
Discussion started by: xyzt
2 Replies

10. UNIX for Dummies Questions & Answers

Comparing directories via ftp

Hello! I am trying to compare a list of files in 2 directories - one on our unix server (I'll call it 'ours') and one on a site we ftp to (I'll call it 'ftp'). I need to make sure that after we ftp, the names that we put out there match the names we have on our side. I was thinking to create a... (1 Reply)
Discussion started by: tekster757
1 Replies
Login or Register to Ask a Question