Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash -- find a list of files with more than 3 lines

Tags:

linux

bash

shell

I have a directory of files and I want to find a list of files who has more than 2 lines.

I know I can use wc -l to test each file, but how do I wrap it up in bash?

Sorry for the newbie question, new to bash.

like image 299
CuriousMind Avatar asked Oct 27 '15 14:10

CuriousMind


2 Answers

You can use this find command:

find . -type f -exec bash -c '[[ $(wc -l < "$1") -gt 2 ]] && echo "$1"' _ '{}' \;
like image 105
anubhava Avatar answered Oct 31 '22 17:10

anubhava


Using xargs and awk:

$ find . -type f | xargs wc -l | awk '$1 > 2'

If you are in a git repository and want to count only tracked files, you can use following command:

$ git ls-files | xargs wc -l | awk '$1 > 2'
like image 27
Hotschke Avatar answered Oct 31 '22 16:10

Hotschke