Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

given two directory trees how to find which files are the same?

Tags:

linux

bash

diff

I am writing a bash script, and I would like to know which files are the same in two directory trees.

It would be the opposite of using diff.

Well i found the answer myself. I had tried it before, but I thought it did not work.

diff -srq dir1/ dir2/ | grep identical 
like image 745
fazineroso Avatar asked Apr 10 '12 11:04

fazineroso


People also ask

How do I compare files in two folders?

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.

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.

Can diff compare directories?

Normally diff reports subdirectories common to both directories without comparing subdirectories' files, but if you use the -r or --recursive option, it compares every corresponding pair of files in the directory trees, as many levels deep as they go.


1 Answers

Well i found the answer myself. I had tried it before, but I thought it did not work.

diff -srq dir1/ dir2/ | grep identical 

What -srq means? From diff --help :

-s  --report-identical-files  Report when two files are the same. -r  --recursive  Recursively compare any subdirectories found. -q  --brief  Output only whether files differ. 
like image 174
fazineroso Avatar answered Sep 30 '22 15:09

fazineroso