Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    6

    Question 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

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Pseudo:

    Code:
    find /some/folder -name *.log -atime -1 exec tail -3 {} \;
    Bash Scripting

    Man Find

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    6
    Thanks. I'll give this a try.

  4. #4
    Just 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

  5. #5
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Try this:
    Code:
    find /opt/logs/ -name '*.log' -atime -1 -exec tail -3 '{}' \;

  6. #6
    Linux 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.

  7. #7
    Just 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

  8. #8
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Actually, what I think you want is ctime. What about this:
    Code:
    find /opt/logs/ -name '*.log' -ctime -1 -print -exec tail -3 '{}' \;
    It gives you output like:
    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

  9. #9
    Linux 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. #10
    Just 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!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...