How to use a grep search to search for a specific string within multiple directories?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to use a grep search to search for a specific string within multiple directories?
# 1  
Old 03-19-2019
How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried:


Code:
grep -lr "this string" *


but I do not believe that is doing what I want.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-20-2019 at 05:11 AM.. Reason: Added CODE tags.
# 2  
Old 03-19-2019
Code:
find myDirToStartWith -type f | xargs grep 'this string'

# 3  
Old 03-19-2019
Do you think you could explain that line for me?


find myDirToStartWith -type f --- means go to the specified directory and look for a regular file? So if I start my grep search from within the directory where I want the search to begin than I can omit this line correct?


| --- means OR in my mind but I am assuming that in this instance it just separates two different commands?


xargs --- This will execute a command recursively? grep in this case




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-20-2019 at 05:13 AM.. Reason: Added CODE tags.
# 4  
Old 03-19-2019
Code:
find myDirToStartWith -type f | xargs grep 'this string'

Starting at directory 'myDirToStartWith' find recursively all the files of type file.
"pipe" (|) all the output of find as input to xargs grep 'this string' which greps for string 'this string' in each of it's input files.

Hope it helps.
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to Search a FILE for a STRING in multiple DIRECTORIES

Hi, Can someone please help me with a Command to Search a FILE for a STRING in multiple DIRECTORIES. I am searching for the VIP in HTTPD.CONF in multiple httpd instances. I am using find ./ -name "httpd.conf" | xargs grep 10.22.0.141 cut -d: -f3- | cut -d ' ' -f4 | sort | uniq -c ... (1 Reply)
Discussion started by: crosairs
1 Replies

2. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

3. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

4. Shell Programming and Scripting

How can i use grep to search a specific pattern?

Hi All, My file contain the below data : w_SA_infa1.log:INFO : LM_36620 : () Command task instance : running command , with command value . Binary file w_SA_infa1.log.bin matches w_SA_infa2.log:INFO : LM_36620 : (30377|1427806528) Command task instance : running command , with command value... (1 Reply)
Discussion started by: aliva Dash
1 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. UNIX for Advanced & Expert Users

word search in multiple directories

need a Command in UNIX which can find out a word from files in multiple directories e.g. /home contains multiple directories /home/d1 /home/d2 . . . . /home/dn under d1,d2...dn contains multiple files. I need to search a specific word in a files under these multiple... (1 Reply)
Discussion started by: jagkoth
1 Replies

7. Shell Programming and Scripting

Search a specific string

Hi, I have following requirement. Pls suggest. To search a string in a file which is combination of character and number(always 9 digit, but numeric). if found then caputure the exit return code as 0 else 1 , if 0 then next job can be triggerd. If exit code is 1, should return a failure... (10 Replies)
Discussion started by: zooby
10 Replies

8. UNIX for Dummies Questions & Answers

problem with grep on search string in a txt file over multiple files

I have a couple of things I got stuck on 1) I have a text file containing 25k search string that I need to search against compressed file. I have used this command but somehow it doesn't seems to use all the search terms. I have put one search string per line in the txt file (I clean up... (2 Replies)
Discussion started by: m00
2 Replies

9. UNIX for Dummies Questions & Answers

Search for files in multiple directories

I want to search for a file pattern in more than one directory. How I need to do that? Here is the scenario: I am having a directory structure like the following: /log ...../20051001 ..........ftp_server_20051001.log ..........ftp_down_server.log ..........ftp_up_server.log... (7 Replies)
Discussion started by: ravikirankethe
7 Replies

10. UNIX for Dummies Questions & Answers

Search all files for specific string

Hi Friends, How can I search all files in all slices on a unix system for a particular string within the file. e.g search string 'oracle' Thanks (4 Replies)
Discussion started by: sureshy
4 Replies
Login or Register to Ask a Question