Typically you may open multiple terminals to view tail -f of multiple files as we explained in our previous 10 examples to view unix log files.
For example, if you want to view Apache error_log and access_log at the same time you may do the following in two different terminals.
On one terminal:
$ tail -f error_log
On another terminal:
$ tail -f access_log
But, wait!
Wouldn’t it be nice if you can execute multiple unix tail command in single terminal using one of the following methods?
$ multi-tail.sh error_log access_log (or) $ tail -f /var/log/syslog -f /var/log/auth.log (or) $ multitail error_log access_log
In this article let us review using three methods how to execute multiple Linux tail -f at the same time in single terminal.
Method 1: Use Custom Shell Script (with Unix tail command)
Create the multitail.sh as shown below.
$ vi multi-tail.sh #!/bin/sh # When this exits, exit all back ground process also. trap 'kill $(jobs -p)' EXIT # iterate through the each given file names, for file in "$@" do # show tails of each in background. tail -f $file & done # wait .. until CTRL+C wait
Now, open multiple files using this new shell script as shown below.
$ ./multi-tail.sh error_log access_log
Method 2: Using the standard Linux tail command
The latest version of the Unix tail command supports multiple -f as shown below.
$ tail -f /var/log/syslog -f /var/log/auth.log
The above will display file name as the first line each time, and then shows the newly grown lines. If you don’t want this to clutter the logs, you can use the next method.
Method 3. Use multitail command on Debian flavor of Linux
Install multitail as shown below.
$ apt-get install multitail
View multitail for multiple file
$ multitail /var/log/syslog /var/log/auth.log
Multitail utility has lot of additional features as explained in the mutitail home page.
- display log files in colors,
- scroll back in a log file,
- search inside log file,
- merge mutliple log files effectively
Comments on this entry are closed.
The output of the second method can be easily customized using grep or sed.
Don’t you think `screen’ would work better?
I do the following …
tail -f filename1
ctrl+z
bg
tail -f filename2
Ctrl+z
bg
jobs //to see what are being run in background
Awesome, I wasn’t aware that “multitail” existed until now.
I would have guessed you were going to also mention the “screen” command here also… 🙂
excellent article….
Thanks alot
Typo alert!
”
…
(or)
$ mutitail error_log access_log
”
should be
”
…
(or)
$ multitail error_log access_log
I do the following …
tail -f filename1 filename2 &
or
tail -f filename1 filename2
there one can see from which file the message comes.
==>filename1 filename2 <==
..
..
@Sergray,
Thanks for your feedback about using grep and sed along with tail -f.
@Dmitry, @Randy,
I agree with you that screen is also another effecient way to view multiple log files on one terminal
@Praveen,
Thanks for sharing your tips. Running tail -f in the background may be OK for two files. Anything more than that, it may get little confusing.
@Daniel,
Thanks for catching the typo. It’s fixed now.
@Emmierich,
Thanks for pointing out the we can view multi files without give -f after each and every file.
@Diptanu,
I’m glad you found this article helpful.
Tips on how to do this with screen?
I use Terminator: https://launchpad.net/terminator
multi-tail.sh script doesn’t kill tails in the background on my system (debian).
on AIX Method 1: works great, thanks
hi,
for SunOS I had to change
trap ‘kill $(jobs -p)’ EXIT
to
trap ‘kill $(jobs -p)’ 2
… and changed to #!/usr/bin/bash
If i may ask for help on a rather different topic:
I have a bash script to replace dates in
start_date = ‘2012-04-23_00:00:00′,’2012-04-23_00:00:00’,
end_date = ‘2012-04-25_00:00:00′,’2012-04-25_00:00:00′,
In my script below replaces the words “start_date and end_date”. How do I define the dates as replacement patterns? Will appreciate any help.
#!/bin/bash
ystart=`date “+%Y”`
mstart=`date “+%m”`
dstart=`date “+%d”`
YYYYMMDDstart=`date -u +%Y-%m-%d`
HHMMSS=”_00:00:00”
YYYYend=`date –date=’2 days’ -u +%Y`
MMend=`date –date=’2 days’ -u +%m`
DDend=`date –date=’2 days’ -u +%d`
YYYYMMDDend=`date –date=’2 days’ -u +%Y-%m-%d`
echo ${YYYYMMDDend}${HHMMSS}
cat namelist.wps.dailyGFS | sed s/start_date/${YYYYMMDDstart}${HHMMSS}/g > namelist.wps.tmp
cat namelist.wps.tmp | sed s/end_date/${YYYYMMDDend}${HHMMSS}/g > namelist.wps
rm namelist.wps.tmp
Great tips. Thanks.
I don’t really understand why you’d script this… my mac and linux machines can follow multiple files simultaneously without any special tricks.
tail -f file1 file2 file3
will follow all 3 files
You can also wildcard.
tail -f /var/log/*log
Thanks for this post, it was really helpful to me. I have a question on terminating tail -f processes. Normally, when I exit any tail -f command using ctrl +C, it still leaves the process running in background and I have to kill it separately by kill command. Is there any way to exit so that the process associated with this command is also terminated and I don’t have to explicitly kill it?
On Solaris 10, I changed:
trap ‘kill $(jobs -p)’ EXIT
to
trap ‘pgrep tail|xargs kill’ 2
Awesome!!
This was very helpful
But I want to tail logs in different folders :
/var/logfolder1/file.log
/var/logfolder2/file.log
/var/logfolder3/file.log
/var/logfolder4/file.log
/var/logfolder5/file.log
/var/logfolder6/file.log
I want to know if I can do something like this in just one line :
tail -f /var/*/file.log
Tail all file.log files inside the sub folders under /var
Screen lets you save sessions and log back into them later – no use for monitoring multiple files at the same time in the same window.
Did not know about multiple files after the tail -f command, though, so this is very useful! Thanks!
You can use the asterisk wildcard too, for example tail -f /var/log/*
watch -n 0 tail -n30 file1 file2