LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-23-2012, 01:54 AM   #1
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Rep: Reputation: Disabled
Compare two files?


Hi Sir/Hello Friends,

My question is, there is one configuration file which had written some day before. Now i have modified that file by adding some more lines of code. Now my task is to compare and check where all i modified that file. Is there any method in linux to compare those files. Can any one help me out.

Regards,
Shahid Nx
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-23-2012, 01:56 AM   #2
descendant_command
Senior Member
 
Registered: Mar 2012
Posts: 1,426

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Code:
diff file1 file2
and 'man diff' for options
 
Old 04-23-2012, 02:05 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Another command you might want to look at: comm
Code:
comm <options> file1 file2
Hope this helps.
 
Old 04-23-2012, 02:15 AM   #4
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
But the out put that diff cmd is giving i m not getting that.

is it possible in linux where it compares the two files and display only lines that are not present in old file. means if old file is of 100 lines and in new file i added 10 more lines to it. So is there any way to display only these added lines. Plz share if any idea
 
Old 04-23-2012, 02:16 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
My question is, there is one configuration file which had written some day before. Now i have modified that file by adding some more lines of code.
Did you make the changes in that same file? Did you take backup of that file before making changes?
 
Old 04-23-2012, 02:17 AM   #6
k3lt01
Senior Member
 
Registered: Feb 2011
Location: Australia
Distribution: Debian Wheezy, Jessie, Sid/Experimental, playing with LFS.
Posts: 2,900

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
Install Meld, open the application tell it what files you want to compare and it will show you what lines are different. I use it alot for optimising package lists.
 
Old 04-23-2012, 02:18 AM   #7
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Yes sir i have taken the back up of that file. nw i have both old and new file.
 
Old 04-23-2012, 02:19 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Both files need to be sorted, otherwise both comm and diff won't work (as stated in the man pages).

If the files are sorted, comm can do what you want/need:
Code:
comm -3 file1 file2
See man page for details.

If the files cannot be sorted: Have a look at k3lt01's suggestion.

Hope this helps.
 
Old 04-23-2012, 02:21 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And as a generic suggestion:
Code:
man -k search_term| egrep '\(1|8\)'
In your case:
Code:
 man -k compare| egrep '\(1|8\)'
bzcmp []             (1)  - compare bzip2 compressed files
bzdiff []            (1)  - compare bzip2 compressed files
cmp []               (1)  - compare two files byte by byte
comm []              (1)  - compare two sorted files line by line
compare []           (1)  - mathematically and visually annotate the difference between an image and its reconstruction
diff []              (1)  - compare files line by line
diff3 []             (1)  - compare three files line by line
git []               (1)  - diff-files - Compares files in the working tree and the index
git []               (1)  - diff-index - Compares content and mode of blobs between the index and repository
git []               (1)  - diff-tree - Compares the content and mode of blobs found via two tree objects
infocmp []           (1)  - compare or print out terminfo descriptions
ldapcompare []       (1)  - LDAP compare tool
lzcmp []             (1)  - compare LZMA compressed files
lzdiff []            (1)  - compare LZMA compressed files
msgcmp []            (1)  - compare message catalog and template
ndiff []             (1)  - Utility to compare the results of Nmap scans
ntfscmp []           (8)  - compare two NTFS filesystems and tell the differences
rcsdiff []           (1)  - compare RCS revisions
test []              (1)  - check file types and compare values
tiffcmp []           (1)  - compare two TIFF files
zcmp []              (1)  - compare compressed files
zdiff []             (1)  - compare compressed files
zipcmp []            (1)  - compare contents of zip archives

Cheers,
Tink
 
1 members found this post helpful.
Old 04-23-2012, 02:22 AM   #10
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
You can also get the clear-cut visual difference between two files using this command -

Code:
# sdiff file1.txt file2.txt
No need to install any other software/package.
 
2 members found this post helpful.
Old 04-23-2012, 02:33 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,647

Rep: Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501Reputation: 2501
you can try: diff file1 file2 | grep '^<' or diff file1 file2 | grep '^>'
 
Old 04-23-2012, 03:59 AM   #12
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
@Satyaveer Arya: Yes sir sdiff command helps,i got my output.

Thanks To all.
 
Old 04-23-2012, 04:15 AM   #13
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Glad to know the the problem solved out and the command worked out for you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compare 2 files name specialized Linux - Newbie 2 03-07-2010 04:20 PM
compare two files tekmann33 Linux - Newbie 2 10-28-2008 08:09 AM
compare two files haydar68 Programming 8 08-16-2008 03:36 PM
Compare files in specific directory to files on CD szim90 Linux - Newbie 3 02-10-2007 08:39 PM
How do I compare 2 files? linuxhippy Slackware 6 03-26-2005 01:54 AM


All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration