Results 1 to 10 of 11
Hi Everyone. I knew back in college when I was learning scripting it would come in handy one day. 20 years does a lot to erase a man's memory.
Here's ...
- 10-29-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 6
Script to print the last line of all files in a directory
Hi Everyone. I knew back in college when I was learning scripting it would come in handy one day. 20 years does a lot to erase a man's memory.
Here's what I want to do.
I have a directory that contains logs. Each log relates to a task that a user has performed. The last line of the log file contains a summary. I would like to read & print the last line or two from all the logs in the last 24 hours.
From a high level, I know I need to do this:
#!/bin/bash
while [time stamp of file < 24hours less than now]
do
print filename
print *****************************************
open current file
print last 3 lines
advance to next file
done
Can anyone give me some hints? Seems the nature of my job is calling for me being able to do this type of thing more and more.
Thanks in advance!
Chris
- 10-29-2009 #2Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
Pseudo:
Bash ScriptingCode:find /some/folder -name *.log -atime -1 exec tail -3 {} \;
Man Find
- 10-29-2009 #3Just Joined!
- Join Date
- Oct 2009
- Posts
- 6
Thanks. I'll give this a try.
- 10-29-2009 #4Just Joined!
- Join Date
- Oct 2009
- Posts
- 6
Using the vi editor, I created a file called results.sh and changed the permissions to 777.
Here are the contents of the file:
#!/bin/bash
find /opt/logs/ -name *.log -atime -1 exec tail -3 {} \;
<EOF>
when I run it I get an error.
[root@ca-crand-linux bin]# ./results.sh
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
[root@ca-crand-linux bin]#
I am going to start RTFM.
Chris
- 10-29-2009 #5
Try this:
Code:find /opt/logs/ -name '*.log' -atime -1 -exec tail -3 '{}' \;
- 10-29-2009 #6Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
As "pseudocode", it's not meant to be copy/paste - glad you went to the manual.
Google: linux find example - would also give quick results.
- 10-29-2009 #7Just Joined!
- Join Date
- Oct 2009
- Posts
- 6
Yup. Just ordered a used book too off Amazon after a trip to Borders proved fruitless. I've been searching lots of websites too.
On the other hand, I was wondering if I could do an ls with an option to list only those files with timestamps in the last 24 hours and pipe to a tail command. That would be okay too but I can't find anything showing me how to do that. Do you have any ideas?
Chris
- 10-29-2009 #8
Actually, what I think you want is ctime. What about this:
It gives you output like:Code:find /opt/logs/ -name '*.log' -ctime -1 -print -exec tail -3 '{}' \;
Code:/var/log/messages Oct 29 20:32:22 localhost init: cannot execute "/opt/IBM/db2/V8.1/bin/db2fmcd" Oct 29 20:32:22 localhost last message repeated 9 times Oct 29 20:32:22 localhost init: Id "fmc" respawning too fast: disabled for 5 minutes /var/log/yum.log Oct 29 20:03:51 Updated: subversion-perl.i386 1.1.4-3.el4_8.2 Oct 29 20:03:51 Updated: mod_dav_svn.i386 1.1.4-3.el4_8.2 Oct 29 20:03:53 Updated: subversion-devel.i386 1.1.4-3.el4_8.2
- 10-29-2009 #9Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
Atime works, but remember that once you tail the file, you have changed the atime.
You may want to explore atime/ctime and follow Thrillhouse's suggestion.
- 10-30-2009 #10Just Joined!
- Join Date
- Oct 2009
- Posts
- 6
I think this is working. I am doing tech support for a network and the element managment system runs on Linux. I want to look at the last line of the logs and wanted to get a head start. I tested that out in my lab and it looks okay. Now I'm going to work with the devleoper to see if he can use it in the customer's production network.
Thanks guys!


Reply With Quote