LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-10-2007, 08:12 PM   #1
mcdrr
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Rep: Reputation: 0
Bash script to search through directories.


Hello everyone I was wondering if someone could please give an example point me in the right direction on how I would go about scripting a search script using bash, I am new to Linux and scripting, I can do basic bash scripts and just need some example to start from as what I have tried so far does not work.

I have log directories in the following format and would like to search the logs for specific information looping through several days.

/log/device/year/month/day/
/log/device/2007/05/01
/log/device/2007/05/02..then about 5 different log files in each "day" directory which I would like to grep for certain data.

What I would like to do is start searching at the "month" directory then have the script loop through the each one of the "day" folders.

Here is one example I often have to search the logs spanning five or six day and have to manually cd into each directory and do a search for and IP, like so grep 10.10.10.1 *
So what I would like to be able to do is grep/run the script for 10.10.10.1 but over a span XX days. Not sure if I should use find or grep or another command to search the log files.

thanks for all the help in advance
 
Old 05-11-2007, 02:32 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,246

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
Roughly like this, depending on what you want to do when you get a match
Also, pass in start pt /log/device/2007/05 from an outer loop as a variable or cmd line param?
(find is recursive)
Code:
for file in `find /log/device/2007/05 -type f print 2>/dev/null`
do
    grep $pattern $file 1>dev/null 2>&1
    if [[ $? -eq 0 ]]
    then
        echo "$pattern found in $file"
    fi
done
 
Old 05-11-2007, 02:34 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
firstly,
if you do a for dir in */
the slash will only pick up directories, useful but little known.

also you can do deep wild cards something like,

Code:
for dir in /log/device/05/*/*.log
do
   grep blah $dir
done
 
Old 05-11-2007, 02:35 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,284

Rep: Reputation: 172Reputation: 172
firstly,
if you do a for dir in */
the slash will only pick up directories, useful but little known.

also you can do deep wild cards something like,

Code:
for log in /log/device/05/*/*.log
do
   grep blah $log
done
or just

grep blah /log/device/05/*/*.log
 
Old 05-11-2007, 10:31 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
Both good advice/approaches.

Thanks for the tip, billy.
 
Old 05-11-2007, 05:41 PM   #6
mcdrr
LQ Newbie
 
Registered: Apr 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Great thanks all for the replies I will give the examples a shot and post back.

thanks again guys!!!
 
  


Reply

Tags
scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search all directories for a file in bash overlook1977 Linux - Newbie 7 04-10-2007 07:00 AM
bash: make rename script traverse directories morrolan Programming 2 11-08-2006 10:52 AM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 01:59 AM
Bash script about directories tarja Programming 3 11-23-2005 01:19 PM
search function (bash script) LYK Programming 2 05-27-2004 10:51 AM


All times are GMT -5. The time now is 06:41 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration