Head command in Linux

The headcommand is one of the filtering commands used to display the first 10 lines by default.

head <option> <filename>

Options :

  • -n num: Prints the first ‘num’ lines instead of the first 10 lines.
  • -c num : Prints the first ‘num’ bytes from the file specified. Newline counts as a single character, so if the head prints out a new line, it will count it as a byte.
  • -q : It is used if more than 1 file is given. Because of this command, data from each file is not preceded by its file name.
  • -v: By using this option, data from the specified file is always preceded by its file name.

Examples :

By giving the below command, you can print the data within a specified range. To print the data within a specified range, we have to use both the head command and the tail command. In the head command, we are mentioning the starting line number, and in the tail command, we are mentioning the ending line number. As a result, the data is printed in the specified range.

$ head -n 20 f1 | tail -10

In the below command, we can display the two most recently used files or folders. The ls command provides the recently used files/folders. The output is then given to the head command, which filters and prints the two recent files/folders as an output.

//To Display recently used files. 
ls -t
//Cut three most recently used files.
ls -t | head -n 2

Thehead command can be piped with one or more filters for additional processing. In the below command, we can sort the three most recently used files or folders.

$ ls -t | head -n 3 | sort

Outputs :
head-command-linux


Tail command in Linux

The tail command displays the last 10 lines of data in the specified file by default.

Syntax :

tail <option> <filename>
Options:
  • -n num : This option prints the last ‘num’ lines instead of the last 10 lines. .This command can be written with or without the 'n' character but the '-' symbol is mandatory. The '+' option is not available in the head command. Without the '+' or '-' signs before num, the command will display the last num bytes from the specified file.
  • -c num : This option prints the last ‘num’ bytes from the file specified. In this option, the newline is also considered as a character and counted as a byte.
  • -q: This option is used if more than one file is given. While passing more than one file, only the data of the respective file is displayed and the filename is not displayed.
  • -v: By using this option, the filename and the data are displayed.
  • -f: This option displays the last 10 lines of the file along with the newly added lines or edited lines. We have to press ctrl+c to abort this command. This option is mainly used by the Unix system administrators to monitor the growth of the log files written by many Unix programmers as they are operating. The log files have error messages. While operating when you get any error, those errors are stored in the log files. By using the -f option you can check the error messages in the log file.

Examples :

By giving the below command the last 3 lines of the data in the f3 file are displayed.

tail -n 3 f3

By giving the below command the data from the second line of the file f3 are displayed.

tail -n +2 f3

By giving the below command without using the -q option the data along with the filename are displayed.

tail f3 f2

By giving the below command along with the -q option, only the data of both the files are displayed.

tail -q f3 f2​

By giving the below command with option -f, you can modify the data and you have to press ctrl+c to abort the editing wizard.

tail -f f3

By giving the below command with option -v, data of the given file along with the filename are displayed.

tail -v f3

To display the last two data of the file the below command is given,

tail -n 2 f3

To display the last two data of the file that should be sorted in reverse order, give the below command.

tail -n 2 f3 | sort -r

By giving the below command,

  • The cat command provides all the data present in the f3 file and sends the output to the head command
  • The head command filters data from the 1st line to the 2nd line and sends the output to the tail command.
  • The tail command provides the last 3 lines of the data.
  • By using the directive operator '>' the output is saved in the file f2.
cat f3 | head -n 2 | tail -n 3> f2
//To display the content in the f2 file
cat f2

By using the below command you can know the version of the tail command currently you have installed in your operating system.

tail --version

The collective output of all the tail command examples:

tail-command-output-linux
tail-output-linux

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions