How To's

How to use the macOS command line to compare content

If on any occasion you have found yourself in the need to compare two directories (folders) to see which files may be different between them, you should know that there are tools that make your work easier instead of having to do it manually.

Although there are very valid third-party GUI tools, the macOS operating system has a free folder comparison utility built into every Mac. You will only need to launch the ‘Terminal’ command line to get it up and running.

The program is called diff and it is quite simple to use. You must start ‘Terminal’ in ‘Applications> Utilities’ and then use the command CD to change the directory that contains the folders you want to compare.

Folders can be located anywhere, of course, but it’s easier if they’re in the same folder. Once there, simply run the following command:

diff -rq folder1 folder2

This is a simple command with two switches on the command line (-rq).

RELATED:   How to share an iCloud calendar

The r indicates diff that looks at each directory recursively, including also the subdirectories that the folder may have.

The what switch is set diff as an abbreviation. If we don’t set the brief mode, diff It would not only tell you which files are different between the two folders, but it would also show the actual differences line by line for any text files that exist.

Since we are only interested in comparing the contents of the directories, we do not need that level of detail, so we will use the brief mode to suppress the more advanced process.

And that’s all you will need to do. This is how it looks in action (comments_newy comments_old) with the two folders compared):

% cd phpcode

% diff -rq comments_new comments_old

Only in comments_new: config.php

Only in comments_old: config_old.php

Only in comments_old: functions.inc

This is obviously a simple example, but it works just as well in a large folder with hundreds of files. If you want to do more with diffYou should know that it is capable of much more than simple folder comparisons.

Writes man diff on the command line if you want to know all its capabilities.

We have another interesting article in which we explain how to fix macOS command line errors.

Original article published in Macworld US.

Related posts

How to Manually Replace the Battery of MacBook Air 13?

vaibhav

How to update your Apple TV to the latest version of tvOS 15

vaibhav

How to play PC games on a Mac

vaibhav

Leave a Comment