recursive search for a file from the current directory

recursive search for a file from the current directory

am 25.11.2007 07:59:59 von Rahul

Hi Everyone,

I just wanted to search for a particular file in the current
directory and its sub-directories. I came to know that grep and find
together needs to be used, but i'm not able to figure out the exact
command.

Thanks in advance!!!

Re: recursive search for a file from the current directory

am 25.11.2007 08:55:45 von Janis Papanagnou

Rahul wrote:
> Hi Everyone,
>
> I just wanted to search for a particular file in the current
> directory and its sub-directories. I came to know that grep and find
> together needs to be used, but i'm not able to figure out the exact
> command.

If you know the filename (or parts thereof) use just find(1) as in

find . -type f -name '*.txt'


Janis

>
> Thanks in advance!!!

Re: recursive search for a file from the current directory

am 25.11.2007 15:07:54 von Guanqun.Lu

On Nov 25, 2:59 pm, Rahul wrote:
> Hi Everyone,
>
> I just wanted to search for a particular file in the current
> directory and its sub-directories. I came to know that grep and find

`grep' searches the content of the files,
while `find' searches the name, type or time of the files.

They're totally different approaches.
Say, if you want to find the files with the string `helloworld' in it,
then use `grep -R helloworld *'
but if you want to find the files whose file names begins with hello,
then use `find . -name "hello*"

> together needs to be used, but i'm not able to figure out the exact
> command.
>
> Thanks in advance!!!

Re: recursive search for a file from the current directory

am 25.11.2007 17:51:43 von Janis Papanagnou

Guanqun.Lu@gmail.com wrote:
> On Nov 25, 2:59 pm, Rahul wrote:
>
>>Hi Everyone,
>>
>> I just wanted to search for a particular file in the current
>>directory and its sub-directories. I came to know that grep and find
>
>
> `grep' searches the content of the files,

Not quite. It searches any stream of data, also the output of find
if one asks for that. That application makes sense in cases where
you can simply define egrep regexps but the shell patterns used by
find may not suffice or are bulky to write.

> while `find' searches the name, type or time of the files.
>
> They're totally different approaches.

I'd rather say; they support each other.

> Say, if you want to find the files with the string `helloworld' in it,
> then use `grep -R helloworld *'
> but if you want to find the files whose file names begins with hello,
> then use `find . -name "hello*"

And if you want to find some specific strings in a subset of files
you would combine the two tools (which is what the OP suspected).

Janis

>
>>together needs to be used, but i'm not able to figure out the exact
>>command.
>>
>>Thanks in advance!!!
>
>

Re: recursive search for a file from the current directory

am 26.11.2007 13:10:29 von Othmar Wigger

On 25 Nov., 17:51, Janis Papanagnou
wrote:
> And if you want to find some specific strings in a subset of files
> you would combine the two tools (which is what the OP suspected).

Right. So let us just tell him. The syntax of the find command is
tricky indeed.

find -type f -exec grep -l {} \;

The -type f option to find makes sure only ordinary files are grepped,
no directories, pipes nor special nodes. The -l option tells grep not
to display matching lines (the file could be a binary one) but only to
list the name of the files that contain the pattern.

Make sure to enter the command exactly as given above, including
spaces and the backslash. The ending semicolon is crucial, and it must
be quoted to prevent its interpretation by the shell.