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?

2 Answers 2

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 
0
-1

This will work:

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.