HeatWare.net header image
HeatWare.net
TwitterRSSEmail
  • Home
  • Windows
    • Windows 8
    • Windows 7
    • Windows Vista
    • Windows XP
  • Linux/Unix
  • Mac
  • Mobile
    • Android
    • iOS
    • Phones (Help / Resources)
  • Software / Programming
    • Free Software
    • Programming – General
    • PHP
    • Ruby/Rails
    • Quality Assurance (QA)
    • Software – General
    • Software Help
    • Databases
  • Cool Websites
  • Other
    • Deals & Bargains
    • News
    • Video Games
    • Hardware
    • Electronics
  • About

0 comments / April 2, 2009 / sood / Linux/Unix

How to compare 2 files using ‘diff’ in Linux

This article will introduce you to the diff command on Linux which is used to compare two files and display the “differences” between the two. I will also provide you with a Bash shell script to compare two files and simply output if they are the same or not. Lets begin with the basic usage of diff

Using Diff to Compare 2 Files

diff filename1 filename2

Simply provide the two files you want to compare to the diff utility and the resulting output will be a list of differences between the two files. If the two files are exactly the same, the output will be blank.

There are some useful parameters that can be passed into diff that you might like to use:

-b Ignore differences in whitespace (For example: If the beginning of line 1 has 1 space in file1, and 2 spaces in file2, this difference will be ignored.

-B Ignore additional or missing blank lines. This works similar to the previous options but applies to lines instead of spaces

Example Bash Script

#!/bin/sh
if diff $1 $2 >/dev/null ; then
echo Files are Same
else
echo Files are Different
fi

This bash script will compare two files that you specify as parameters and simply output if they are the same or not. In some cases, you do not need to know the details of the differences but just whether they are different or not and this script does just that. Simply save this script into your system (lets call it heatware_diff.sh) and run it with the following syntax:

heatware_diff.sh filename1 filename2

Related Posts

  • How to Find Files Owned by Group(s) in Linux
  • Run a script on Linux startup in 2 simple steps
  • Using 'tail' to output updates/appendings to a file in Linux
  • How to Find Files Owned by Users(s) in Linux
  • Linux - How to find the number of files in a folder?

Linux

0 comments… add one
Cancel reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Why You Should Consider Diving Apps
  • How to Buy a Smartphone that Fits Your Budget
  • How to Overcome Frustrating PDF Stress
  • Convert PDF to Word: Easy, Reliable and Quality Conversion
  • PDF to Excel App ­ – A New Way of Handling Tricky Documents

Recent Comments

  • Valarie Walter on Basic Troubleshooting Steps for your Cell Phone
  • John Mists on A Brief History of Android OS
  • syarif on PostgreSQL: How to reload config settings without restarting database
  • Raghu on How to SSH to a server using Ruby – Part I
  • francisco clemente on Basic Troubleshooting Steps for your Cell Phone

Tags

ACSLS Android Bargain Cell Phones Cool Software Cool Websites Databases Deals Ebooks Facebook Free Software G2x Galaxy S5 iOS iPhone Kindle LG Linux Linux/Unix Mac Mobile mysql Office OpenStack OS X PHP Postgres PostgreSQL ruby Samsung Galaxy S6 Shell Smartphones Sun T-Mobile Tips Tips & Tricks Ubuntu Unix Virtualization VMWare Windows Windows 7 Windows 8 Windows Vista Windows XP

Latest Tweet

Follow @HeatwaredotNet

SP
@HeatwaredotNet

  • Why You Should Consider Diving Apps https://t.co/Is41cdUv2I #diving-apps
    about 6 years ago

All Categories

Other Links

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2015 — HeatWare