The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script in saved in compress format naveeng.81 Shell Programming and Scripting 1 04-14-2008 08:31 AM
mailx saved messages are unreadable (base64) Squeakygoose UNIX for Dummies Questions & Answers 1 01-30-2008 11:49 AM
Sudo file not saved Asteroid UNIX for Advanced & Expert Users 0 07-30-2007 07:18 AM
only root's crontab gets not saved flok UNIX for Advanced & Expert Users 1 06-25-2007 10:50 AM
Where are the password saved in a UNIX server? hast5 UNIX for Dummies Questions & Answers 12 10-19-2006 04:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
How can I identify the last saved log?

Our system produce logs when a script is run which may not be daily, the logs have a format: name_YYMMDD.log - both name and .log are consistent, date changes as per the day the script is run.

Is there a way of finding the last saved log?
  #2 (permalink)  
Old 10-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Code:
find /var/log -type f -printf "%T@\t%p\n" | sort -nr | head -1 |cut -f2
Explanation:
Look in /var/log and find all files (-type f) and print out their change-timestamp in seconds (which is sortable) and filename. Then sort by number in reverse-order so that the largest number will be first. Take only the first line and cut out the timestamp.
  #3 (permalink)  
Old 10-13-2008
Gee-Money Gee-Money is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 48
how about doing something like this:

Code:
ls -ltr name_*.log | tail -n 1 | awk '{ print $NF }'
  #4 (permalink)  
Old 10-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Actually, Gee-Money is on the ball. If your script is in a particular directory, you don't need find. Just ls. However, his script is unnecessarily complex:
Code:
ls -1t |sed -n '/name.*\.log$/{p;q}'
would do and be a bit quicker. (Normally, ls prints out in columns UNLESS it's printing out to a pipe. The -1 here just emphasizes that.)
  #5 (permalink)  
Old 10-13-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
Why does this not work?

I have taken the above example and developed a small test script as shown below to do what I want:

NEXT=TEST01
dir='C:/cygwin/var/log/img/aut/'
cmd1="sed -n '/"
echo $cmd1
cmd2=".*\.mqsc$/{p;q}'"
echo $cmd2
echo "$cmd1$NEXT$cmd2" > command.txt
command=$(grep sed command.txt)
ls -1t $dir | grep $NEXT > temp.txt
filename=$($command < temp.txt)

The results I get when I run the script are:
+ NEXT=TEST01
+ dir=C:/cygwin/var/log/img/aut/
+ cmd1='sed -n '\''/'
+ echo sed -n ''\''/'
sed -n '/
+ cmd2='.*\.mqsc$/{p;q}'\'''
+ echo '.*\.mqsc$/{p;q}'\'''
.*\.mqsc$/{p;q}'
+ echo 'sed -n '\''/TEST01.*\.mqsc$/{p;q}'\'''
++ grep sed command.txt
+ command='sed -n '\''/TEST01.*\.mqsc$/{p;q}'\'''
+ ls -1t C:/cygwin/var/log/img/aut/
+ grep TEST01
++ sed -n ''\''/TEST01.*\.mqsc$/{p;q}'\'''
sed: -e expression #1, char 1: unknown command: `''
+ filename=


  #6 (permalink)  
Old 10-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Quote:
Originally Posted by gugs View Post
[size=2]
Code:
'
+ echo 'sed -n '\''/TEST01.*\.mqsc$/{p;q}'\'''
There's a qouting problem somewhere. These escaped-double-quotes shouldn't be there at all.
[quote][code]
Why can't you just do:
Code:
ls -1t $dir >temp.txt
sed -n "/$NEXT/{p;q}" temp.txt
  #7 (permalink)  
Old 10-13-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
otheus there was no reason why I could not use:

ls -1t $dir >temp.txt
sed -n "/$NEXT/{p;q}" temp.txt

I don't even know why I didn't think of it! One of those days.

THANK YOU EVERYONE WHO HELPED, GREATLY APPRECIATED. I DON'T KNOW WHAT I WOULD WITHOUT THE HELP.
Sponsored Links
Closed Thread

Bookmarks

Tags
unix commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0