You are not logged in.

#1 2022-06-20 17:24:11

dawnofman
Member
Registered: 2019-07-26
Posts: 140

[solved] finding differences among two directories: best approach ?

Given two directories; eg: /whatever/one/ and /whateverelse/two/ ... what basically I am asking for is the three distinctive recursive file lists for added, removed, and matching ones among /one/ or /two/ --the last components on both paths. By matching I mean same file name object among the two directories which may not necessarily means same content, because having the matching file list will allow to run cmp or diff thereafter to check for differences among them.

Use case ? ... to be used within a BASH script to inform the user of any changes among both directories; eg: manual intervention required.

My first approach was to use find to catalog everything within each directory to memory variables, cut to discard anything upward one/two on the file path to allow sorting them, and then diff --side-by-side --suppress-common-lines to show added/removed files/directories. I already done all the coding and, albeit not performance-wise nor lean-code, it is working as expected sans one minor detail:

I cannot manage to find a way to get a list of matching file names among the two directories; diff seems not to have the complement action; ie: show me what's not changed only ... at least from what I can gather from diff's man page.

Yesterday, and after coding this approach, I learned that diff can produce differences for two directories without previously using find; eg: diff -qr '/whatever/one/' '/whateverelse/two/'; showing results in the form "Only in ..." but once again, not knowing how to show what didn't match.

Like everything else in linux's user land there are sure to be multiple ways to achieve the same.

What do you suggest is the best approach ?

Last edited by dawnofman (2022-06-21 01:49:37)

Offline

#2 2022-06-20 18:14:58

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,658
Website

Re: [solved] finding differences among two directories: best approach ?

Use comm:

$ tree a b
a
├── b
├── other
└── same
b
├── a
├── one
└── same

1 directory, 2 files
$ comm <(find a/* | cut -d / -f 2- | sort) <(find b/* | cut -d / -f 2- | sort)
	a
b
	one
other
		same

Last edited by schard (2022-06-20 18:18:45)


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3 2022-06-20 18:20:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: [solved] finding differences among two directories: best approach ?

dawnofman wrote:

My first approach was to use find to catalog everything within each directory to memory variables, cut to discard anything upward one/two on the file path...

Why use cut?  Just use find to get this result, e.g.,:

find /wherever/one -printf '%P\n' | sort
dawnofman wrote:

... and then diff...

Diff?  It sounds like you want `comm` not `diff`.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2022-06-20 18:48:07

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] finding differences among two directories: best approach ?

schard wrote:

Use comm:

Didn't know it existed. Faaantastic. Switches -1 -2 -3 do the trick in a straightforward way. Thanks schard !

Offline

#5 2022-06-20 18:48:23

Maniaxx
Member
Registered: 2014-05-14
Posts: 761

Re: [solved] finding differences among two directories: best approach ?

Maybe 'git diff'. There should be options and filters to tweak the output. Something like this:

git diff --no-index --binary folder1 folder2 > output.txt

sys2064

Offline

#6 2022-06-20 18:56:32

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] finding differences among two directories: best approach ?

Trilby wrote:

Why use cut?  Just use find to get this result, e.g.,:

find /wherever/one -printf '%P\n' | sort

Because find, at least as stated on the man page, does not have a flag to print the path found below the starting point only; ie: without the starting point ... it has %H %h %P %p %f

Provided:

find /wherever/one -mindepth 1 -printf "%?\n"

I only need; eg:

one/fileone
one/filetwo
one/filethree
one/three/filefour
one/three/filefive
one/three/filesix

Because the directories I am comparing are for live and backup ones having totally different root paths and every combination I tried as stated in the man page either gets me the last component only or the full path only, but not the path below my starting point.

Trilby wrote:

Diff?  It sounds like you want `comm` not `diff`.

Because I just learned of comm smile !

Offline

#7 2022-06-20 19:46:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: [solved] finding differences among two directories: best approach ?

dawnofman wrote:

... and every combination I tried as stated in the man page either gets me the last component only or the full path only, but not the path below my starting point.

That's exactly what %P is for which is why I put it in my example.  In what way is that not what you need?


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2022-06-20 19:51:36

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] finding differences among two directories: best approach ?

Trilby wrote:
dawnofman wrote:

... and every combination I tried as stated in the man page either gets me the last component only or the full path only, but not the path below my starting point.

That's exactly what %P is for which is why I put it in my example.  In what way is that not what you need?

Because %P does what I want if when running find I am currently located on the starting point (cd/pwd) but not when I am; eg: in ~ and I issue find /whatever/whateverelse/ ... and wants everything coming below (and not including /whatever/whateverelse/.

Offline

#9 2022-06-20 20:03:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,480
Website

Re: [solved] finding differences among two directories: best approach ?

Are you using gnu find from [core]/findutils?  Gnu find with the command line I provided will list everything below the starting-point without including the starting-point (e.g., the output will not include /whatever/whateverelse/) regardless of what the cwd is.

Last edited by Trilby (2022-06-20 20:04:53)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#10 2022-06-20 20:20:00

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] finding differences among two directories: best approach ?

Trilby wrote:

Are you using gnu find from [core]/findutils?  Gnu find with the command line I provided will list everything below the starting-point without including the starting-point (e.g., the output will not include /whatever/whateverelse/) regardless of what the cwd is.

You are right. I suppose I tested so many things that ... sorry Trilby !

Sometimes happens sad

Offline

Board footer

Powered by FluxBB