Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering file from the result of find command

Tags:

unix

I want to look for a particular pattern in a directory of files and want to exclude certain patterns while the result is displayed.

I use the following command

find . -type f -exec grep -il 'foo' {} \; | find . -not -name "*.jar" -not -name "*.gz" -not -name "*.log" 2>/dev/null

when the result is displayed, I see the following error message

find: grep terminated by signal 13

Can someone please guide me as to why this error message is there and if there is a better command to use for getting the desired result (basically excluding jar files or log files or some other type of files from the result set)?

like image 245
user1588737 Avatar asked Dec 20 '22 01:12

user1588737


2 Answers

I think you're trying to use find as grep in the second half of that pipeline; find doesn't work like that. What you should do is have it all in one command:

find . -type f -not -name "*.jar" -not -name "*.gz" -not -name "*.log" -exec grep -il 'foo' {} + 2>/dev/null

Or

find . -type f -not -name "*.jar" -not -name "*.gz" -not -name "*.log" -exec grep -qi 'foo' {} \; -print 2>/dev/null

Or let grep traverse the tree itself and filter the names after (using grep, not find):

grep -irl foo . | grep -v -e '\.jar$' -e '\.gz$' -e '\.log$'
like image 196
Kevin Avatar answered Jan 09 '23 13:01

Kevin


find . | grep -v "jar$" | grep -v "log$" | grep -v "gz$"
like image 31
Adam Burry Avatar answered Jan 09 '23 15:01

Adam Burry