1

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

  • 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 08:14
  • @TobySpeight okay im not totally sure if that command works but i will edit it – Stefan xyz Oct 12 '18 at 08:17
  • The ideal "reproducible example" would be a script we can run in a temporary directory, that creates subdirs and files, then runs your proposed command. And the expected output of that command, of course. – Toby Speight Oct 12 '18 at 08:20
  • @TobySpeight hmm okay im going to do just give me a moment – Stefan xyz Oct 12 '18 at 08:27
  • 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 08: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 08: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 09:55
  • Do nested subdirectories "count"? mkdir -p ~/a/b/c -- does a have two subdirectories? – Jeff Schaller Oct 12 '18 at 10:39
  • @JeffSchaller no they dont count – Stefan xyz Oct 15 '18 at 09:16

2 Answers2

4
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:

Kusalananda
  • 333,661
  • 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 07: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 08:09
  • @kusalanada tested the secound one shows me also directorys with at least two subdirectorys but i just want to have all with exactly 2 subdirectory – Stefan xyz Oct 12 '18 at 08:09
  • @Stefanxyz You have an error in your code. The code that I wrote can't return directories that have anything other than exactly two subdirectories. – Kusalananda Oct 12 '18 at 08:11
  • my code is correct but i think its becouse im using fedora workstation or vmware has a problem but ty for trying – Stefan xyz Oct 12 '18 at 08:15
  • @Stefanxyz Update the question with the exact command you are trying and an example of where it gets it wrong. Running it in a VM should not matter, neither running it on Fedora, OpenBSD or Solaris or any other Unix. – Kusalananda Oct 12 '18 at 08:16
  • well im trying to find all directorys with the find command but i got no clue to do it since i worked myself through the manual but i didnt find anything which might work – Stefan xyz Oct 12 '18 at 08:25
  • and as said i want to see all directorys which have just 2 subdirectorys and thats it – Stefan xyz Oct 12 '18 at 08:26
  • 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 08:30
0

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;'
RudiC
  • 8,969
  • 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