Lucy Bain
Lucy Bain

Reputation: 2616

How can I find files with more than one line in them with a given name?

Question: How can I get all the files with name "interesting-file.txt" that have more than one line in them?

Background research:

But I'm having problems putting it all together.

I've tried a few variations of

find . -name "interesting-file.txt" | awk '{print wc -l $0}'

with no success. I haven't started on the "greater than one" other than to look for the related command.

I'm on OSX, using the standard terminal.

Thanks for any help!

Upvotes: 6

Views: 4093

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74645

I would go for something like this:

find . -name "interesting-file.txt" -exec awk 'END { if (NR > 1) print FILENAME }' {} \;

All the files that are found are passed to awk, which prints the name if the number of lines NR is greater than 1.

With GNU awk you can take advantage of the ENDFILE block and use {} + to minimise the number of invocations of awk:

find . -name "interesting-file.txt" -exec awk 'ENDFILE { if (FNR > 1) print FILENAME }' {} +

Upvotes: 13

Related Questions