In Linux, the “tail” command is used to display the last lines of a file or the output of another command. It allows you to see a specified number of lines from the end of a file. Here’s how to use the “tail” command:
- Open a Terminal:
Launch a terminal emulator on your Linux system.
- Type the “tail” command:
The basic syntax of the “tail” command is as follows:
tail [options] [file(s)]
- The “[file(s)]” parameter represents the name(s) of the file(s) you want to display. You can specify multiple file names separated by spaces.
- If you don’t provide any file name, “tail” will read input from the standard input (e.g., the output of a command or piped input). For example, to display the last 10 lines of a file called “file.txt”, you can use the following command:
By default, the “tail” command displays the last 10 lines of the file.
- Specifying the Number of Lines:
You can specify the number of lines to display using the “-n” option followed by the desired number. For example, to display the last 5 lines of a file, you can use the following command:
tail -n 5 file.txt
The “tail” command will display the last 5 lines of “file.txt”.
- Following the File:
The “-f” option can be used to continuously display the appended lines of a file in real-time. This is useful for monitoring log files or other continuously updated files. For example:
tail -f file.log
The “tail” command will display the last few lines of “file.log” and keep monitoring for new lines. As new lines are appended to the file, they will be displayed in the terminal.
- Using Options:
The “tail” command provides various options to modify its behavior. Some commonly used options include:
- “-n” (number of lines): Specify the number of lines to display.
- “-c” (bytes): Display the specified number of bytes instead of lines.
- “-q” (quiet): Suppress the printing of headers when multiple files are specified.
- “-v” (verbose): Always print headers when multiple files are specified. For example, to display the last 3 lines of a file without printing the file name, you can use the following command:
tail -n 3 -q file.txt
- Combining “tail” with Other Commands:
You can use the “tail” command in conjunction with other commands by piping the output of one command as input to “tail”. For example, to display the last 5 lines of the output of a command, you can use the following command:
command | tail -n 5
Replace “command” with the actual command whose output you want to display.
- Exiting the Command:
Once the desired lines are displayed using the “tail” command, you can continue executing other commands or exit the terminal as needed.
The “tail” command is useful for quickly viewing the last lines of a file or the output of other commands. Use the available options to customize the behavior of “tail” according to your requirements.