- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: How to Compare Files one by one using Shell Sc...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 12:31 PM
04-21-2008 12:31 PM
I have two directories and I wanted to compare files one by one in these directories using shell script. Can anybody help how should I start looping through files in each directory and compare these line by line and get the difference?
Thanks
Padma
Solved! Go to Solution.
- Tags:
- diff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 12:41 PM
04-21-2008 12:41 PM
Re: How to Compare Files one by one using Shell Script
you're going to have to be a little more specific...
Compare file names only?
Compare like named files?
Text only, or are there binaries, such as executables?
Desired output?
"man diff".....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 12:45 PM
04-21-2008 12:45 PM
Re: How to Compare Files one by one using Shell Script
These are the text files and I mentioned that I wanted to compare these files line by line.
Regards
Padma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 12:49 PM
04-21-2008 12:49 PM
Re: How to Compare Files one by one using Shell Script
Have a look at the manpages for 'diff' and for 'dircmp':
http://docs.hp.com/en/B3921-60631/diff.1.html
http://docs.hp.com/en/B3921-60631/dircmp.1.html
Regards!
...JRF...
- Tags:
- dircmp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 12:53 PM
04-21-2008 12:53 PM
Re: How to Compare Files one by one using Shell Script
Assumptions:
1) both directories have all the same number of files, only their content may be different.
2) both directories are siblings of the same parent directory.
3) There are no subdirectories that you want to scan.
4) There are no missing files that you are, or are not trying to compare.
5) The two sibling directories are named "a" and "b". And they are children of the current directory "."
$> cd a
for i in *
do
diff $i ../b/$i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 01:01 PM
04-21-2008 01:01 PM
Re: How to Compare Files one by one using Shell Script
a call of
dircmp -d -s dira dirb
will
- no identical file
- flag files only present in one of the directories
- list a diff-report of identical filenames in both directories
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 01:08 PM
04-21-2008 01:08 PM
Re: How to Compare Files one by one using Shell Script
I forgot all about dircmp.
> Do what they say, use dircmp.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2008 01:13 PM
04-21-2008 01:13 PM
Re: How to Compare Files one by one using Shell Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 09:22 AM
04-22-2008 09:22 AM
Re: How to Compare Files one by one using Shell Script
Some more clarification. Both the directories have some files common and also files which are in one directory but not in other. How to get the list of uncommon files?
Secondly I am not sure i m not able to find dircmp on my linux box.
Thanks
Padma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 09:28 AM
04-22-2008 09:28 AM
Re: How to Compare Files one by one using Shell Script
WARNINGS
This command is likely to be withdrawn from X/Open standards. Applications using this command might not be portable to other vendors' systems. As an alternative diff -R is recommended.
You might want to take a look at diff -R.
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 09:36 AM
04-22-2008 09:36 AM
Solution> Both the directories have some files common and also files which are in one directory but not in other. How to get the list of uncommon files?
Using 'diff' would show you this automatically. Consider:
# ls /tmp/dir1 /tmp/dir2
/tmp/dir1:
hosts inetd.conf
/tmp/dir2:
hosts services
# diff /tmp/dir1 /tmp/dir2
17c17
< 127.0.0.1 LOCALHOST loopback
---
> 127.0.0.1 localhost loopback
Only in /tmp/dir1: inetd.conf
Only in /tmp/dir2: services
#
...at worst, you could 'grep -v "^Only in"' to filter out the unmatched files; or 'grep "^Only in"' to find them!
> Secondly I am not sure i m not able to find dircmp on my linux box.
It probably isn't. In fact, the manpages for 'dircmp' note, "This command is likely to be withdrawn from X/Open standards. Applications using this command might not be portable to other vendors' systems. As an alternative diff -R is recommended."
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 10:09 AM
04-22-2008 10:09 AM
Re: How to Compare Files one by one using Shell Script
it does work.
Regards
Padma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 10:12 AM
04-22-2008 10:12 AM
Re: How to Compare Files one by one using Shell Script
I was not able to find diff -R and I just used diff
diff -R is not supported on my Linux box. I am using Fedora Linux.
Thanks
Padma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 10:27 AM
04-22-2008 10:27 AM
Re: How to Compare Files one by one using Shell Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2008 11:31 AM
04-22-2008 11:31 AM
Re: How to Compare Files one by one using Shell Script
> diff -R is not supported on my Linux box. I am using Fedora Linux.
No, but:
# diff -r
...and I'm running Fedora 8 in this case.
Your first line of defense is to do:
# man diff
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2008 01:31 AM
04-23-2008 01:31 AM
Re: How to Compare Files one by one using Shell Script
You can just list both directories and use comm(3) to get this:
$ ls dir1 > dir_files.1
$ ls dir2 > dir_files.2
# Gives files only in dir1
$ comm -23 dir_files.1 dir_files.2
# Gives files only in dir2
$ comm -13 dir_files.1 dir_files.2
# Gives files that aren't in both:
# Ones in dir2 are indented
$ comm -3 dir_files.1 dir_files.2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2008 06:34 AM
04-23-2008 06:34 AM
Re: How to Compare Files one by one using Shell Script
I'm guessing you've realized it by now - but you're getting "HPUX" answers because you've posted in the HPUX forum. There is a Linux forum, in which you're much more likely to get Linux specific help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2008 06:40 AM
04-23-2008 06:40 AM
Re: How to Compare Files one by one using Shell Script
> but you're getting "HPUX" answers because you've posted in the HPUX forum
There was never a specification of what operating system but I think we have covered the options available to (L)UNIX at large.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2008 04:15 AM
04-28-2008 04:15 AM
Re: How to Compare Files one by one using Shell Script
This one I mostly use:
for i in `find /tmp/dir1 -type f|awk -F/ '{print $NF}'`
do
find /tmp/dir2 -type f -exec ll {} \; |grep $i
done
Can be ported to any UNIX.
Regards,
Rasheed Tamton.