Home > Blockchain >  Linux "find" command and highlight filename
Linux "find" command and highlight filename

Time:12-24

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" {} \;

CodePudding user response:

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.

CodePudding user response:

To find those files which have the word foo in the content and in the file name, you could do a

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

The result of the -exec will print the names of all files which have foo in them, and the subsequent grep chooses from this files with foo in name.

  • Related