×

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

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.

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.

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.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

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

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

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

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.

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.

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.

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.

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

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.

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.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

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.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.