3

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

My structure always looks like this:

/some/path/app1/migrations/0001_hello_world.py  
/some/path/app2/migrations/0001_foo.py  
/some/path/app2/migrations/0002_bar.py

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?

Ron
  • 173
  • 1
  • 5

2 Answers2

3

Try,

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.

Output:

  2 app1
  1 app2
pLumo
  • 22,565
-1

This will work:

$ for folder in *; do echo "$folder: "; \
    find ./$folder -type f | wc -l; done | grep -A 1 "some/path/"`
slm
  • 369,824
Dr-Bracket
  • 377
  • 1
  • 5
  • 21
  • how would i combine it with my git diff? – Ron Jun 13 '19 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 '19 at 14:38
  • and what would "dir" be? sorry, not so familiar with shell scripting :( – Ron Jun 13 '19 at 14:41