# Grep Problem

**URL:** <https://community.unix.com/t/grep-problem/154206>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [June 22, 2005, 4:57am UTC](https://community.unix.com/t/grep-problem/154206 "2005-06-22T04:57:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![avadhani](https://community.unix.com/letter_avatar/avadhani/32/5_5575768a8748004e209b776fc1b2916d.png) [@avadhani](https://community.unix.com/u/avadhani)\
**Post date:** [June 22, 2005, 4:57am UTC](https://community.unix.com/t/grep-problem/154206/1 "2005-06-22T04:57:50Z")

</div>

Hi,  
I want search all files containing some "searchtext" in current dir and sub dir. For this, I used

grep "searchtext" \*/\*

This will search only in sub dir files and does not give results if file containing search text exists in current directory.

and

grep "searchtext" \*

This will search only files current directory, not sub directories.

But when I use , following command . I get result.

find . | xargs grep "searchtext"  
It searches at both current dir and sub dir .

How to achive this using grep command .

thanks

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [June 22, 2005, 5:21am UTC](https://community.unix.com/t/grep-problem/154206/2 "2005-06-22T05:21:11Z")

</div>

> [@avadhani](#):
>
> But when I use , following command . I get result.
> 
> find . | xargs grep "searchtext"  
> It searches at both current dir and sub dir .
> 
> How to achive this using grep command .

Arent you using grep in your command ?

Or do you just want to use grep without the find ?

vino

---

<div class="post-metadata">

**Author:** ![avadhani](https://community.unix.com/letter_avatar/avadhani/32/5_5575768a8748004e209b776fc1b2916d.png) [@avadhani](https://community.unix.com/u/avadhani)\
**Post date:** [June 22, 2005, 5:28am UTC](https://community.unix.com/t/grep-problem/154206/3 "2005-06-22T05:28:14Z")

</div>

Hi,

I want to use only grep command to search keyword in all files (current dir and sub dir)

Thanks

---

<div class="post-metadata">

**Author:** ![Simerian](https://community.unix.com/letter_avatar/simerian/32/5_5575768a8748004e209b776fc1b2916d.png) [@Simerian](https://community.unix.com/u/Simerian)\
**Post date:** [June 22, 2005, 7:39am UTC](https://community.unix.com/t/grep-problem/154206/4 "2005-06-22T07:39:05Z")

</div>

> [@avadhani](#):
>
> Hi,
> 
> I want to use only grep command to search keyword in all files (current dir and sub dir)
> 
> Thanks

find has a command option that allows you to run, for instance, a grep against each file it locates. This is sometimes very useful... You'll have to check the man page for the syntax.

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [June 22, 2005, 8:26am UTC](https://community.unix.com/t/grep-problem/154206/5 "2005-06-22T08:26:06Z")

</div>

> [@simerian](#):
>
> find has a command option that allows you to run, for instance, a grep against each file it locates. This is sometimes very useful... You'll have to check the man page for the syntax.

Here is your answer

```plaintext
find . -name 'haystack' -exec grep 'needle' {} \; -print

```

The -print will display the corresponding haystack for each successful needle found.

Vino

---

<div class="post-metadata">

**Author:** ![Just\_Ice](https://community.unix.com/user_avatar/community.unix.com/just_ice/32/165_2.png) [@Just\_Ice](https://community.unix.com/u/Just_Ice)\
**Post date:** [June 22, 2005, 8:37am UTC](https://community.unix.com/t/grep-problem/154206/6 "2005-06-22T08:37:35Z")

</div>

> [@avadhani](#):
>
> Hi,
> 
> I want to use only grep command to search keyword in all files (current dir and sub dir)
> 
> Thanks

... i find it quiet interesting that when given the whole gamut of Unix tools that are available to do things with, there are people who insist on only using 1 tool and 1 tool only even when there are other tools that might be able to achive the required results quicker --- unless this is homework ....

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [June 22, 2005, 9:13am UTC](https://community.unix.com/t/grep-problem/154206/7 "2005-06-22T09:13:57Z")

</div>

> [@just ice](#):
>
> even when there are other tools that might be able to achive the required results quicker ---

Just Ice,

for learning point of view (atleast for me), how would you approach the OP to suit your statement.

Thanks,  
vino

---

<div class="post-metadata">

**Author:** ![Just\_Ice](https://community.unix.com/user_avatar/community.unix.com/just_ice/32/165_2.png) [@Just\_Ice](https://community.unix.com/u/Just_Ice)\
**Post date:** [June 22, 2005, 11:16am UTC](https://community.unix.com/t/grep-problem/154206/8 "2005-06-22T11:16:54Z")

</div>

perl or find-awk or awk comes to mind ... of course, i'm not that good with perl but the few examples i've seen on this forum have been very fast in what they were designed to do ...

however, the point i was making is that people should not restrict their choices to certain tools only --- people who do will inevitably find their options restricted by their inflexibility ... obviously this is not the case for homework questions as those were meant to have restrictions ...

---

<div class="post-metadata">

**Author:** ![SemiOfCol](https://community.unix.com/letter_avatar/semiofcol/32/5_5575768a8748004e209b776fc1b2916d.png) [@SemiOfCol](https://community.unix.com/u/SemiOfCol)\
**Post date:** [June 23, 2005, 2:58pm UTC](https://community.unix.com/t/grep-problem/154206/9 "2005-06-23T14:58:44Z")

</div>

You can use grep --version to tell which version you are on.

the GNU grep I have (2.5a and 2.5.1) can use -r for a recursive search.

grep -r "search text" \* will search the current directory and all sub-directories underneath.

You can always man grep and look for either -r, -R or -d recurse options. Not all versions of grep will use these.

;
