≡ Menu

3 Methods To View tail -f output of Multiple Log Files in One Terminal

Linux Tail Command for 2 filesTypically 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 on Linux - View multiple log files together

Fig: multitail - Click on the image to enlarge

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
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • sergray September 9, 2009, 2:28 am

    The output of the second method can be easily customized using grep or sed.

  • Dmitri Minaev September 9, 2009, 2:50 am

    Don’t you think `screen’ would work better?

  • Praveen September 9, 2009, 7:26 am

    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

  • Randy September 9, 2009, 8:01 am

    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… 🙂

  • diptanu September 9, 2009, 8:25 am

    excellent article….

    Thanks alot

  • Daniel September 9, 2009, 10:44 am

    Typo alert!


    (or)

    $ mutitail error_log access_log

    should be


    (or)

    $ multitail error_log access_log

  • emmerich September 10, 2009, 3:15 pm

    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 <==
    ..
    ..

  • Ramesh Natarajan September 10, 2009, 11:39 pm

    @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.

  • Ted September 12, 2009, 11:50 pm

    Tips on how to do this with screen?

  • Ikon February 1, 2010, 3:30 am
  • matej February 12, 2010, 12:08 pm

    multi-tail.sh script doesn’t kill tails in the background on my system (debian).

  • Anonymous June 25, 2011, 6:40 am

    on AIX Method 1: works great, thanks

  • alex March 8, 2012, 3:13 am

    hi,
    for SunOS I had to change
    trap ‘kill $(jobs -p)’ EXIT
    to
    trap ‘kill $(jobs -p)’ 2

  • alex March 8, 2012, 3:15 am

    … and changed to #!/usr/bin/bash

  • Zilore Mumba April 29, 2012, 6:17 am

    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

  • Carlos Estensser May 19, 2012, 11:25 am

    Great tips. Thanks.

  • Stephen Z May 29, 2012, 3:44 am

    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

  • Milind December 28, 2012, 12:04 pm

    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?

  • Jimmy August 28, 2013, 1:46 pm

    On Solaris 10, I changed:

    trap ‘kill $(jobs -p)’ EXIT

    to

    trap ‘pgrep tail|xargs kill’ 2

  • nisav November 7, 2013, 11:03 am

    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

  • no screen June 14, 2014, 6:26 pm

    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!

  • Matt August 24, 2014, 8:29 am

    You can use the asterisk wildcard too, for example tail -f /var/log/*

  • Marco Coutinho June 17, 2015, 3:37 am

    watch -n 0 tail -n30 file1 file2