General :: Using 'find' And 'grep' In A For Loop?
Mar 23, 2011
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:
[URL]
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?
View 4 Replies
ADVERTISEMENT
Jun 3, 2010
I'm trying quickly dig through our VPN logs for a list of class A ips , and I'm having trouble. I have a file with a list of numbers, one per line. I'm wanting to incorporate each number into an extended regex:
Code:
for i in `cat ips`; do zgrep -E '[[:space:]]$i(.[0-9]{1,3}){3}[[:space:]]' vpn1/2009.08.20.log.gz; done
If I substitute one of the numbers in for $i, it works for that number. For example:
Code:
for i in `cat ips`; do zgrep -E '[[:space:]]58(.[0-9]{1,3}){3}[[:space:]]' vpn1/2009.08.20.log.gz; done
I think the loop is passing literally "$i" to zgrep instead of its value due to the tics, and I can't figure out how to resolve this. I can't remove the tics b/c zgrep doesn't like that. Putting a backslash in front of $i doesn't work.and the number loop seems to be working fine:
Code:
for i in `cat ips`; do echo $i; done
1
14
27
43
[code]....
View 3 Replies
View Related
Sep 11, 2009
I am trying to do a find/grep/wc command to find matching files, print the filename and then the word count of a specific pattern per file. Here is my best (non-working) attempt so far:
wc `find . ( -name "*.as" -o -name "*.mxml" ) -exec grep -H HeightResizableList {}` ;
View 10 Replies
View Related
Feb 28, 2011
I have used diff command in past.I faced a situation to which I did not had a cluehere are some text strings (which can be stored in a file)Quote:
CONFIG_XEN=y
CONFIG_XEN_PVHVM=y
CONFIG_XEN_MAX_DOMAIN_MEMORY=128
[code]...
View 3 Replies
View Related
Nov 2, 2010
I am looking for all the files that contain the text string 'moo.sql'. I ran the following:
find . -name '*.php' | grep -lir 'moo.sql' *
Unfortunately it seems to return non-php files in addition to php files. I thought the find portion of this would filter the file names so grep would only search php files.
View 3 Replies
View Related
Dec 24, 2010
For searching a file or directory i normally use grep command. kindly can you guide me the difference between grep and find command. I have used both but that are the difference between them ? are the same or grep is new as comapird to find command.
View 2 Replies
View Related
Dec 1, 2010
I'm trying to use grep to find the words in the dictionary that contain the letters "th" and the letter m.
I tried grep 'th m*.' Desktop/Dictionary/words(Thats where the destined dictionary word document is located)
grep 'th' Desktop/Dictionary/words works but only for the words with th. I have no idea of what expression to use to make it a unionized expression with m
View 3 Replies
View Related
Dec 5, 2010
I want to find files containing the "$" char (ascii 0x24). 'Grep -irl $ *' would output the names of every file in path *, of course, because it means end of line (EOL). So giving grep the string "$" won't do. So I tried 'grep -irl $ *'. But this doesn't work either and I do not understand why. Am I not escaping the dollar sign? grep should interpret it literally. Neither 'grep -irl "$" *' will work. Fortunately, there's LQ, besides grep's man page.
View 9 Replies
View Related
Feb 12, 2010
I am new to linux as well as awk, grep or sed. I need a find and replace command single liner or script that loops trough input file (file1) and find the particular input in file2 and add "!" in front of the found string.
Example:
input file: file1
g+h=o+p
a+b=c+d
file2 (file that need to look for)
a+b=c+d1e105
x+y=z+s5e105
g+h=o+pabcdefg
t+r=w+qxvyderf
Output file (file3 should look like this)
!a+b=c+d1e105
x+y=z+s5e105
!g+h=o+pabcdefg
t+r=w+qxvyderf
I have tried many awk and sed method of find and replce but it did not work the way I wanted. This is mainly due to my lack of experience in awk and sed. The program should loop trough file1 and find in file2 and output in file3 for the 1st (g+h=o+p) set then repeat the same process again for set 2 (a+b=c+d).
View 14 Replies
View Related
Oct 20, 2010
I am bussy with a litle bash script but i have now a problem.I have a file on the server with every time different text.Somewere in this text the is the following line:PHP Code:<BR><DIV CLASS='itemTotalsTitle'>2 Matching Service Entries Displayed</DIV> I want to make a bash script that replace this line when it says:"0 Matching Service Entries Displayed"To a other text like:"There a no knowing problem(s) on this moment."]If there is a other number than "0" than replace this line with:2 problems have been found on this moment, whe are bussy to fix this problem
View 3 Replies
View Related
Jan 5, 2011
When I used the find command, I almost always need to search the local drives. But, I almost always have super large network shares mounted and these are included in the search. Is there an easy way to exclude those in the find command, grep and other similar commands? Example:
find / -name .vimrc
View 1 Replies
View Related
Mar 22, 2010
I have some big files of logs that contain errors printed by an app. They are most of the time relevant, however most of them are similar. So i figured i could check what happened between a time interval with a find.
Im using this one
Code:
And I get an output similar to this one.
Code:
Is there a way to condensate the output lines to get only one or two, indicating the start and last occurrence of a block? Or I need to create a program to do so?
Because right now I get thousands of similar lines, but when I'm scrolling through them i sometimes miss relevant information that i would've otherwise noted if it wasn't all that spammy.
View 10 Replies
View Related
Dec 6, 2010
I tried to tag late onto a question similar to mine on stackoverflow (Find Non-UTF8 Filenames on Linux File System) to elicit further replies, with no luck so far, so here goes again... I have the same problem as the OP in the link above and convmv is a great tool to fix one's own filesystem. My question is therefore academic, but I find it unsatisfactory (in fact I can't believe) that 'find' is not able to find non standard ascii characters.
Is there anyone out there that would know what combination of options to use to find filenames that contain non standard characters on what seems to be a unicode FS, in my case the characters seem to be 8bits extended ascii rather than unicode, the files come from a Windows machine (iso-8859-1) and I regularly need to fetch them. I'd love to see how find and/or grep can do the same as convmv.
[Code]....
View 2 Replies
View Related
Sep 1, 2010
I have a mytext file with month and year as two separate fields. likemytext fil
08 2010
09 2010
10 2010
........
........
........
I want to read the values of each field i.e., month and year into an awk script.
View 10 Replies
View Related
Jan 8, 2011
How would I use a unix grep regular expression to find any two capital letters side by side and how would I find an expected comma in an expected spot?
View 2 Replies
View Related
Jun 14, 2011
How to find file with name="php.ini" on linux using grep command?
View 2 Replies
View Related
Oct 27, 2010
I want to write some code to search for a specific string in a text file, but without using grep command.
View 5 Replies
View Related
Dec 15, 2010
if I'd a file like this
something
somethine123
begin something end
begin xyz end
somethingelse
and I want to find the strings only between begin and end, exluding begin and end; How can I grep that?
View 3 Replies
View Related
Jun 16, 2010
I have inherited a wordpress theme with a folder of images that I think are no longer being used. I wanted to find the orphaned images using grep, so I wrote this script:
Code:
#!/bin/bash
echo $PWD
for i in *.*; do
cd ..
[Code].....
Its seems like I got some false positives out of it, but it worked pretty ok. I guess. :| Of course, it is not checking for images in the content of the database.
Orphan finding has to be a wheel that is already invented.
View 1 Replies
View Related
Nov 11, 2010
I'm trying to find exact matches of some users in the /etc/passwd file using "grep -w", but it doesn't always work. For example, I have the following users:[URl].. So, let's say, I want to search for the user "stewart" (which doesn't exist)
[Code]...
View 13 Replies
View Related
Jun 21, 2011
Kernel 2.6.21.5, Slackware 12.0
Just after the booting process is finished, running ps I get this:
Code:
As you can see, there are a lot of names beginning with the letter 'k'. Are these processes needed for the GUI to be fully functional when it is run (X Window System + WM + Xfce4)? And can I setup the system such that they're never run?
The reason for this question is that I am testing some programs and need the CPU to have as little time stolen as possible.
View 4 Replies
View Related
Mar 20, 2010
Getting the list of files in the root directory that have changed less than 10 hours earlier, using grep, but without the directories.
View 1 Replies
View Related
Dec 4, 2010
I'd like to mount an iso file ,however, it failed as following: ljk@ljk-laptop:~/Test$ modinfo loop ERROR: modinfo: could not find module loop what I can do?
View 1 Replies
View Related
May 28, 2010
I'm trying to math all class references in a C++ file using grep with regular expression. I'm trying to know if a specific include is usuless or not, so I have to know if there is a refence in cpp. I wrote this RE that searches for a reference from class ABCZ, but unfortunately it isn't working as I espected:
grep -E '^[^(/*)(//)].*[^a-zA-Z]ABCZ[]*[*(<:;,{& ]'
^[^(/*)(//)] don't math comments in the begging of the line ( // or /* )
.* followed by any character
[code]....
Well, I can get patterns like this:
class Test: public ABCZ{
class Test: public ABCZ {
class Test : public ABCZ<T>
[code]....
View 4 Replies
View Related
Nov 22, 2010
I need to kind of grep within grep. My input file would be something like:
[Code]....
and I need to find the first occurrence of hello before MY PATTERN (hello 9008 in this case), so the output should be:
[Code]....
View 4 Replies
View Related
Aug 24, 2011
i need to scan all files on a dns server (bind 9, lenny) to find a string (192.168) i been suggested to use find with grep i filter out the things i dont need like logs here's the command
find /* -name "slot*" -prune -o -name "var" -prune -o -name "proc" -prune -o -name "log" -prune -o -name "*logs*" -prune -o -name "*history*" -prune -o -type f -print | xargs grep -n '192.168' 2>/dev/null
it show me results with various depth in the directories but it fail to show the occurrence that are in /etc/bind/zones/ if i run the exact same command but with the path being /etc/bind/* the missing occurrence are shown what did i do wrong ?
View 4 Replies
View Related
Nov 10, 2010
./test.sh: line 3: syntax error near unexpected token `cd'
./test.sh: line 3: `cd $f'
--------------------------------
[code]....
View 1 Replies
View Related
Apr 7, 2011
I need to find file from ls - l by using grep and certain regexp.
In particular I need greep to see certain file ending, like let's say .txt but only using regex.
So it should be something like
ls -l | grep '^.+.txt$'
But that doesn't seem to work for me.
View 1 Replies
View Related
Mar 7, 2011
I have a requirement to check the following conditions..If my folders are not A and B then list files in the directory else no listing the filesso my if loop is some thing like this.
Code:
if [ $dirName = "C" && $dirName != "A" && != "B" ] then
ls -la
[code]....
View 1 Replies
View Related
Apr 21, 2010
I have bash 4.0. I used the following for loop example, but all it outputs is "{1..10} instead of the actual numbers one through ten. why?
Code:
for a in {1..10}
do
echo -n "$a "
done
View 5 Replies
View Related