1

I'm trying to get a number of directories that start with a specific letter. I tried to get this done with:

find /home/test1 -type d | grep -m 1 a* | wc -l

but I'm getting 0 in the result no matter what I do.

2
  • 1
    find has a -name parameter, see man find
    – Panki
    Jan 12, 2021 at 14:26
  • 2
    The -name is a better method. But for completeness: [1] grep patterns tend to contain chars that are special to shell -- it needs quoting. [2] The pattern needs to specify first character, and to use a grep pattern, not a shell one: '^a'. [3] grep is only reading one file (stdin pipe) so -m 1 will exit on the first line, so the count can only be 0 or 1. Jan 12, 2021 at 14:57

1 Answer 1

2
LC_ALL=C find /home/test1//. -name 'a*' -type d |
  LC_ALL=C grep -c //

Or with GNU find:

LC_ALL=C find /home/test1/ -name 'a*' -type d -printf . | wc -c

Remember file paths can contain newline characters, so counting lines is not an option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .