How to Use diff to Compare Two Files at Mac Command Line

Feb 6, 2018 - 4 Comments

Use diff to compare files at the command line

Need to quickly compare two files for differences? The command line ‘diff’ tool offers a great choice for users comfortable with the Terminal. Diff allows you to easily compare two files, with the command output reporting back any differences between the inputted files.

The diff command is available by default on the Mac, and it works the same in Linux and other unix operating systems as well, just in care you were wondering, and for Windows users it’s quite similar to how the ‘fc’ file compare tool works.

For best results you’ll want to be working with plain text files of some sort and not rich text. If need be you can always make a copy of the file and convert it to plain text via the textutil command line tool on the Mac, or even by using TextEdit.

How to Use Diff to Compare Files at the Command Line

The diff is a command line tool, thus you must first launch the Terminal app, found in /Applicaitons/Utilities/ and then you’re ready to begin.

The basic syntax for diff at the command line is as follows:

diff (file input 1) (file input 2)

For example, if in the present directory we want to compare bash.txt and bash2.txt, the syntax would look like the following:

diff bash.txt bash2.txt

The -w flag can be handy for plain text files because it tells diff to ignore white space when comparing files. And of course you can use a full path to the files to compare as well if need be, for example to compare an edited hosts file with another version elsewhere:

diff -w /etc/hosts ~/Downloads/BlockEverythingHosts.txt

Sample output may look something like the following:

$ diff -w /etc/hosts ~/Downloads/BlockEverythingHosts.txt

0a1

< ## < 127.0.0.1 localhost > # time for a break

9a12

> 127.0.0.1 facebook.com

The greater than and less than symbols serve as pointer arrows of sorts, indicating which file the difference originated from in relation to the order presented in the original command syntax.

Diff is quite powerful, you can also use diff to compare two directory contents, which can be helpful for verifying backups or file changes or file integrity.

Obviously diff requires the command line, but if you’d rather be in the familiar graphical interface of Mac OS when comparing documents there are various options available as well, including comparing two word documents with Microsoft Word or if you’re working with code and syntax, try the Xcode FileMerge tool, git, or even the excellent BBEdit text editor for Mac. And if you’re on windows, the ‘fc’ command works basically the same as the diff command, with ‘fc file1 file2’ achieving more or less the same comparison as diff.

Have any other tips for diff, or for comparing two files against one another? Share them below!

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

4 Comments

» Comments RSS Feed

  1. David55 says:

    Nice tip ! thank you, yep “Diff” very nice command

    you may want to use thoses flag’s for text file

    -a = Specify the content in the file is text
    &
    -y = Specify to show the output in 2 colomns

    as by ex ;

    diff -a -y a.txt b.txt

  2. Luiz says:

    If one wants a visual tool, try DiffMerge (https://sourcegear.com/diffmerge).
    Not the best in the world, but it’s free at least and does the job.

    On Windows, forget everything and just use WinMerge, the best one for ages IMO.

  3. Rob says:

    Kaleidoscope is a brilliant gui tool.
    It compares text files, images and folders.
    https://www.kaleidoscopeapp.com

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac