M BUZZ CRAZE NEWS
// general

How to find same subfolders in two folders on first level only?

By David Jones

Scenario:

  • 2 main folders, that may have subfolders with the same names (I do not know what names might be duplicated, that's what I am trying to find)
  • the subfolders have MANY other files & subfolders so tools with automatic recursion are not really an option
  • I only care about duplicate subfolder names on the first level of the two main folders
  • contents of the subfolders don't matter
  • contents of the files don't matter

I tried using meld GUI but that just takes endless time to finish for these structures.

I tried using diff --brief --report-identical-files folder1 folder2 but that basically reports everything and it does not even include the folders so I can't even | grep identical.

Am I using wrong tools? Or is there some trick that I didn't get from diff --help ? Or am I doing something wrong?

Thanks

0

2 Answers

I'd use a simple find:

find "/path/to/main1" "/path/to/main2" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort | uniq -d

Or to make it zero-terminated to prevent issues with newline characters:

find "/path/to/main1" "/path/to/main2" -mindepth 1 -maxdepth 1 -type d -printf '%f\0' | sort -z | uniq -zd | xargs -0
2

Using zsh, given

% tree dir1 dir2
dir1
├── bar
└── foo └── baz
dir2
├── bar
│   └── baz
└── baz
6 directories, 0 files

then

% a=( dir1/*(/ND:t) ) ; b=( dir2/*(/ND:t) )

creates arrays of the tails (basenames) of directories / in the two top-level directories dir1 and dir2 (with Dotglob and Nullglob options enabled).

Then we can use an expansion of the form ${name:*arrayname} to retain only elements that are present in both arrays:

% print -rC1 ${a:*b}
bar

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy