scrubbicus Posted March 8, 2009 Share Posted March 8, 2009 There's a line of text I need to find inside a whole bunch of directories. Is there any script I can use for this? Or can someone help me start it out? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/ Share on other sites More sharing options...
trq Posted March 9, 2009 Share Posted March 9, 2009 Does it need to be done in php? grep is the perfect tool for this. grep -Hn 'text to search' /dir/* Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-780042 Share on other sites More sharing options...
Maq Posted March 9, 2009 Share Posted March 9, 2009 You can easily dump the results in a text file too, just add > file_name.txt to the end of the command. Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-780043 Share on other sites More sharing options...
scrubbicus Posted March 9, 2009 Author Share Posted March 9, 2009 this is for personal use so however it can be done i have a really messy directory i need to find something in Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-780545 Share on other sites More sharing options...
scrubbicus Posted March 10, 2009 Author Share Posted March 10, 2009 I'm confused with the grep -Hn 'text to search' /dir/* Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-780962 Share on other sites More sharing options...
trq Posted March 10, 2009 Share Posted March 10, 2009 Whats confusing about it? Its a Linux comamnd that will search through all files contained within /dir looking for the text 'text to search'. Upon finding the words 'text to search' it will print the file name, line number the text was found on, then the line itself. Of course, you never mentioned what OS your using so this command might not be a great deal of help. Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-780968 Share on other sites More sharing options...
scrubbicus Posted March 10, 2009 Author Share Posted March 10, 2009 I'm using Windows Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-780984 Share on other sites More sharing options...
trq Posted March 10, 2009 Share Posted March 10, 2009 Then you'll likely need to write a script that will loop through each file within your directory using scandir and foreach, read each file into a string using file_get_contents, search this string for your search term using either strstr or preg_match and if the term is found place the file name into an array. You can then loop through the array to list all files containing your search term. Quote Link to comment https://forums.phpfreaks.com/topic/148530-searching-directories-for-a-line-of-text/#findComment-781018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.