Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 14th, 2003, 07:35 AM
Jose Del Valle Jose Del Valle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 12 Jose Del Valle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Command to find a string in files

does somebody knows a command to find a string in all the files under a directory (and subdirectories)?

Perhaps a pipe ? With grep mystring filename
and find . -name "*.conf"

I tried it, but it is not working

Thanks for any help
JdV

Reply With Quote
  #2  
Old May 14th, 2003, 12:54 PM
GNUbie's Avatar
GNUbie GNUbie is offline
Throws Rocks
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Cincinnati, Ohio
Posts: 392 GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 h 31 m 7 sec
Reputation Power: 8
I can't seem to think in shell, but the following Perl lines should work:

Code:
@files = `ls`;
foreach $file (@files) {
    system "grep $string $file";
}
__________________
Two things have come out of Berkeley, Unix and LSD.
It is uncertain which caused the other.

Reply With Quote
  #3  
Old May 14th, 2003, 01:08 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
grep knows the parameter "-r" which is "recursive". Another interesting parameter for searching strings is "-i" which is "case insensitive".
i.e.
Code:
# grep -ir "ftp" /var/log


searches for the word "ftp" in all logfiles, and reports "ftp", "FTP", "Ftp", ...
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #4  
Old May 14th, 2003, 01:50 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,836 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 30 m 33 sec
Reputation Power: 446
Jose:

There seems to be a little confusion over whether you are looking for a command-line command or a way to do the grep within a program.

M.Hirsch's "grep -r" is the way from the command line.

From within a program, you could use a pipe:
Code:
FILE *fp;
char command[] = "grep -ir \"ftp\" /var/log";
char buffer[PIPE_BUF]; /* PIPE_BUF defined by system */

/* open the pipe to read */
fp = popen(command,"r");
/* read each line output by the command */
while ((fgets(buffer,PIPE_BUF,fp)) != NULL)
{
    /* process the line of output */
}
pclose(fp);

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Command to find a string in files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway