I have a directory structure as follows:


Each sub_directory may or may not have a file called xyz.json. I want to find the total count of xyz.json files in the directory dir.

How can I do this?


You can use :

find path_to_dir -name xyz.json | wc -l
| improve this answer | |
  • This works great for the example. Here is a modified version of the same solution that you'll find performs better when searching many directories with many files. I further restrict find to only files, make it print a '.' for each one, then have wc count the dots, rather than lines. It's a weird trick but it performs slightly better: find path_to_dir -type f -name xyz.json -printf '.' | wc -c – Jon Red Feb 12 at 19:27

An alternative using shell globing

## Make ** match all files and 0 or more dirs and subdirs
$ shopt -s globstar

$ ls -dq **/xyz.json | wc -l
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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