Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I think that you cannot pass arguments to an alias. Try converting fg to a function instead. – ierax Mar 18 '11 at 9:39
I have an alias --> alias ff='find . | xargs grep $1 -sl' which works fine! – Narek Mar 18 '11 at 9:45
fg is a shell builtin, I recommend you chose a different name whether you implement this as an alias or (better still) a function. – Johnsyweb Mar 18 '11 at 9:53
@Narak, I think your second example there is only working by accident. – mattdm Mar 18 '11 at 12:11

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 "
share|improve this answer
This assumes GNU grep, of course. – Johnsyweb Mar 18 '11 at 10:07

find ./ -name "$1" -exec grep -l "$2" {} \; should do the trick.

share|improve this answer
$i should be $1, and $1 and $2 should be quoted to allow blanks and such in filenames and search-word. – user unknown Mar 26 '11 at 12:07
Whoops, my bad. Fixed. – Shadur Mar 26 '11 at 16:13
up vote 0 down vote accepted

This works!

function fndg()
   find . -name "$1" | xargs grep -rn --color "$2"
share|improve this answer
find can call grep without xargs invocation, see -execdir, but in this case, the called program grep can iterate itself. – user unknown Mar 26 '11 at 12:06
xargs will be more efficient, as it will call grep with several filenames at once, instead of one at a time using -exec – glenn jackman Mar 26 '11 at 23:25
gnu find can handle mutliple filenames at once too: find . -name "$1" -exec grep --color $2 -- {} + if ended with + instead of ";" – user unknown Mar 27 '11 at 19:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.