Head command in Linux with examples – GeeksforGeeks

It is the plug-in for the Tail command. The head command, as the name implies, prints the upper data number N of the given input. By default, prints the first 10 lines of the specified files. If more than one file name is provided, the data in each file is preceded by its file name.

Syntax:

head [OPTION]… [ARCHIVE]…

Consider two files that have name state.txt and capital.txt contains all the names of the Indian states and capitals respectively.

$ cat status.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha Punjab Rajasthan Sikkim Tamil Nadu Telangana Tripura Uttar Pradesh Uttarakhand West Bengal Cat capital.txt Hyderabad Itanagar Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar

Without any options, it displays only the first 10 lines of the specified file. Example

: $ head state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir

Options

1. -n num: Prints the first ‘num’ lines instead of the first 10 lines. It is mandatory to specify num in the command, otherwise it displays an error.

$ head -n 5 state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh

2. -c num: Prints the first ‘num’ bytes of the specified file. The new line counts as a single character, so if the head prints a new line, it will count it as a byte. It is mandatory to specify num in command, otherwise it displays an error.

$ head -c 6 state.txt Andhra 3.

-q: Used if more than 1 file is given. Because of this command, the data in each file is not preceded by its file name.

Unused -q option $ head state.txt capital.txt ==> state.txt <== Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir==> capital.txt <== Hyderabad Itanagar Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar With the use of -q option $ head -q state.txt capital.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir Hyderabad Itanagar Dispur Patna Raipur Panaji Gandhinagar Chandigarh Shimla Srinagar 4. -v

: When you use this option, the data in the specified file is always preceded by its file name.

$ head -v state.txt ==> state.txt <== Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Himachal Pradesh Jammu and Kashmir

Head applications Print line command

between lines M and N(

  1. M>N): For this purpose, we use the head, tail and pipeline(|) commands. The command is: head -M file_name | tail +N since the head command takes first M lines and from M tail command lines cuts lines starting from +N to the end, we can also use head -M file_name | tail +(M-N+1) command since the head command takes first M lines and from M lines tail command cuts (M-N+1) lines starting from the end. Let’s say that from the file .txt state we have to print lines between 10 and 20.

$ head –n 20 state.txt | tail -10 Jharkhand Karnataka Kerala Madhya Pradesh Maharashtra Manipur Meghalaya Mizoram Nagaland Odisha

  1. How to use the head with pipeline(|): The head command can be piped with other commands. In the following example, the output of the ls command is piped to the head to display only the three most recently modified files or folders.

Show all recently used or modified files. $ ls -t e.txt d.txt c.txt b.txt a.txt Cut three most recently used files. $ ls -t | head -n 3 e.txt d.txt c.txt

  1. It can also be piped with one or more filters for further processing. For example, the sort filter could be used to sort the three most recently used files or folders in alphabetical order.

$ ls -t | head -n 3 | sort c.txt d.txt e.txt

  1. There are several other filters or commands along which we use head command. Primarily, it can be used to view huge log files on Unix.

This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See their article listed on the GeeksforGeeks homepage and help other Geeks.

Please write comments if you find something wrong, or if you want to share more information about the topic discussed above.