Showing results for 
Search instead for 
Do you mean 


Frequent Advisor



Suppose I have a directory and inside it 2000 files are present. Now my question how to search any pattern or word in all files in one stroke through command line..

Acclaimed Contributor Acclaimed Contributor

Re: Search

Well you can use grep:
grep regular-expression *
Honored Contributor Honored Contributor

Re: Search


Few examples

#grep email /tmp/* -> will list the lines containing the word "email" from all files in /tmp dir

#grep -i email /tmp/* -> Case sensitive search

#grep -c email /tmp/* -> list the no of lines in each file containing the word "email"

#grep "email doc" /tmp/* -> To search multiple words

Best wishes,

Trusted Contributor Trusted Contributor

Re: Search

run the following from root

find . -name
Valued Contributor Valued Contributor

Re: Search

Command is:
find / -name * | grep
There are two types of people in the world, Marines and those who wish they were.
Honored Contributor Honored Contributor

Re: Search

grep works

the examples using find are trying to find the requested pattern in the filenames themselves. The original request was "within the files"
Acclaimed Contributor Acclaimed Contributor

Re: Search


As noted, a simple:

# cd /path; grep regexp *

...would work as long as the shell doesn't expand the "*" into a list of filenames that exceeds the maximum argument size.

It is also assumed that the directory contains only "text" (Ascii) files and not binary ones (e.g. executables) that will write garbage to your terminal.

If you want to examine a directory and its subordinate directories:

# find /path -type f -exec grep -i local /dev/null {} + useful. This looks at only files (not directories) and examines each file for a pattern "local". The addition of the '/dev/null' forces the output to include the filename along with the matching line regardless of how many different files are found with matches. The "+" terminator (instead of "\;") makes the operation quite performance conservative.


Honored Contributor Honored Contributor

Re: Search

And to build on the James solution:
# find /path -type f -maxdepth 1 -exec grep -i local /dev/null {} +

The maxdepth will only stay in the directory that you have specified and not the subdirectories. I really don't know if you need the /dev/null. But else the command on a linux or debian would be:

# find /path -type f -maxdepth 1 -exec grep -i local {} +
Honored Contributor Honored Contributor

Re: Search

Hi Rkumar,

I used to do

$cd /directory
$grep string * #(if u r string is abc it will look for abc (small letter) only)
$grep -i string * #(in this case it will search abc as well as ABC also)

Esteemed Contributor Esteemed Contributor

Re: Search

grep -i $(file *|grep text|cut -d":" -f1)

this will search for in all the text file sin teh directory