Search only text files with 'find' command?


 
Thread Tools Search this Thread
Operating Systems Linux Search only text files with 'find' command?
# 1  
Old 09-08-2012
Question Search only text files with 'find' command?

I've been using this to search an entire directory recursively for a specific phrase in my code (html, css, php, javascript, etc.):

Code:
find dir_name -type f -exec grep -l "phrase" {} \;

The problem is that it searches ALL files in the directory 'dir_name', even binary ones such as large JPEG images. With a directory also containing large number of images dispersed everywhere, it dramatically reduces the efficiency. Sometimes it takes very long to get a response.

Is there any parameter I can easily use to make it only search text files?
# 2  
Old 09-08-2012
Why not use the -name primary to find?
Check man find.

Last edited by elixir_sinari; 09-08-2012 at 03:38 AM..
# 3  
Old 09-08-2012
There is no find option for picking or excluding text files. By convention, JPEG files usually end in .jpgor.jpeg, but it is just a convention and even following convention it could be in uppercase or lowercase. If you know that the JPEG images in your file hierarchy all have the extension.jpg, you could exclude them from the search using:
Code:
find dir_name -type f ! -name '*.jpg' -exec grep -l "phrase" {} +

Note also the+instead of\;at the end of the -exec primary. That will produce the same output, but will invoke grep many fewer times (which will significantly improve performance when processing lots of files).

If your source files all use common naming conventions you could restrict your search to just those files using something like:
Code:
find dir_name -type f \( -name '*.htm' -o -name '*.html' ... \) -exec grep -l "phrase" {} +

where the...is replaced by other-o -name '*.ext'sequences specifying the rest of the filename extensions matching your set of source files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for text in files

I am working on a script to search for text. read -p "Enter text to search for : " n1 grep $n1 /home/andy/bin/*.sh It works, but if I backspace, it does this. Enter text to search for : sset Master^ ------ Post updated at 12:39 PM ------ My mistake is I used the left arrow key... (4 Replies)
Discussion started by: drew77
4 Replies

2. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

3. Shell Programming and Scripting

Find command to search and delete files older than 1 days at a desired location

Hello All, Can someone please help me out in creating the find command to search and delete files older than 1 days at a desired location. Thanks in advance for your help. (3 Replies)
Discussion started by: Pandee
3 Replies

4. Shell Programming and Scripting

Find command to search files in a directory excluding subdirectories

Hi Forum, I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result. cd $dir... (8 Replies)
Discussion started by: jhilmil
8 Replies

5. Shell Programming and Scripting

smart search through find command

The requirement here is i'm searching for a particular word from the log file between two time stamp. The process for particular account will happen, so i need to see there is any error occurred in between before it completes. in the below code yyyyy and xxxxx are two unique fields. initially... (1 Reply)
Discussion started by: Amutha
1 Replies

6. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

7. Shell Programming and Scripting

Using find command to search a pattern in group of files

Hi i am having a group of *.csh files under parent directory. Now i want to search a particular pattern in these group of *.csh files(suppose i need to search a pattern ABC - proj ). Can anyone please tell me how to do it using find command. Thanks in advance sarbjit (4 Replies)
Discussion started by: sarbjit
4 Replies

8. UNIX for Dummies Questions & Answers

sorting files with find command before sending to text file

i need help with my script.... i am suppose to grab files within a certain date range now i have done that already using the touch and find command (found them in other threads) touch -d "$date_start" ./tmp1 touch -d "$date_end" ./tmp2 find "$data_location" -maxdepth 1 -newer ./tmp1 !... (6 Replies)
Discussion started by: deking
6 Replies

9. UNIX for Advanced & Expert Users

find file with date and recursive search for a text

Hey Guyz I have a requirement something like this.. a part of file name, date of modification of that file and a text is entered as input. like Date : 080206 (MMDDYY format.) filename : hotel_rates text : Jim now the file hotel_rates.ZZZ.123 (creation date is Aug 02 2006) should be... (10 Replies)
Discussion started by: rosh0623
10 Replies

10. Shell Programming and Scripting

To search for a text in until i find that

In a CSH, i need to write a loop to seach continuously for text "Started" and then only exit from the loop when ever if finds that. Can someone please suggest how we can write this Thanks, Sateesh (3 Replies)
Discussion started by: kotasateesh
3 Replies
Login or Register to Ask a Question