Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two directories using diff while ignoring non-existing files?

Tags:

I would like to use diff to compare two directories for differing files, using the -q option for brief output. However, the output is cluttered with a lot of files that only exist in one directory, but not the other. Can I force diff (or use another tool) to only show files that differ AND exist in both directories?

The current command I use is

diff -q <dir1> <dir2> 

Any ideas are appreciated.

like image 376
Michael Schlottke-Lakemper Avatar asked Jul 04 '12 08:07

Michael Schlottke-Lakemper


People also ask

How do I compare two directories in differences?

Click on the “Select Files or Folders” tab in the far left, to start a new comparison. Each comparison you run opens in a new tab. To start a new comparison, click on the “Select Files or Folders” tab in the far left, change the targets and click “Compare” again.

Can you use diff on directories?

You can use diff to compare some or all of the files in two directory trees. When both file name arguments to diff are directories, it compares each file that is contained in both directories, examining file names in alphabetical order as specified by the LC_COLLATE locale category.

What happens when you use diff to compare two files?

Use the diff command to compare text files. It can compare single files or the contents of directories. When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.

How do I compare two directories in UNIX?

Use the dircmp command to compare two directories specified by the Directory1 and Directory2 parameters and write information about their contents to standard output. First, the dircmp command compares the file names in each directory.


1 Answers

It prints a bunch of lines like

Only in dir1/blah: blah 

right? So just throw them away with grep.

LC_ALL=C diff ... | grep -v '^Only in' 

The LC_ALL=C is to make sure that the standard "Only in" message will be printed, not any translation.

like image 200
Alan Curry Avatar answered Sep 21 '22 13:09

Alan Curry