1

I find myself writing this command a lot:

find . -iname "*foo*" | fgrep -i "foo"

To find all files and folders that have "foo" in their names, and to highlight the matching part in the results.

That's not very convenient. What would be a simpler solution? Do I need to write a custom command for this?

MasterScrat
  • 7,090
  • 14
  • 48
  • 80
  • check how does fprintf works with find. find . -type f \( -perm +100 -printf '\033[32m%p\033[0m\n' -or -print \) – Noproblem Oct 28 '15 at 11:02

2 Answers2

2

For convenience you can create a BASH function for this:

hlt() { find . -iname '*'"$1"'*' | grep --color "$1"; }

and call it as:

hlt foo
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Any why idea why even though your solution works, if I put `find . -iname '*'"$1"'*' | grep --color "$1";` in a bash script file then it doesn't work? – MasterScrat Oct 28 '15 at 12:31
  • It should work in a script also. Can you show the full script? – anubhava Oct 28 '15 at 14:08
1

if you're using bash take a look at Make a Bash alias that takes a parameter? for how to make "custom command" also note that grep without -i is case sensitive while -iname is not so you might be missing some mixed case hilights. On the side note fgrep should be faster than grep and probably less resource hungry :)

Community
  • 1
  • 1