{"id":12133,"date":"2019-03-22T11:35:05","date_gmt":"2019-03-22T11:35:05","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12133"},"modified":"2019-03-22T11:35:05","modified_gmt":"2019-03-22T11:35:05","slug":"manage-files-effectively-using-head-tail-and-cat-commands-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/22\/manage-files-effectively-using-head-tail-and-cat-commands-in-linux\/","title":{"rendered":"Manage Files Effectively using head, tail and cat Commands in Linux"},"content":{"rendered":"<p>There are several commands and programs provided by\u00a0<strong>Linux<\/strong>\u00a0for viewing the contents of file. Working with files is one of the daunting task, most of the computer users be it newbie, regular user, advanced user, developer, admin, etc performs. Working with files effectively and efficiently is an art.<\/p>\n<div id=\"attachment_5919\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/04\/Manage-files-in-linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5919\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2014\/04\/Manage-files-in-linux.png\" alt=\"View Content of Files in Linux\" width=\"435\" height=\"321\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Manage Files in Linux<\/p>\n<\/div>\n<p>Today, in this article we will be discussing the most popular commands called\u00a0<strong>head<\/strong>,\u00a0<strong>tail<\/strong>\u00a0and\u00a0<strong>cat<\/strong>, most of us already aware of such commands, but very few of us implement it when needed.<\/p>\n<h3>1. head Command<\/h3>\n<p>The head command reads the first ten lines of a any given file name. The basic syntax of head command is:<\/p>\n<pre>head [options] [file(s)]<\/pre>\n<p>For example, the following command will display the first ten lines of the file named \u2018<strong>\/etc\/passwd<\/strong>\u2018.<\/p>\n<pre># <strong>head \/etc\/passwd<\/strong> \r\n\r\nroot:x:0:0:root:\/root:\/bin\/bash \r\ndaemon:x:1:1:daemon:\/usr\/sbin:\/bin\/sh \r\nbin:x:2:2:bin:\/bin:\/bin\/sh \r\nsys:x:3:3:sys:\/dev:\/bin\/sh \r\nsync:x:4:65534:sync:\/bin:\/bin\/sync \r\ngames:x:5:60:games:\/usr\/games:\/bin\/sh \r\nman:x:6:12:man:\/var\/cache\/man:\/bin\/sh \r\nlp:x:7:7:lp:\/var\/spool\/lpd:\/bin\/sh \r\nmail:x:8:8:mail:\/var\/mail:\/bin\/sh \r\nnews:x:9:9:news:\/var\/spool\/news:\/bin\/sh<\/pre>\n<p>If more than one file is given, head will show the first ten lines of each file separately. For example, the following command will show ten lines of each file.<\/p>\n<pre># head \/etc\/passwd \/etc\/shadow\r\n\r\n==&gt; <strong>\/etc\/passwd<\/strong> &lt;== root:x:0:0:root:\/root:\/bin\/bash bin:x:1:1:bin:\/bin:\/sbin\/nologin daemon:x:2:2:daemon:\/sbin:\/sbin\/nologin adm:x:3:4:adm:\/var\/adm:\/sbin\/nologin lp:x:4:7:lp:\/var\/spool\/lpd:\/sbin\/nologin sync:x:5:0:sync:\/sbin:\/bin\/sync shutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown halt:x:7:0:halt:\/sbin:\/sbin\/halt mail:x:8:12:mail:\/var\/spool\/mail:\/sbin\/nologin uucp:x:10:14:uucp:\/var\/spool\/uucp:\/sbin\/nologin ==&gt; <strong>\/etc\/shadow<\/strong> &lt;==\r\nroot:$6$85e1:15740:0:99999:7:::\r\nbin:*:15513:0:99999:7:::\r\ndaemon:*:15513:0:99999:7:::\r\nadm:*:15513:0:99999:7:::\r\nlp:*:15513:0:99999:7:::\r\nsync:*:15513:0:99999:7:::\r\nshutdown:*:15513:0:99999:7:::\r\nhalt:*:15513:0:99999:7:::\r\nmail:*:15513:0:99999:7:::\r\nuucp:*:15513:0:99999:7:::<\/pre>\n<p>If it is desired to retrieve more number of lines than the default ten, then \u2018<strong>-n<\/strong>\u2018 option is used along with an integer telling the number of lines to be retrieved. For example, the following command will display first\u00a0<strong>5<\/strong>\u00a0lines from the file \u2018<strong>\/var\/log\/yum.log<\/strong>\u2018 file.<\/p>\n<pre># <strong>head -n5 \/var\/log\/yum.log<\/strong>\r\n\r\nJan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686\r\nJan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686\r\nJan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686\r\nJan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch\r\nJan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch<\/pre>\n<p>In fact, there is no need to use \u2018<strong>-n<\/strong>\u2018 option. Just the hyphen and specify the integer without spaces to get the same result as the above command.<\/p>\n<pre># <strong>head  -5 \/var\/log\/yum.log<\/strong>\r\n\r\nJan 10 00:06:49 Updated: openssl-1.0.1e-16.el6_5.4.i686\r\nJan 10 00:06:56 Updated: openssl-devel-1.0.1e-16.el6_5.4.i686\r\nJan 10 00:11:42 Installed: perl-Net-SSLeay-1.35-9.el6.i686\r\nJan 13 22:13:31 Installed: python-configobj-4.6.0-3.el6.noarch\r\nJan 13 22:13:36 Installed: terminator-0.95-3.el6.rf.noarch<\/pre>\n<p>The head command can also display any desired number of bytes using \u2018<strong>-c<\/strong>\u2018 option followed by the number of bytes to be displayed. For example, the following command will display the first\u00a0<strong>45<\/strong>\u00a0bytes of given file.<\/p>\n<pre># <strong>head -c45 \/var\/log\/yum.log<\/strong>\r\n\r\nJan 10 00:06:49 Updated: openssl-1.0.1e-16.el<\/pre>\n<h3>2. tail Command<\/h3>\n<p>The tail command allows you to display last ten lines of any text file. Similar to the head command above, tail command also support options\u00a0 \u2018<strong>n<\/strong>\u2018 number of lines and \u2018<strong>n<\/strong>\u2018 number of characters.<\/p>\n<p>The basic syntax of tail command is:<\/p>\n<pre># <strong>tail [options] [filenames]<\/strong><\/pre>\n<p>For example, the following command will print the last ten lines of a file called \u2018<strong>access.log<\/strong>\u2018.<\/p>\n<pre># <strong>tail access.log<\/strong> \r\n\r\n1390288226.042      0 172.16.18.71 TCP_DENIED\/407 1771 GET http:\/\/download.newnext.me\/spark.bin? - NONE\/- text\/html\r\n1390288226.198      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html\r\n1390288226.210   1182 172.16.20.44 TCP_MISS\/200 70872 GET http:\/\/mahavat.gov.in\/Mahavat\/index.jsp pg DIRECT\/61.16.223.197 text\/html\r\n1390288226.284     70 172.16.20.44 TCP_MISS\/304 269 GET http:\/\/mahavat.gov.in\/Mahavat\/i\/i-19.gif pg DIRECT\/61.16.223.197 -\r\n1390288226.362    570 172.16.176.139 TCP_MISS\/200 694 GET http:\/\/p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com\/v6exp3\/redir.html pg \r\n1390288226.402      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html\r\n1390288226.437    145 172.16.18.53 TCP_DENIED\/407 1723 OPTIONS http:\/\/172.16.25.252\/ - NONE\/- text\/html\r\n1390288226.445      0 172.16.18.53 TCP_DENIED\/407 1723 OPTIONS http:\/\/172.16.25.252\/ - NONE\/- text\/html\r\n1390288226.605      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html\r\n1390288226.808      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html<\/pre>\n<p>If more than one file is provided, tail will print the last ten lines of each file as shown below.<\/p>\n<pre># <strong>tail access.log error.log<\/strong>\r\n\r\n==&gt; access.log &lt;== 1390288226.042      0 172.16.18.71 TCP_DENIED\/407 1771 GET http:\/\/download.newnext.me\/spark.bin? - NONE\/- text\/html 1390288226.198      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html 1390288226.210   1182 172.16.20.44 TCP_MISS\/200 70872 GET http:\/\/mahavat.gov.in\/Mahavat\/index.jsp pg DIRECT\/61.16.223.197 text\/html 1390288226.284     70 172.16.20.44 TCP_MISS\/304 269 GET http:\/\/mahavat.gov.in\/Mahavat\/i\/i-19.gif pg DIRECT\/61.16.223.197 - 1390288226.362    570 172.16.176.139 TCP_MISS\/200 694 GET http:\/\/p4-gayr4vyqxh7oa-3ekrqzjikvrczq44-if-v6exp3-v4.metric.gstatic.com\/v6exp3\/redir.html pg  1390288226.402      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html 1390288226.437    145 172.16.18.53 TCP_DENIED\/407 1723 OPTIONS http:\/\/172.16.25.252\/ - NONE\/- text\/html 1390288226.445      0 172.16.18.53 TCP_DENIED\/407 1723 OPTIONS http:\/\/172.16.25.252\/ - NONE\/- text\/html 1390288226.605      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html 1390288226.808      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html ==&gt; error_log &lt;==\r\n[Sun Mar 30 03:16:03 2014] [notice] Digest: generating secret for digest authentication ...\r\n[Sun Mar 30 03:16:03 2014] [notice] Digest: done\r\n[Sun Mar 30 03:16:03 2014] [notice] Apache\/2.2.15 (Unix) DAV\/2 PHP\/5.3.3 mod_ssl\/2.2.15 OpenSSL\/1.0.0-fips configured -- resuming normal operations<\/pre>\n<p>Similarly, you can also print the last few lines using the \u2018<strong>-n<\/strong>\u2018 option as shown below.<\/p>\n<pre># <strong>tail -5 access.log<\/strong>\r\n\r\n1390288226.402      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html\r\n1390288226.437    145 172.16.18.53 TCP_DENIED\/407 1723 OPTIONS http:\/\/172.16.25.252\/ - NONE\/- text\/html\r\n1390288226.445      0 172.16.18.53 TCP_DENIED\/407 1723 OPTIONS http:\/\/172.16.25.252\/ - NONE\/- text\/html\r\n1390288226.605      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html\r\n1390288226.808      0 172.16.16.55 TCP_DENIED\/407 1753 CONNECT ent-shasta-rrs.symantec.com:443 - NONE\/- text\/html<\/pre>\n<div class=\"google-auto-placed ap_container\">\n<p>You can also print the number of characters using \u2018<strong>-c\u2019<\/strong>\u00a0argument as shown below.<\/p>\n<pre># <strong>tail -c5 access.log<\/strong>\r\n\r\nymantec.com:443 - NONE\/- text\/html<\/pre>\n<h3>3. cat Command<\/h3>\n<p>The \u2018<strong>cat<\/strong>\u2018 command is most widely used, universal tool. It copies standard input to standard output. The command supports scrolling, if text file doesn\u2019t fit the current screen.<\/p>\n<p>The basic syntax of cat command is:<\/p>\n<pre># cat [options] [filenames] [-] [filenames]<\/pre>\n<p>The most frequent use of cat is to read the contents of files. All that is required to open a file for reading is to type cat followed by a space and the file name.<\/p>\n<pre># <strong>cat \/etc\/passwd<\/strong> \r\n\r\nroot:x:0:0:root:\/root:\/bin\/bash \r\ndaemon:x:1:1:daemon:\/usr\/sbin:\/bin\/sh \r\nbin:x:2:2:bin:\/bin:\/bin\/sh \r\nsys:x:3:3:sys:\/dev:\/bin\/sh \r\nsync:x:4:65534:sync:\/bin:\/bin\/sync \r\ngames:x:5:60:games:\/usr\/games:\/bin\/sh \r\nman:x:6:12:man:\/var\/cache\/man:\/bin\/sh \r\nlp:x:7:7:lp:\/var\/spool\/lpd:\/bin\/sh \r\n\u2026<\/pre>\n<p>The cat command also used to concatenate number of files together.<\/p>\n<pre># echo 'Hi Tecmint-Team' &gt; 1 \r\n# echo 'Keep connected' &gt; 2 \r\n# echo 'Share your thought' &gt; 3 \r\n# echo 'connect us tecmint.com@gmail.com' &gt; 4<\/pre>\n<pre># cat 1 2 3 4 &gt; 5<\/pre>\n<pre># cat 5 \r\n\r\nHi Tecmint-Team \r\nKeep connected \r\nShare your thought \r\nconnect us tecmint.com@gmail.com<\/pre>\n<p>It can be also used to create files as well. It is achieved by executing cat followed by the output redirection operator and the file name to be created.<\/p>\n<pre># cat &gt; tecmint.txt\r\n\r\nTecmint is the only website fully dedicated to Linux.<\/pre>\n<p>We can have custom end maker for \u2018cat\u2019 command. Here it is implemented.<\/p>\n<pre># <strong>cat &gt; test.txt &lt;&lt; end<\/strong> \r\n\r\nI am Avishek \r\nHere i am writing this post \r\nHope your are enjoying \r\nend<\/pre>\n<pre>#<strong> cat test.txt<\/strong> \r\n\r\nI am Avishek \r\nHere i am writing this post \r\nHope your are enjoying<\/pre>\n<p>Never underestimate the power of\u00a0 \u2018cat\u2019 command and can be useful for copying files.<\/p>\n<pre># cat avi.txt\r\n\r\nI am a Programmer by birth and Admin by profession<\/pre>\n<pre># cat avi.txt &gt; avi1.txt<\/pre>\n<pre># cat avi1.txt\r\n\r\nI am a Programmer by birth and Admin by profession<\/pre>\n<p>Now what\u2019s the opposite of cat? Yeah it\u2019s \u2018<strong>tac<\/strong>\u2018. \u2018<strong>tac<\/strong>\u2018 is a command under Linux. It is better to show an example of \u2018tac\u2019 than to talk anything about it.<\/p>\n<p>Create a text file with the names of all the month, such that one word appears on a line.<\/p>\n<pre># <strong>cat month<\/strong>\r\n\r\nJanuary\r\nFebruary\r\nMarch\r\nApril\r\nMay\r\nJune\r\nJuly\r\nAugust\r\nSeptember\r\nOctober\r\nNovember\r\nDecember<\/pre>\n<pre># <strong>tac month<\/strong>\r\n\r\nDecember\r\nNovember\r\nOctober\r\nSeptember\r\nAugust\r\nJuly\r\nJune\r\nMay\r\nApril\r\nMarch\r\nFebruary\r\nJanuary<\/pre>\n<p>For more examples of cat command usage, refer to the\u00a0<a href=\"https:\/\/www.tecmint.com\/13-basic-cat-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">13 cat Command Usage<\/a><\/p>\n<p>That\u2019s all for now.<\/p>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/view-contents-of-file-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are several commands and programs provided by\u00a0Linux\u00a0for viewing the contents of file. Working with files is one of the daunting task, most of the computer users be it newbie, regular user, advanced user, developer, admin, etc performs. Working with files effectively and efficiently is an art. Manage Files in Linux Today, in this article &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/22\/manage-files-effectively-using-head-tail-and-cat-commands-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Manage Files Effectively using head, tail and cat Commands in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12133","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12133"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12133\/revisions"}],"predecessor-version":[{"id":12134,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12133\/revisions\/12134"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}