1748267 Members
3770 Online
108760 Solutions
New Discussion юеВ

Search

 
Waugh
Frequent Advisor

Search

Hi,

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..

Reagrds
Rkumar
9 REPLIES 9
Dennis Handly
Acclaimed Contributor

Re: Search

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

Re: Search

Hi,

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,

Ganesh.
Grayh
Trusted Contributor

Re: Search


run the following from root

find . -name
Adam W.
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.
OldSchool
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"
James R. Ferguson
Acclaimed Contributor

Re: Search

Hi:

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 {} +

...is 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.

Regards!

...JRF...
Jannik
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 {} +
jaton
Suraj K Sankari
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)
or
$grep -i string * #(in this case it will search abc as well as ABC also)

Suraj
Arturo Galbiati
Esteemed Contributor

Re: Search

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

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

HTH,
Art