×

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

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.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

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

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

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

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.

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.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

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.

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.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.