The head and tail commands in LINUX


Introduction

The command line is a powerful tool for managing and manipulating files and directories in Linux. Two essential commands for working with text files are the “head” and “tailcommands. These commands allow users to display the beginning or end of a file, respectively. In this article, we will explore the usage and options of the head and tail commands in Linux.

head command

The head command is used to view the first few lines of a file. By default, it will display the first 10 lines of a file, but this number can be changed with the ‘-n’ option. The syntax for the head command is as follows −

$ head [options] [file(s)]

head command options

The head command has several options that can be used to customize its output. Some of the most used options are −

  • -n − The -n option is used to specify the number of lines to display. For example, to view the first 20 lines of a file named "example.txt", the command would be:

$ head -n 20 example.txt
  • -q − The -q option is used to suppress header printing when multiple files are used.

  • -v − The -v option is used to always print headers when using multiple files.

Uses of head command

The head command can be used in various situations, such as −

Viewing the beginning of a large file

When working with large files, it can be useful to view the beginning of the file to get an idea of ​​its contents without having to open it in an editor or viewer.

Comparing the beginning of multiple files

You can also use the main command to compare the beginning of multiple files.

$ head -n 20 file1.txt file2.txt

tail Command

The tail command is used to display the last few lines of a file. Like the head command, tail will display the last 10 lines of a file by default, but this number can be changed with the -n option. The syntax of the tail command is as follows −

$ tail [options] [file(s)]

tail command options

The tail command also has several options that can be used to customize its output. Some of the most used options are −

-n − The -n option is used to specify the number of lines to display. For example, to view the last 20 lines of a file named "example.txt", the command would be:

$ tail -n 20 example.txt

-f − The ‘-f’ option is used to keep the file open and continue displaying output as the file grows. This option is useful when working with log files.

$ tail -f example.log

-F − The ‘-F’ option is similar to the ‘-f’ option, but it also controls file truncation.

Usage of tail command

The tail command can be used in various situations, such as −

See the end of a large file

When working with large files, it can be useful to view the end of the file to get an idea of ​​its contents without having to open it in an editor or viewer.

Monitoring log files

The tail -f option is commonly used to monitor log files in real time. This is useful for troubleshooting and analyzing the behavior of a system or application.

Comparing the end of multiple files

You can also use the tail command to compare the ends of multiple files.

$ tail -n 20 file1.txt file2.txt

Advanced usage

The head and tail commands can also be combined with other command line utilities such as grep, sed, awk, etc. to perform more complex tasks, such as −

  • Extracting a specific section of a file based on a pattern

  • Extracting a specific column from a CSV file

  • Extracting a specific line from a file

Conclusion

The head and tail commands are essential tools for working with text files on Linux. They allow users to quickly and easily view the beginning or end of a file, respectively. With the various options available, users can also specify the number of lines to always display, suppress, or print headers when using multiple files, and monitor files as they grow. These commands are easy to use and can save time when working with large or complex files. Remember that these commands can also be combined with other command-line utilities to perform more complex tasks.

Updated on: 13-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements