The command
find ~ -maxdepth 2 -mindepth 2
doesnt work is there any other solution?
Hm okay i got the solution:
find ~ -maxdepth 1 -links 4 -type d
Ty for those who tried to solve it
The command
find ~ -maxdepth 2 -mindepth 2
doesnt work is there any other solution?
Hm okay i got the solution:
find ~ -maxdepth 1 -links 4 -type d
Ty for those who tried to solve it
find . -type d -exec sh -c '
for pathname do
set -- "$pathname"/*/
[ "$#" -eq 2 ] && printf "%s\n" "$pathname"
done' sh {} +
The above command will print the pathnames of all directories under the current directory that contains exactly two subdirectories.
The in-line sh -c
script gets pathnames of found directories from find
in batches, and will iterate over each batch, one directory at a time.
For each directory, $pathname
, the shell glob "$pathname"/*/
is expanded. This pattern would expand to all the pathnames of all subdirectories directly under $pathname
(or would remain unexpanded if there were no subdirectories). The parameter $#
will contain the number of items that the pattern expanded to, and if this is two, the path to the directory is printed.
The above would not count hidden directories. For that, use bash
with its dotglob
shell option activated:
find . -type d -exec bash -O dotglob -c '
for pathname do
set -- "$pathname"/*/
[ "$#" -eq 2 ] && printf "%s\n" "$pathname"
done' bash {} +
Related:
-ge 2
or -gt 2
instead of -eq 2
in the test.
– Kusalananda
Oct 12 '18 at 08:09
stat
available? Making use of the No. of hard links being 2 (parent dir link plus ..
link) plus the sub dir count, try
stat -c"%n %F %h" * | sed -n '/directory 4/ s///p;'
ls
as a zero sized file with link count 1 in the standard case. There are in fact filesystems where your assumption is wrong.
– schily
Oct 11 '18 at 17:07
mkdir -p ~/a/b/c
-- doesa
have two subdirectories? – Jeff Schaller Oct 12 '18 at 10:39