Search (3 Views)
Reply
Frequent Advisor
Waugh
Posts: 131
Registered: ‎09-13-2008
Message 1 of 10 (3 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,762
Registered: ‎03-06-2006
Message 2 of 10 (3 Views)

Re: Search

Well you can use grep:
grep regular-expression *
Please use plain text.
Honored Contributor
Ganesan R
Posts: 1,891
Registered: ‎05-23-2006
Message 3 of 10 (3 Views)

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.
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 4 of 10 (3 Views)

Re: Search


run the following from root

find . -name
Please use plain text.
Valued Contributor
Adam W.
Posts: 279
Registered: ‎08-06-2008
Message 5 of 10 (3 Views)

Re: Search

Command is:
find / -name * | grep
There are two types of people in the world, Marines and those who wish they were.
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 6 of 10 (3 Views)

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"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 10 (3 Views)

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...
Please use plain text.
Honored Contributor
Jannik
Posts: 490
Registered: ‎01-19-2003
Message 8 of 10 (3 Views)

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 {} +
Please use plain text.
Honored Contributor
Suraj K Sankari
Posts: 1,659
Registered: ‎09-09-2008
Message 9 of 10 (3 Views)

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
Please use plain text.
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 10 of 10 (3 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation