How to search all the files in a directory for a specific string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to search all the files in a directory for a specific string
# 1  
Old 12-14-2009
How to search all the files in a directory for a specific string

Hi Guys,

I want to search the content of all the files (of a particular type like .txt)
in a directory for a specific string pattern. Can anyone help me?

Thanks
# 2  
Old 12-14-2009
Code:
grep "string" *.txt

should do you ?
# 3  
Old 12-14-2009
Thanks. how can i search all the sub directories?
# 4  
Old 12-14-2009
If you have GNU grep:
Code:
grep -r 'string' *.txt

Otherwise:
Code:
find . -type f -name '*.txt' -print | xargs grep 'string'

# 5  
Old 12-14-2009
Code:
grep -i "string" `find ./ -name "*.txt"`


Last edited by pludi; 12-14-2009 at 08:36 AM.. Reason: code tags
# 6  
Old 12-14-2009
Quote:
Originally Posted by vikas898
Code:
grep -i "string" `find ./ -name "*.txt"`

That approach has 2 potential problems:
  1. The *.txt glob will be expanded if there are files with the txt extension in the current directory, screwing up the find command.
  2. If find returns too many files, grep/the shell will refuse running and complain about a too large argument list

Also, I'd advise anyone against using backticks and use the $() construct instead, as it's less likely to be misinterpreted, and can be nested multiple times.
# 7  
Old 12-15-2009
Thanks a lot Pludi Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. UNIX for Beginners Questions & Answers

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: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

3. Shell Programming and Scripting

How to search for a string in all the files irrespective of directory.?

Hi All, How to search for a string in all the files irrespective of directory. If I use grep -i 'hello' *.* It will search for the string hello in the files of current directory. But I want to check for the string hello in the files of all the directories. Thanks (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

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

5. UNIX for Dummies Questions & Answers

How to search in specific directory using find?

Hi, Is there any way to use find command and search only specific subdirectories in a directory. for example /home/d1 /home/d2 /home/d3 i want to search in the following directories /home /home/d1 /home/d2 i do not want the find command to search the /home/d3 directory. (6 Replies)
Discussion started by: Little
6 Replies

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

7. UNIX for Dummies Questions & Answers

How to search directory for specific file?

I am new to Unix scripting and would like some help. Here is my scenario: 1) I have a text files that contains two fields: file name and retention period in months: File1 36 file2 24 File3 12 2) The directory I am searching contains sequential files. 3) I need to be able to take the file name... (10 Replies)
Discussion started by: Mustafa19804
10 Replies

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

9. Shell Programming and Scripting

Search for a file in specific directory

I have to search a file in a prticular directory. filename will be passed through command line. The directory may contain subdirectory. i.e. suppose directory in /u03/appl (it can hard coded in script). This directory may contain subdirectory. $ scriptname.sh filename output should be... (2 Replies)
Discussion started by: jadoo_c2
2 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