LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-01-2007, 11:15 AM   #1
jeepescu
LQ Newbie
 
Registered: Aug 2006
Posts: 7

Rep: Reputation: 0
bash script help - finding last n log files in all sub folders


Hello everyone,
I am quite a bit puzzled by the following scripting need:

I have a tree of subfolders, starting from a root named "logs". There can be any number of knots, I will call them a, b and c in this attempt to explain things. Each knot can have n leaves, let's name them a1, a2, a3 and b1, and b2.
Each leaf folder will store log files, whose names are like YYYY-MM-DD.HHMMSS.log
The script I need will have to return the last n log files for every leaf, where n is as script parameter passed at runtime.

So it would look like this:

jeepescu@gondolin:/$ . myScript.sh 2
/home/jeepescu/logs/a/a1 2007-11-01.103005.log
/home/jeepescu/logs/a/a1 2007-11-01.102900.log
/home/jeepescu/logs/a/a2 2007-10-15.102105.log
/home/jeepescu/logs/a/a2 2007-09-11.103005.log
/home/jeepescu/logs/a/b1 2007-09-05.234900.log
/home/jeepescu/logs/a/b1 2007-09-11.102900.log
/home/jeepescu/logs/a/b2 2007-10-21.102105.log
/home/jeepescu/logs/a/b2 2007-09-17.103005.log


Now, is bash scripting a good tool for writing such a thing, or should I start looking into perl, python or other creature ?


Thanks in advance.
 
Old 11-01-2007, 12:15 PM   #2
vadiml
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 19
something like:

for d in logs/*/*
do
ls -1 $d | tail -n $1
done
 
Old 11-01-2007, 12:38 PM   #3
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
"ls -1tr" might be better as it will force the output to be list chronologically (most recent at the bottom). My high school computer teacher told us to never leave anything to chance. Tell the computer exactly how you want it. Granted, the file names naturally lend themselves to being chronoligical, but you never know when someone might change the output format, and it leaves it being a bit more open to other, future application only having to change the base directory of the search.

Just my 2 cents.
 
Old 11-02-2007, 02:17 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
change
ls -1 $d | tail -n $1
to
ls -1t $d | head -n $1

sorts newest at top
 
Old 11-03-2007, 07:57 PM   #5
jeepescu
LQ Newbie
 
Registered: Aug 2006
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks to all who replied.
It works marvelously!!!
 
  


Reply


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
Bash script to put log files into single file and email DragonM15 Programming 13 11-08-2007 03:27 AM
Bash script for server log (namely var/log/messages) tenaciousbob Programming 17 05-24-2007 10:43 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
Need help with finding path in bash script ylikone Linux - Software 1 08-02-2005 10:47 AM
Finding out the creation date for files/folders... darkarcon2015 Linux - Software 1 03-24-2005 05:59 PM


All times are GMT -5. The time now is 05:16 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