You are not logged in.
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
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
sameLast edited by schard (2022-06-20 18:18:45)
Inofficial first vice president of the Rust Evangelism Strike Force
Offline
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... 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
Use comm:
Didn't know it existed. Faaantastic. Switches -1 -2 -3 do the trick in a straightforward way. Thanks schard !
Offline
Maybe 'git diff'. There should be options and filters to tweak the output. Something like this:
git diff --no-index --binary folder1 folder2 > output.txtsys2064
Offline
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.
Diff? It sounds like you want `comm` not `diff`.
Because I just learned of comm
!
Offline
... 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
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
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
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 ![]()
Offline