How to find same subfolders in two folders on first level only?
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
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