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'm trying to produce this behaviour:

grep 192.168.1 *.txt

By passing a string into grep via Xargs but it is going on the end instead of as the first parameter.

echo 192.168.1 | xargs grep  *.txt

I need to tell xargs (or something similar) to put the incoming string between 'grep' and '*' instead of on the end.

How do I do this?

share|improve this question
up vote 12 down vote accepted
$ echo 192.168.1. | xargs -I{} grep {} *.txt


Sample files:

$ cat {1..3}.txt

Example run:

$ echo 192.168.1. | xargs -I{} grep {} *.txt
share|improve this answer

Another approach:

find . -name \*.txt -print0 | xargs -0 grep 192.168.1

This will not overflow the shell's command line length with too many file names. To avoid confusing xargs/grep with file names that have spaces, -print0 and -0 options will delineate each found name with a null rather than a LF.

share|improve this answer

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.