Home » Linux tail Command

Linux tail Command

by Online Tutorials Library

Linux tail command

Linux tail command is used to display the last ten lines of one or more files. Its main purpose is to read the error message. By default, it displays the last ten lines of a file. Additionally, it is used to monitor the file changes in real-time. It is a complementary command of the head command.

Syntax:

Let’s dive into the tail command with some examples.

Default behavior

The default use of the tail command displays the last ten lines of the files. Create a filenum.txt’ having numbers 1 to 15 (each number in a new line).

Let’s open it by executing the tail command without any arguments as follows:

Consider the below output:

Linux File tail

From the above output, we can see the last ten lines of the ‘num.txt’ are displayed.

Display the specific number of Lines

The ‘-n’ option displays the specified number of lines. To specify the number of lines, execute the command as follows:

It will display the specified number of lines from the last. Consider the below example:

The above command will display the last five lines of the file ‘num.txt’. We can also omit the letter ‘n’ instead, and we can use the hyphen (-) and the number without any space. Consider the below output:

Linux File tail

As we can see from the above output, the last five lines of ‘num.txt’ are displayed.

Display the specified number of bytes

The ‘-c’ option displays the specified number of bytes from the last. To display the specified number of bytes, execute the command as follows:

It will display the specified number of bytes. Consider below example:

The above command will display the file content up to 6 bytes from last. Consider the below output:

Linux File tail

From the above output, the last six bytes of the file ‘num.txt’ are displayed.

We can also use the suffix with the number such as b, kb, k, MB, and more to specify the number of bytes. These suffixes multiplied the specified number as:

b: multiplies it by 512.

kb: multiplies it by 1000.

k: multiplies it by 1024.

MB: multiplies it by 1000000.

Track a file for changes

To track a file for changes, the ‘-f’ option is used. Here, ‘-f’ stands for the following. It is useful for monitoring log files. Execute the below command:

The above command will monitor the file ‘num.txt’. To exit from monitoring, press “CTRL+C” keys. Consider the below output:

Linux File tail

Display multiple files

We can display multiple files from last at once by executing the tail command. To display the multiple files, provide the file names as input. It will display the last ten lines of specified files.

For example, we have another file ‘alphabet.txt’ that contains every character of the alphabet in a new line. To display both files ‘num.txt’ and ‘alphabet.txt,’ execute the command as follows:

The above command will display the last ten lines of the specified files. Consider the below output:

Linux File tail

As from the above output, we can see that the last ten lines of the specified files are displayed at once.

Tail with other commands

The tail command can be used with other commands. It can be piped to other commands to filter the output. Consider the below command:

From the above command, we have piped the tail command with the ls command. It will only display six files or folders modified the longest time ago. Consider the below output:

Linux File tail

Let’s execute the tail command with ps command to display the top running process. Execute the command as follows:

Consider the below output:

Linux File tail


Next TopicLinux cat

You may also like