How to compare files in two folders using cmp?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare files in two folders using cmp?
# 1  
Old 09-22-2010
How to compare files in two folders using cmp?

i recently copied 400GB of data from a NTFS drive to a ext4 drive. I want to verify that the data is 100% identical to the original.

I wanted to use cmp but it only does two files. The directory that was copied contains many subdirectories and all sorts of files (not just text).

So I guess what I want to know is:

1. Is CMP the right command to use to compare (verify identical data) of two files regardless of its type? eg video, document, executable, zip, encrypted file etc

2. How would you write a quick shell script to compare two folders?

So far i thought of two ways:
1. supply the directories of each:
Source: /media/ntfsdrive/data
New Copy: /media/ext4drive/data

But this seems difficult.

2. run find . in each directory (source and the new copy) - should b exactly the same if you output to a file and do a diff.
some how run CMP for each listing.

This seems difficult too but some what easier. I have no shell programming experience though, so does anyone know a quick way of doing this? Any help would be highly appreciated. Thank you!
# 2  
Old 09-22-2010
Quote:
Originally Posted by fuzzylogic25
...
1. Is CMP the right command to use to compare (verify identical data) of two files regardless of its type? eg video, document, executable, zip, encrypted file etc
Yes, 'cmp' does a byte by byte comparison of two files.

Quote:
Originally Posted by fuzzylogic25
2. How would you write a quick shell script to compare two folders?
One way would be ...
Code:
# Build first the lists to be compared:

	find /media/ntfsdrive/data/ /media/ext4drive/data/ -type f  |
               awk '/\/media\/ntfsdrive\/data\// { print > "list_1" }
                    /\/media\/ext4drive\/data\// { print > "list_2" }' 
	
# Do the actual comparison with 'cmp', ( assuming no whitespaces in the filenames ):

	 awk '{ getline s < "list_2"; print "cmp", $0, s }' list_1 | sh

Note that one list's filenames have to be in precise order with their counterparts in the other list, for 'cmp' to work correctly.
# 3  
Old 09-22-2010
find plus md5sum should do the trick:
Code:
cd /media/ntfsdrive/data; find . -type f -exec md5sum {} \+ | ( cd /media/ext4drive/data; md5sum -c --quiet )

# 4  
Old 09-22-2010
If both directory trees are intended to be identical (no files in one that are not in the other):
Code:
diff -r /media/ntfsdrive/data /media/ext4drive/data

If both trees are identical, diff will be silent and not generate any output; its exit status will be zero.

Regards,
Alister

---------- Post updated at 07:07 PM ---------- Previous update was at 06:53 PM ----------

Another option using find and cmp which ignores any extra files at the destination tree (so long as each file was copied correctly we're happy):
Code:
find /media/ntfsdrive/data -type f -exec sh -c '
    for f; do
        cmp "$f" /media/ext4drive"${f#/media/ntfsdrive}"
    done
' sh {} +

Regards,
Alister
# 5  
Old 09-22-2010
Alistair, i wanted to use diff command because it can do things recursively, but isnt that only for text files? I mean it says it does a line by line comparison, where cmp does byte by byte. I am dealing with alot of non-text files as well.
# 6  
Old 09-23-2010
It can compare binary files, though it may not actually generate a diff if they differ; it may simply print the two filenames and the word "differ" to let you know, along with returing a non-zero exit status.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

3. Shell Programming and Scripting

Linux Script to compare two folders and copy missing files

Hi, I need help in shell scripting. If someone can help me, that would be great! Problem. I want Linux Script to compare two folders and copy missing files. Description. I have two directories /dir1 /dir2 I need to copy all distinct/new/unique/missing files from /dir1 and that... (1 Reply)
Discussion started by: S.Praveen Kumar
1 Replies

4. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

5. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

6. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

7. Shell Programming and Scripting

Compare the checksum of files in 2 different folders

Hi I have 2 different folders on different machines. they are supposed to be same but some time for unknown reason they are not. then we have to generate a report for files which are not matching. I was doing as below - cd folder1 find . -type f | sort | cksum >1.txt cd folder2 find .... (7 Replies)
Discussion started by: reldb
7 Replies

8. Shell Programming and Scripting

Issue in comparing (cmp) two files

Hi All, I have to compare set of files so I created a case statement with the option to give more than one file to compare. The Problem now i am facing is, if I compare the files directly, from prompt or just using the script only for a particular file then It's saying No difference, but If I... (4 Replies)
Discussion started by: Sudhar
4 Replies

9. Shell Programming and Scripting

Compare 2 folders...

Hello, I try to compare 2 folders, i explain, i have file in this 2 folder and i want to print out the difference in this folders... ex: folder1: file1 file2 file3 folder2: file1 file2 print file3 I do a ls of the 2 folders and i use the command diff (diff $var1 $var2) without... (8 Replies)
Discussion started by: protocomm
8 Replies

10. Shell Programming and Scripting

CMP two files with slight difference and return code

I am comparing two files which are identical except for the timestamp which is incorporated within the otherwise same 372 bytes. I am using the command: cmp -s $Todays_file $Yesterdays_file -i 372 When I run the command without the -i 372 it shows the difference i.e. the timestamp.... (5 Replies)
Discussion started by: gugs
5 Replies
Login or Register to Ask a Question