{"id":5281,"date":"2021-03-23T07:41:32","date_gmt":"2021-03-23T07:41:32","guid":{"rendered":"https:\/\/linuxways.net\/?p=5281"},"modified":"2021-03-23T07:41:32","modified_gmt":"2021-03-23T07:41:32","slug":"use-of-head-command-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/use-of-head-command-in-linux\/","title":{"rendered":"Use of Head Command in Linux"},"content":{"rendered":"<p>From the name implies, the head command shows by default the 10 top lines from a file. This command can be piped with other commands to produce standard output. The head command is a command-line utility through which you can easily retrieve the top data from a specified file and show the result to standard output.<\/p>\n<p>We will explain in this tutorial, how to use the head command, a command-line utility on a Linux system. We will explain head command options with brief examples.<\/p>\n<h2><strong>Syntax of Head Command<\/strong><\/h2>\n<p>The head command has the following syntax:<\/p>\n<pre>head &lt;options&gt; &lt;filename&gt;<\/pre>\n<p><strong>Options:<\/strong> Head options are used to specify the action which action performs on a file. For example, use option -n to specify the line number.<\/p>\n<p><strong>Filename: <\/strong>you can specify one or more file names as input using this argument. If you will not mention a file name then, it reads the standard raw input.<\/p>\n<h2><strong>Use of head command with examples<\/strong><\/h2>\n<p>We will discuss the following different uses of head command in this article:<\/p>\n<h3><strong>Use of Head command without an option<\/strong><\/h3>\n<p>When the head command is used with file name without any option then, in that case, it returns the first 10 lines of a given file as follows:<\/p>\n<pre>$ head filename.txt<\/pre>\n<p>For example, using the head command we want to retrieve the first 10 lines from our test file.<\/p>\n<pre>$ head testfile.txt<\/pre>\n<p>The following output returns on the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1093\" height=\"725\" class=\"wp-image-5282\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-336.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-336.png 1093w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-336-300x199.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-336-1024x679.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-336-768x509.png 768w\" sizes=\"auto, (max-width: 1093px) 100vw, 1093px\" \/><\/p>\n<h3><strong>Print the specified lines using the head command<\/strong><\/h3>\n<p>By using the -n (&#8211;lines) option along with the specified integer number, you can display the desired number of lines from a file as follows:<\/p>\n<pre>$ head -n &lt;integer&gt; filename.txt<\/pre>\n<p>For example, you want to print the first 20 lines from a file. In this case, you would mention 20 with option n as follows:<\/p>\n<pre>$ head -n 20 testfile.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1091\" height=\"798\" class=\"wp-image-5283\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-337.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-337.png 1091w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-337-300x219.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-337-1024x749.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-337-768x562.png 768w\" sizes=\"auto, (max-width: 1091px) 100vw, 1091px\" \/><\/p>\n<p>You can also omit the option n from the above command and just mention the integer with a hyphen as follows:<\/p>\n<pre>$ head -20 testfile.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1091\" height=\"801\" class=\"wp-image-5284\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-338.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-338.png 1091w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-338-300x220.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-338-1024x752.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-338-768x564.png 768w\" sizes=\"auto, (max-width: 1091px) 100vw, 1091px\" \/><\/p>\n<h3><strong>Print specified bytes using the head command<\/strong><\/h3>\n<p>You can also print the specified number of bytes of a file by using the option \u2018-c\u2019 along with the head command as follows:<\/p>\n<pre>$ head -c &lt;integer&gt; filename.txt<\/pre>\n<p>Let\u2019s explain with an example, to print the first 200 bytes of data from a file through the head command. You would run the following command:<\/p>\n<pre>$ head -c 200 testfile.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1102\" height=\"217\" class=\"wp-image-5285\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-339.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-339.png 1102w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-339-300x59.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-339-1024x202.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-339-768x151.png 768w\" sizes=\"auto, (max-width: 1102px) 100vw, 1102px\" \/><\/p>\n<p>The multiplier suffix can also specify to display the bytes. For example,<\/p>\n<p>The following multipliers you can use with a specified integer of bytes:<\/p>\n<ul>\n<li>b multiplies by 512.<\/li>\n<li>kB multiplied by 1000<\/li>\n<li>K multiplied by 1024<\/li>\n<li>MB multiplied by 1000000<\/li>\n<li>M multiplies by 1048576<\/li>\n<\/ul>\n<p>To print the top 2 kilobytes, execute the following command:<\/p>\n<pre>$ head -c 2k testfile.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1101\" height=\"789\" class=\"wp-image-5286\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-340.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-340.png 1101w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-340-300x215.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-340-1024x734.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-340-768x550.png 768w\" sizes=\"auto, (max-width: 1101px) 100vw, 1101px\" \/><\/p>\n<h3><strong>Specify multiple files as input with head command<\/strong><\/h3>\n<p>The multiple files, you can also take as input with the head command as follows:<\/p>\n<pre>$ head testfile.txt testfile2.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1096\" height=\"792\" class=\"wp-image-5287\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-341.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-341.png 1096w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-341-300x217.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-341-1024x740.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-341-768x555.png 768w\" sizes=\"auto, (max-width: 1096px) 100vw, 1096px\" \/><\/p>\n<p>The above-mentioned command will show you the top ten lines from each file name.<\/p>\n<p>The same options are used with the head command in case of multiple files.<\/p>\n<h3><strong>Head command use with the tail command<\/strong><\/h3>\n<p>The head command can be also used with other commands. For example, to print the lines between 10 and 20 lines, you can use the tail command as follows:<\/p>\n<pre>$ head -n 20 testfile.txt | tail -10<\/pre>\n<h3><strong>Head command pipeline with other commands<\/strong><\/h3>\n<p>The head command can also be used as a pipe with other commands. For example, to display the most recent 2 modified files use the following command:<\/p>\n<pre>$ ls -t \/etc | head -n 2<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1093\" height=\"131\" class=\"wp-image-5288\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-342.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-342.png 1093w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-342-300x36.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-342-1024x123.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-342-768x92.png 768w\" sizes=\"auto, (max-width: 1093px) 100vw, 1093px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We have given in this article a good understanding of the use of head command with all required options. We have seen how to use other commands with head commands effectively. By using the tail command with a head command, you can also display the last lines of a file on the terminal.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>From the name implies, the head command shows by default the 10 top lines from a file. This command can be piped with other commands to produce standard&hellip;<\/p>","protected":false},"author":1,"featured_media":5330,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[239],"class_list":["post-5281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-head-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=5281"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5281\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/5330"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=5281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=5281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=5281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}