I have defined the following alias in ~/.bashrc
:
alias fg='find . -name $1 | xargs grep --color $2'
in order to write
fg "*.txt" " my_text "
and find all file that have extension .txt and contain " my_text " but it does not work. Why?
Aliases in bash do not take parameters (as already pointed out), so when you need something like that you can use bash functions (like the one provided by @l0b0).
But what you are trying to achieve here, can be done in a better way by using only grep.
grep -r --color --include="*.txt" " my_text " ./
BTW, fg
is a shell built in command, an important one. You should avoid using it as a name for aliases or functions.
EDIT: in a function
$ ffg() { rgrep --color --include="$1" "$2" ./; }
$ ffg "*.txt" " my_text "
find ./ -name "$1" -exec grep -l "$2" {} \;
should do the trick.
This works!
function fndg()
{
find . -name "$1" | xargs grep -rn --color "$2"
}
find
can call grep without xargs invocation, see -execdir
, but in this case, the called program grep
can iterate itself.
Commented
Mar 26, 2011 at 12:06
gnu find
can handle mutliple filenames at once too: find . -name "$1" -exec grep --color $2 -- {} +
if ended with + instead of ";"
Commented
Mar 27, 2011 at 19:59
fg
to a function instead.fg is a shell builtin
, I recommend you chose a different name whether you implement this as an alias or (better still) a function.