{"id":1141,"date":"2018-08-06T08:21:33","date_gmt":"2018-08-06T02:51:33","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1141"},"modified":"2019-04-27T07:44:35","modified_gmt":"2019-04-27T02:14:35","slug":"basic-cat-commands-in-linux-with-examples","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/basic-cat-commands-in-linux-with-examples\/","title":{"rendered":"Basic Cat Commands in Linux with Examples"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p>The cat (short for \u201c<strong>concatenate<\/strong>\u201c) commands is one of the most frequently used command in Linux\/Unix like operating systems.<\/p>\n<p>cat command allows us to create single or multiple files, view contain of file.<\/p>\n<p>We will discuss about cat commands in detail.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1. How to view contents of a file using cat<\/strong><\/p>\n<p>The cat commands allow you to view content of a file on the standard output (stdout).<\/p>\n<p>Check this with the below command.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat [filename]<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true\">[root@vps~]# cat file1.txt\nTesting file 1\n\n[root@vps~]# \n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. How to display multiple files using cat<\/strong><\/p>\n<p>With cat commands we can also check contents of multiple files in one go.<\/p>\n<p>Check this with the below command.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat [filename] [filename] ...<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat file1.txt file2.txt\nTesting file 1\n\nTesting file 2\n\n[root@vps~]#<\/pre>\n<p><strong>3. How to display contents of file with line numbers<\/strong><\/p>\n<p>Also you can display contents of a file preceding with line numbers.<\/p>\n<p>This can be done by using the tool\u2019s -n command line option.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat -n [filename]<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat -n file1.txt\n\n1 Testing file 1\n2\n\n[root@vps~]#<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>4. How to create file using cat command<\/strong><\/p>\n<p>If you want, you can also create a new file, and fill it with information using a single cat command.<\/p>\n<p>Check the below command to know how to execute this.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat &gt; [name-of-new-file]<\/pre>\n<p>When run, the command requires you to enter the information on the terminal.<\/p>\n<p>Once you&#8217;re done, just press CTRL+d.<\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat &gt; file3.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true\">[root@vps~]# cat &gt; file3.txt\nHi\nTesting file 3\n\n[root@vps~]#<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>5. How to copy the contents of one file to another file<\/strong><\/p>\n<p>You can also use cat to copy the contents of one file to another file.<\/p>\n<p>This can be done in the following way.<\/p>\n<p><strong>cat [filename-whose-contents-is-to-be-copied] &gt; [destination-filename]<\/strong><\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat file1.txt &gt; file2.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat file1.txt &gt; file2.txt\n[root@vps~]# cat file2.txt\nTesting file 1\n\n[root@vps~]#<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>6. How to make cat highlight line-ends<\/strong><\/p>\n<p>If you want, you can also make cat highlight the end of lines.<\/p>\n<p>The tool does this by displaying $ at the end of each line.<\/p>\n<p>This feature can be accessed using the tool\u2019s -E command line option.<\/p>\n<p><strong>cat -E [filename]<\/strong><\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat -E file1.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat -E file1.txt\n\nTesting file 1 $\n\n[root@vps~]#<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>7. How to make cat suppress repeated empty lines<\/strong><\/p>\n<p>If you want, you can also make the cat command suppress repeated empty lines in output.<\/p>\n<p>This can be done by using the -s command line option.<\/p>\n<p><strong>cat -s [filename]<\/strong><\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat -s file1.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat file1.txt\nTesting file 1\n\nTesting file 2\n\nTesting file 3\n[root@vps~]# cat -s file1.txt\nTesting file 1\n\n\nTesting file 2\n\n\nTesting file 3<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>8. How to make cat display tab characters as ^I<\/strong><\/p>\n<p>The cat command also allows you to display tab characters as ^I.<\/p>\n<p>This can be done by using the tool\u2019s -T command line option.<\/p>\n<p><strong>cat -T [filename]<\/strong><\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat -T file3.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat -T file3.txt\nTesting ^I\n^File\n^I^Created\n\n[root@vps~]# cat file3.txt\nTesting\nFile\nCreated\n[root@vps~]# cat -T file3.txt\nTesting ^I\n^File\n^I^Created<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>9. View the Contents in Reverse Order<\/strong><\/p>\n<p>If you want, you can also make the cat command display content in reverse order.<\/p>\n<p>This can be done using the tac command line option.<\/p>\n<p><strong>tac [filename]<\/strong><\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">tac file4.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat file4.txt\nTesting\nFile\nCreated\n\n[root@vps~]# tac file4.txt\nCreated\nFile\nTesting<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>10. The -A option<\/strong><\/p>\n<p>In case, you need to use the -E and -T option together, then instead of writing -ET in the command, you can just use the -A command line option.<\/p>\n<p><strong>cat -A [filename]<\/strong><\/p>\n<pre class=\"theme:dark-terminal lang:default decode:true \">cat -A file4.txt<\/pre>\n<pre class=\"theme:dark-terminal lang:default decode:true \">[root@vps~]# cat file4.txt\nTesting\nFile\nCreated\n\n[root@vps~]# cat -A file4.txt\nTesting$\n^File$\nCreated\n$<\/pre>\n<p>&nbsp;<\/p>\n<p>We hope you\u2019ve found this useful!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description The cat (short for \u201cconcatenate\u201c) commands is one of the most frequently used command in Linux\/Unix like operating systems. cat command allows us to create single or multiple files, view contain of file. We will discuss about cat commands in detail. &nbsp; 1. How to view contents of a file using cat The cat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1289,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1141"}],"version-history":[{"count":1,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1141\/revisions"}],"predecessor-version":[{"id":1143,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1141\/revisions\/1143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1289"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}