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?
# 8  
Old 10-13-2008
Quote:
Originally Posted by otheus
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.)

No, ls only prints in columns if the output is going to a terminal. Anywhere else (e.g., pipe or redirection), it prints one file per line.
# 9  
Old 10-13-2008
Quote:
Originally Posted by gugs
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?

Since the shell will sort the files automatically, there is no need for any external command:

Code:
set -- name_*.log
shift $(( $# - 1 ))
lastlog=$1

# 10  
Old 10-13-2008
Another cheap and easy way:

Code:
ls -t | grep log$ | head -n 1

I might even use that in a script...
# 11  
Old 10-14-2008

There's nothing cheap about using three external commands when you don't need any.
# 12  
Old 10-14-2008
Quote:
Originally Posted by cfajohnson

There's nothing cheap about using three external commands when you don't need any.
Considering the extremely lightweight task being performed, even three external commands are, in fact, extremely cheap with system resources. Also, the three commands in question are among those that many users have in their UNIX toolbox quite readily; hence the "easy". There's really no advantage to declining to use external commands.
# 13  
Old 10-14-2008
Quote:
Originally Posted by treesloth
Considering the extremely lightweight task being performed, even three external commands are, in fact, extremely cheap with system resources.

Three external commands are always much more expensive in terms of time and systems resources than none.
Quote:
Also, the three commands in question are among those that many users have in their UNIX toolbox quite readily; hence the "easy". There's really no advantage to declining to use external commands.

External commands use many times more resources and time than shell builtin functions and syntax.

If the tasks are lightweight, that's all the more reason for not using heavy external commands.
# 14  
Old 10-14-2008
Quote:
Originally Posted by cfajohnson

Three external commands are always much more expensive in terms of time and systems resources than none....
Sure, and if the original task were anything even vaguely approaching a heavy task, it would make a difference. The original task was extremely lightweight. Many times virtually nil is, in this case, still very much virtually nil. Yes, there is merit in knowing the internal methods; in this particular case, the difference is utterly irrelevant. My goal is add to the body of methods offered, not to claim that mine is the only one, or even the best for all circumstances.

Last edited by treesloth; 10-14-2008 at 04:52 AM..
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