LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2010, 09:00 AM   #1
raj k yadav
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Rep: Reputation: 2
tail the output of tail -n 1


Hi All,

I want to tail the latest log file in one command line. I dont know what command/option to use for the same.

Please help me. Thanks in advance.
 
Old 02-01-2010, 09:08 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,387
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
If you mean that you want to look at the latest log message from any of the /var/log files, then something like:
Code:
tail -n 1 $(ls -t /var/log/ | head -n 1)
(the ls part finds the name of the most recently modified log file)

This won't work with the '-f' (follow) flag for the tail.

Last edited by neonsignal; 02-01-2010 at 09:09 AM.
 
Old 02-01-2010, 09:28 AM   #3
raj k yadav
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 2
Thanks for the solution. It works for me but i need to understand a bit more. The way I want to do is:

cd /data/logs/LOG/SIG_SER/Feb-2010-SIG_SER; ls -ltrh| tail -n 1 > $LOGFILE1 | tail -n 20 "echo $LOGFILE1"
-bash: 1: ambiguous redirect
tail: cannot open `echo ' for reading: No such file or directory


Can you please guide me on this.
 
Old 02-01-2010, 09:48 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
This will corect the error
Code:
cd /data/logs/LOG/SIG_SER/Feb-2010-SIG_SER; ls -ltrh | tail -n 1 > $LOGFILE1 | tail -n 20 "$LOGFILE1"
but this is neater
Code:
cd /data/logs/LOG/SIG_SER/Feb-2010-SIG_SER; ls -ltrh | tail -n 1 | tail -n 20 -
EDIT:

Nonsense! Sorry This is better
Code:
cd /data/logs/LOG/SIG_SER/Feb-2010-SIG_SER
tail -n 20 "$(ls -ltrh | tail -n 1)"

Last edited by catkin; 02-01-2010 at 09:50 AM.
 
Old 02-01-2010, 03:39 PM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Jessie (Fluxbox WM)
Posts: 1,387
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by raj k yadav View Post
Thanks for the solution. It works for me but i need to understand a bit more. The way I want to do is:

cd /data/logs/LOG/SIG_SER/Feb-2010-SIG_SER; ls -ltrh| tail -n 1 > $LOGFILE1 | tail -n 20 "echo $LOGFILE1"
You are on the right track, but there are several issues.

The quotes aren't quite correct in the last part of your command. To insert the output of a command like echo into another command, you either have to use backquotes, or the $() notation, eg:
Code:
tail -n 20 `echo $LOGFILE1`
Code:
tail -n 20 "$(echo $LOGFILE1)"
However, there is no need to 'echo' a variable, since you can place it directly in the command anyway.

Also, you cannot assign to a variable $LOGFILE by using the redirection. So ultimately your line would end up something like this:

Code:
LOGFILE1=$(ls -tr | tail -n 1); tail -n 20 "$LOGFILE1"
I suspect you are also trying to display the log file details as well. This requires a separate command. The pipe '|' is fine for passing results from one command to the next, but the semicolon ';' is used for running multiple commands on a single line. eg

Code:
LOGFILE1=$(ls -tr | tail -n 1); ls -lh "$LOGFILE1"; tail -n 20 "$LOGFILE1"

Last edited by neonsignal; 02-01-2010 at 03:55 PM.
 
1 members found this post helpful.
Old 02-07-2010, 12:26 AM   #6
raj k yadav
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 2
Thanks to Catkin and Neonsignal. The way u explained is very good. It really worked for me. Thank you so much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
netstat - grep - tail -f output log manwithaplan Linux - Networking 4 08-21-2009 06:36 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
fdisk and tail result in no output ubume2 Ubuntu 1 05-07-2008 05:47 AM
How to make newer "tail" behave like older "tail" rylan76 Linux - Software 4 12-07-2007 05:27 AM
can output of tail be redirected? dsids Linux - Newbie 19 10-16-2006 03:19 AM


All times are GMT -5. The time now is 08:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration