Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare files with same names in two different directories using a shell script

Tags:

bash

shell

diff

Before moving on to use SVN, I used to manage my project by simply keeping a /develop/ directory and editing and testing files there, then moving them to the /main/ directory. When I decided to move to SVN, I needed to be sure that the directories were indeed in sync.

So, what is a good way to write a shell script [ bash ] to recursively compare files with the same name in two different directories?

Note: The directory names used above are for sample only. I do not recommend storing your code in the top level :).

like image 892
Animesh Avatar asked Sep 23 '08 08:09

Animesh


People also ask

How do I compare two files between directories in Linux?

Click on directory comparison and move to the next interface. Select the directories you want to compare, note that you can add a third directory by checking the option “3-way Comparison”. Once you selected the directories, click on “Compare”.

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.


2 Answers

The diff command has a -r option to recursively compare directories:

diff -r /develop /main
like image 169
Greg Hewgill Avatar answered Sep 18 '22 09:09

Greg Hewgill


diff -rqu /develop /main

It will only give you a summary of changes that way :)

If you want to see only new/missing files

diff -rqu /develop /main | grep "^Only

If you want to get them bare:

diff -rqu /develop /main | sed -rn "/^Only/s/^Only in (.+?): /\1/p"
like image 37
Kent Fredric Avatar answered Sep 20 '22 09:09

Kent Fredric