LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-23-2011, 01:31 PM   #1
linuxlastslonge
Member
 
Registered: Jun 2002
Location: Franklin, IN
Distribution: Debian, CentOS, Mac OS X
Posts: 158

Rep: Reputation: 30
Exclamation Using 'find' and 'grep' in a for loop


I have a server hosting 100+ websites. I need to quickly identify which websites are configured with a database. There are way too many to manually check every website for a PHP file with a database name. So, I created a list of all databases from MySQL and put them in a text file. I then exported the text file to a shell variable and used it in a for loop.

bash variable
Code:
DBLIST=`cat dblist.txt`
Example of $DBLIST
Code:
db1 db_testing2 database_clientname production words4cheap
for loop
Code:
for db in $DBLIST; do find . -type "f" -iname "*.php" -exec grep -i $db '{}' \; -print; done
Note: my find statement starts searching at . which is the directory that contains all of my websites and their data, each website is setup in a sub directory, identified by it's domain name.

Example: I'm in /var/www. Beneath /var/www are a list of directories:

blah.com
foobar.com
blahfoo.com

However, this is taking too long (it's been running most of the day) and I was wondering if there wasn't an easier way to accomplish what I'm trying to achieve?

Thanks!
 
Old 03-23-2011, 01:59 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
What distro are you running? If you have the resources installed, you could use locate, since updatedb is ran as a scheduled cron job and creates a index for using locate. It is very fast, and I use it all the time. Hope that helps,

Josh
 
1 members found this post helpful.
Old 03-23-2011, 02:14 PM   #3
linuxlastslonge
Member
 
Registered: Jun 2002
Location: Franklin, IN
Distribution: Debian, CentOS, Mac OS X
Posts: 158

Original Poster
Rep: Reputation: 30
Good idea, but the real lag is coming from looping through 100+ databases and grepping every php file in /var/www for a match. I guess I'm just stuck playing the waiting game ... :-\
 
Old 03-23-2011, 02:19 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
I know that, but using locate is usually faster than find.
 
1 members found this post helpful.
Old 03-23-2011, 04:16 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by linuxlastslonge View Post
Code:
DBLIST=`cat dblist.txt`
Use $(command) instead of `command`. It can't be confused with commas and is more easily nested.
 
1 members found this post helpful.
  


Reply



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] using awk and grep in a loop nesrin Programming 8 12-06-2010 05:34 AM
Strange error using grep in a while loop anil3 Linux - General 4 03-30-2009 03:09 AM
Grep in a for loop. rose_bud4201 Programming 13 03-18-2009 04:02 AM
Grep result in a loop and linefeed issue romainp Programming 2 06-13-2008 10:11 AM
problem with 'rpm -qa | grep' in while loop ionic_slim Programming 9 11-11-2007 08:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration