×

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last 10 lines of a file, but this can be modified with command line options. The tail command is often used to view the end of large log files or to monitor the output of a running process.

The basic syntax of the tail command is:

tail [options] [file]

When executed without any options, the tail command will display the last 10 lines of the specified file. For example, the following command will display the last 10 lines of the file "example.txt":

tail example.txt

The most commonly used option for the tail command is the "-n" option, which allows you to specify the number of lines to display. For example, the following command will display the last 20 lines of the file "example.txt":

tail -n 20 example.txt

Another useful option for the tail command is the "-f" option, which stands for "follow." This option causes the tail command to continuously display the last few lines of a file as they are added, similar to the "tail -f" command in Linux. This can be useful for monitoring log files or other files that are being updated in real-time.

tail -f example.txt

The tail command also has an option "-c" for displaying the last 'n' bytes of a file. This option is useful when you want to see the last bytes of a file, for example, last 100 bytes of a file.

tail -c 100 example.txt

Additionally, the tail command can also be used with pipes, which allows the output of one command to be passed as input to another command. For example, the following command will display the last 10 lines of the output of the "df" command, which shows the disk usage on the system:

df | tail

The tail command can also be used with the "grep" command to filter the output. For example, the following command will display the last 10 lines of the file "example.txt" that contain the word "error":

tail example.txt | grep error

The tail command can also be used in shell scripts and automation tasks to capture the last few lines of a file, as well as to monitor the output of a running process.

The tail command is a versatile command that can be used in a variety of ways, making it a valuable tool for system administrators, developers, and other users. Some of the other key features and uses of the tail command include:

  • By default, the tail command reads the entire file and then displays the last 10 lines. But with the option -n +m, it can be used to display the lines starting from the mth line of the file.
    • tail -n +m example.txt
  • The tail command can also be used to monitor multiple files at the same time using the option -q and -f. The -q option tells tail to suppress the output of the file name and the -f option makes tail to follow the files for changes.
    • tail -qf file1 file2
  • By using the option -v, tail command can be used to display the lines that are not common between two files.
    • tail -v file1 file2
  • The tail command can also be used to display the output of a command in real-time. For example, the following command will display the output of the "ping" command in real-time:
    • ping google.com | tail -f
  • The tail command can also be used to display the output of a command that is running in the background. For example, the following command will start the "command1" in the background and display the output in real-time:
    • command1 & tail -f logfile.txt
  • The tail command can also be used to display the output of multiple files at the same time by using the option -n and -f together. For example, the following command will display the last 5 lines of the file1, file2 and file3 in real-time:
    • tail -n 5 -f file1 file2 file3
  • The tail command can also be used to display the output of a command in a specific format. For example, the following command will display the output of the "ls" command in a long format:
    • ls -l | tail

In summary, the tail command is a powerful and versatile command that can be used in a variety of ways. It can be used to display the last few lines of a file, monitor the output of a running process, display the output of multiple files at the same time, and display the output of a command in a specific format. With the various options and combination of other commands, tail command is a very handy tool for system administrators, developers, and other users to monitor and troubleshoot the system.


Related Topics

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

4 minutes read.