I would like to count number of subdirectories in depth 2, but ignore the parent directories of depth 1 in the bash script.
Example:
root_dir ---|--ignore_dir ---|--count_dir
| |--count_dir ---|--ignore_dir
| |--ignore_dir
|--ignore_dir ---|--count_dir
|--count_dir
|--count_dir
Any ideas?
You could try something like this :
find /path/to/root_dir -maxdepth 2 -mindepth 2 -type d | wc -l
Here we explicitely limit the depth of find
to 2 (no more, no less) and list all directories. wc -l
counts the number of lines from the output of the find
command.
Note:
If your folders names contain newlines, specific characters or unusual encoding, using wc -l
will yield incorrect results.
Taking inspiration from this question, you can instead print a character for each folder found, and count the resulting number of characters.
You could use the following snippet :
find /path/to/root_dir -maxdepth 2 -mindepth 2 -type d -printf '.' | wc -c