How to Use tail Command in Linux

With the tail command in Linux, you can instruct your system to display the last lines of either single or multiple files. This command-line utility also helps in analyzing file logs and the changes that happen over time.

There are instances when you need to look at the data of any file from the end, especially when something is either added or omitted. In that case, the Linux tail command is of great value. 

From monitoring a file to displaying a new entry, everything is made simple with the tail command. In this tutorial, we’ll guide you through the steps of using the command and instruct the system to display the desired data.

Tail Command in Linux: Syntax

Just like any other command, the tail command in Linux has a basic form that looks something like this:

$ tail [OPTION]...[FILE]...

Here, the [OPTION] section refers to the instructions that we provide in the form of various flags. At the same time, [FILE] signifies the input file names. 

The tail command, when used without defining any [OPTION], displays the last 10 lines. On the contrary, running the tail command with no [FILE] specified reads the standard input.

Using tail Command in Linux

As already mentioned, when you run the tail command in Linux without specifying an option, the system usually displays the last 10 lines.

For Example:

$ tail newfile.txt

Displaying Specific Number of Lines with tail Command in Linux

The tail command in Linux can instruct your system to display a specific number of lines. For this, you’ll need to input the -n (--lines) flag.

The Format:

$ tail -n <NUMBER> filename.txt

For Example:

$ tail -n 50 newfile.txt
specifying number of lines or a file

Running the command as mentioned above will display the last 50 lines of the file named “newfile”. Interestingly, you can also omit the letter n and use the number and hyphen instead. It will display the same result.

Displaying Specific Number of Bytes with tail Command in Linux

To instruct your system for displaying a specific number of bytes, use the tail command alongside the -c (--bytes) flag.

The Format:

$ tail -c <NUMBER> filename.txt

For Example:

$ tail -n 500 newfile.txt
tail command in linux

Running the command as mentioned above will display 500 bytes of data for the file named newfile. A multiplier suffix after the number will allow you to indicate the number of bytes that you want your system to show.

b flag: multiplies by 512

kB flag: multiplies by 1000

K flag: multiples by 1024

MB flag: multiplies by 1000000

M flag: multiples by 1048576

Using tail Command in Linux to Watch a File for Changes

If you’re willing to monitor the changes in a log file, the tail command is of great significance. The -f (--follow) option is all you need to input alongside the usual command.

For instance, 

$ tail -f /varlog/nginx/error .log
watch a file for changes in linux

Running the command will display the last 10 lines for the log file and will also check for updates. However, if you need to interrupt the tail command’s progress while monitoring a file, use the “Ctrl+C’ key combination.

Displaying Multiple Files

Getting the last ten lines from multiple files is pretty straightforward. Simply run the tail command followed by the file names. Also, using the -n flag will help you specify the number of lines for each file as well.

For Example:

$ tail newfile1.txt newfile2.txt newfile3.txt
tail command for multiple files

Running the command will display the last ten lines for three files.

Using tail Command with Other Commands

The tail command features a great utility which makes it capable of being used with other functions. It is usually done by redirecting the standard output from one to the other utilities via pipes.

For Example:

Running the following code will monitor the Apache of the provided access log file and display the lines that have the IP address 192.178.32.12

$ tail -f /var/log/apache2/access.log | grep 192.178.32.12
tail command for redirecting the standard output

Uses of tail Command in Brief

-c, --bytes=[+]NUM: It output the last NUM bytes; or use 

-c +NUM: You can use this command to output starting with byte NUM of each file

 -f, --follow[={name|descriptor}]: Using the command lets output appended data as the file grows; an absent option argument means ‘descriptor.’

-n, --lines=[+]NUM: It output the last NUM lines, instead of the last 10; or use

-n +NUM: The user can use this flag to output starting with line NUM

--max-unchanged-stats=N

with --follow=name: This flag lets you reopen a FILE that has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files); with notify, this option is rarely useful

 --pid=PID (with -f): It terminates the process once the ID, PID dies

 -q, --quiet, --silent: They never output headers giving file names

 --retry: The flag keep trying to open a file if it is inaccessible

-s, --sleep-interval=N: with -f, it instructs the command to sleep for approximately N seconds

--pid=P: This particular command checks process P at least once every N seconds

v, --verbose: This command always output headers giving file names

-z, --zero-terminated: the command line delimiter is NUL, not newline

--help flag: The command display this help and exit

-version: This flag output version information

This is everything that you need to know about tail command in Linux and its uses. The article walks you through the application of this command in the most user-friendly manner. However, if you still hold any queries, use the comment box and let us know.

If this guide helped you, please share it.

Leave a Reply
Related Posts