How can I identify the last saved log?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I identify the last saved log?
# 1  
Old 10-13-2008
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  
Old 10-13-2008
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  
Old 10-13-2008
how about doing something like this:

Code:
ls -ltr name_*.log | tail -n 1 | awk '{ print $NF }'

# 4  
Old 10-13-2008
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  
Old 10-13-2008
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  
Old 10-13-2008
Quote:
Originally Posted by gugs
[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  
Old 10-13-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cannot access saved file

This may be wrong place to ask. I am currently modifying "stock" configuration file and making slow progress. Run into a snag - twice so far. I can work on the project switching from editing to run all day long, no issue. Next day i cannot access the file to run it. The file... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

Squares in saved code

can you help i am merging 2 files together and saving to a third file with awk and its working with this code awk 'OFS="";NR==FNR{a=$0;next} {print a,"\n","\b",$0}' file1 file2 > file3the problem is in file3 when its saved i get a small square at the start of every 2nd line (see picture) ... (6 Replies)
Discussion started by: bob123
6 Replies

3. UNIX for Dummies Questions & Answers

Script Not getting Saved

Hi , Script File Is Not Getting Saved This Are The Steps I Am Following For Saving And Executing A Script 1). vi ( To Open Vi Editor ) 2). vi filename ( vi firstprog.ksh) #!bin\kash date 3) !wq :( Saving And Quit) When I Am Saving The Scrpit I Am Getting The Below... (1 Reply)
Discussion started by: anudeepkumar123
1 Replies

4. Shell Programming and Scripting

Identify log files based on time stamp,zip and then copy..HELP

Hi All, PFB is a requirement. I am new to shell scripting. So plz help. It would be highly appreciated. 1. choose all the log files based on a particular date (files location is '/test/domain')--i.e,we should choose all the files that are modified on 29th November, neither 28th nor 30th 2.... (3 Replies)
Discussion started by: skdas_niladri
3 Replies

5. UNIX for Dummies Questions & Answers

where alias saved?

step 1 # alias alias cp='cp -i' alias l.='ls -d .* --color=tty' alias ll='ls -l --color=tty' alias ls='ls --color=tty' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' step 2 # cat ~/.bashrc # .bashrc (3 Replies)
Discussion started by: cqlouis
3 Replies

6. UNIX and Linux Applications

Bluefish: where are the preferences saved?

I have just tried out Bluefish as an alternative to my regular text editor. If I save the modified preferences and reboot, the preferences have to be reentered again. Does anyone know which file the preferences are saved in? The command find / -mmin -5 | grep bluefish yields zero hits. Thanks... (2 Replies)
Discussion started by: figaro
2 Replies

7. Shell Programming and Scripting

Can STDERR be saved to a variable

Guys i'm trying to save STDERR to a variable for a portion of my ksh script on solaris. I know i can create redirects to files as such: exec 4>/tmp/lava print "This will be saved to /tmp/lava and not screen"; >&4 print "This will be seen on screen" >&2 I want to save the STDOUT of a... (4 Replies)
Discussion started by: lavascript
4 Replies

8. UNIX for Advanced & Expert Users

Sudo file not saved

Hi all, I have edited my sudoers file. I am using visudo command I have added the following lines and saved the file. I am saving the lines as :wq But I am very amazed to see that these lines are not written in the sudoers file. I have retried the above process many times, when I... (0 Replies)
Discussion started by: Asteroid
0 Replies

9. UNIX for Advanced & Expert Users

only root's crontab gets not saved

Hi, Something funny is happening over here: when a regular user edits his cron-file (crontab -e) saves and exits vi the correct new cron-file gets installed and saved to disk. But if root does the same, vi saves it but if I then check the cron-file it has the previous contents! I did strace (==... (1 Reply)
Discussion started by: flok
1 Replies
Login or Register to Ask a Question