Skip to content
Advertisement

Linux “find” command and highlight filename [closed]

I am using the Linux find command. It looks through all the *.jar files and their contents and finds the word “foo” in them but sometimes the filename might also contain the word “foo”. (ie. foo.jar, foo-123.jar, myfoo.jar, etc) If so, I would like to highlight the word “foo” in the filename.

I have 2 commands that works great on their own but is there a way I can combine them to one statement?

find . -type f -name *.jar | grep -E --color "foo" 
find . -type f -name *.jar -exec grep -li "foo" {} ;

Advertisement

Answer

Use the option -fprint of find to write to a process substitution.

find . -type f -name *.jar -fprint >(grep --color foo) -exec grep -li foo {} ;

Note: Quote arguments with globbing characters like *. Using the option -E is useless, if you search just for a simple string.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement