I have the following problem: I want to count migration files per directory.

My structure always looks like this:


What I need is a shell script, in the best case a one-liner to get the following result:

app1: 1
app2: 2

So I get the numbers of files per app directory.

I got as far as getting my list of files:

git diff --color --name-only --diff-filter=A origin/develop my-branch | grep "\/migrations\/[0-9]"

I can count all of those matches with wc -l but I cannot count per directory.

Unfortunately even with google and stack, I'm stuck.

Any ideas on that topic?

2 Answers 2



git diff --color --name-only --diff-filter=A origin/develop my-branch \
    | grep '/migrations/[0-9]' \
    | cut -d/ -f4 \
    | sort \
    | uniq -c

adapt the field number of cut (here: 4) as necessary.


  2 app1
  1 app2

This will work:

$ for folder in *; do echo "$folder: "; \
    find ./$folder -type f | wc -l; done | grep -A 1 "some/path/"`
  • how would i combine it with my git diff?
    – Ron
    Jun 13, 2019 at 14:35
  • Could you try piping it before the find? Like this: for folder in *; do echo "$folder: "; git diff --color --name-only --diff-filter=A origin/develop my-branch | find ./$folder -type f | wc -l; done | grep -A 1 "dir"
    – Dr-Bracket
    Jun 13, 2019 at 14:38
  • and what would "dir" be? sorry, not so familiar with shell scripting :(
    – Ron
    Jun 13, 2019 at 14:41

You must log in to answer this question.

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