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

closed as unclear what you're asking by Toby Speight, user88036, Anthony Geoghegan, Jeff Schaller, GAD3R Oct 12 '18 at 18:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • That command does something totally different to what the subject line says. What are you actually trying to do? Please edit your question to clarify - I suggest you show a filesystem structure and the results you expect from it. – Toby Speight Oct 12 '18 at 8:14
  • @TobySpeight okay im not totally sure if that command works but i will edit it – Stefan xyz Oct 12 '18 at 8:17
  • 3
    If yo have found an adequate answer to your question that is not already posted as an answer, please post it as a proper answer (and accept it) rather than leaving the question unanswered. – Kusalananda Oct 12 '18 at 8:45
  • 2
    Note that counting the number of hard links would fail on filsystem that allows for creating additional hard links for directories or that implements the dot and dotdot directories in other ways than though hard links. – Kusalananda Oct 12 '18 at 8:57
  • 1
    If you found a solution, you should post it as a separate answer instead of making it part of the the question. – Anthony Geoghegan Oct 12 '18 at 9:55
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 {} +


  • i just want to have a directorys with exactly 2 subdirectorys not more than 2 and not less than 2 – Stefan xyz Oct 11 '18 at 13:05
  • @Stefanxyz That's exactly what this does. – Kusalananda Oct 11 '18 at 13:07
  • no i get all directorys which have at least 2 subdirectorys so i get directorys which have 2 subdirectorys but also some with 3 or more okay didnt look: at least the unedited version does it – Stefan xyz Oct 12 '18 at 7:59
  • @Stefanxyz You would only get directories with more than two subdirectories if you used -ge 2 or -gt 2 instead of -eq 2 in the test. – Kusalananda Oct 12 '18 at 8:09
  • 1
    @Stefanxyz And as I said, it would be nice to see an example where the above code fails in doing that. Note that my code allows there to be files in the directories besides the two subdirectories. Are you expecting the directories to be empty apart from the two subdirectories? – Kusalananda Oct 12 '18 at 8:30

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;'
  • This only works on filesystems that implement a UNIX filesystem design bug from the 1970s. – schily Oct 11 '18 at 14:51
  • @schily: mind to explain a bit? – RudiC Oct 11 '18 at 17:03
  • 1
    The fact that historic UNIX filesystems implemented '.' and '..' as hardlinks can be seen as a bug and a related behavior is not granted by POSIX. A directory could be shown by 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.