Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

diff directories a and b. show only files in b, not in a

Tags:

unix

diff

the title sumarises my question. given directories a and b, i want to be able to generate a list of files that are in b but not in a.

a normal diff does this, but it also shows files in a not in b:

$ diff -u /mnt/Media/a ~/b    
Only in /mnt/Media/a: abab
Only in /home/conor/b: blah

i would also like diff to list filenames only - none of the "Only in .." stuff

thanks

like image 700
conor Avatar asked Mar 27 '13 19:03

conor


3 Answers

Try this

pick-up one of these :

$ LANG=C diff -qr a b | awk -F"Only in b: " '/^Only in b:/{print $2}'

or

$ LANG=C diff -qr a b | grep -oP "^Only in b: \K.*"

or

$ LANG=C diff -qr a b | grep '^Only in b:' | cut -d: -f2-

Note

LANG=C

is only there to avoid displaying in any locale language but in English.

Doc

See man diff

like image 90
Gilles Quenot Avatar answered Dec 18 '22 11:12

Gilles Quenot


The uniq command is more useful than you might imagine. Consider two directories dirA and dirB:

% ls -R dirA dirB
dirA:
s1/ s2/

dirA/s1:
f2

dirA/s2:
f1  f2

dirB:
s1/ s2/

dirB/s1:
f1  f2

dirB/s2:
f1
%

File s1/f1 is missing from dirA, and file s2/f2 is missing from dirB.

Create lists of the contents of the two directories:

% (cd dirA; find . -type f >../listA)
% (cd dirB; find . -type f >../listB)

Now find the lines which are present only in listB:

% cat listA listA listB | sort | uniq -u 
./s1/f1
% 

Ta-dah!

like image 23
Norman Gray Avatar answered Dec 18 '22 12:12

Norman Gray


Typically when I have to do this, I go low-tech:

cd ~/a
find . -type f | sort > ~/fooa
cd ~/b
find . -type f | sort > ~/foob
vimdiff ~/fooa ~/foob

It lets me refine the results. "Oh, whoops, I wanted to exclude .svn directories from ~/a", so rerun the ~/fooa file without .svn directories and then rediff.

like image 44
Andy Lester Avatar answered Dec 18 '22 11:12

Andy Lester