The search for cast paths must precede the expression in the script

I am trying to find alias find and grep for the line shown below

alias f='find . -name $1 -type f -exec grep -i $2 '{}' \;'

I intend to run it like

f *.php function

but when I add this to .bash_profile and run it, I click

[a@a ~]$ f ss s
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

How do i solve this?

+5
source share
2 answers

Aliases do not accept positional parameters. You will need to use the function.

f () { find . -name "$1" -type f -exec grep -i "$2" '{}' \; ; }

You also need to specify some of your arguments.

f '*.php' function

This rejects the glob extension so that it findexecutes it, not the shell.

+7
source

Extending Dennis Williamson's solution:

f() { find . -name "$1" -type f -print0 | xargs -0 grep -i "$2"; }

xargs, -exec grep... , .

+4

All Articles