{"id":4483,"date":"2022-12-10T09:18:09","date_gmt":"2022-12-10T09:18:09","guid":{"rendered":"https:\/\/arzhost.com\/blogs\/?p=4483"},"modified":"2023-10-19T18:28:29","modified_gmt":"2023-10-19T18:28:29","slug":"how-to-use-linux-cat-command","status":"publish","type":"post","link":"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/","title":{"rendered":"How to Use Linux Cat Command? Use the following syntax"},"content":{"rendered":"<p>If you have ever used Linux, you have undoubtedly come across some code that makes How to Use Linux <a href=\"https:\/\/www.ibm.com\/docs\/en\/ssw_aix_71\/c_commands\/cat.html\" target=\"_blank\" rel=\"noopener\">Cat Command<\/a>? Concatenate is abbreviated as CAT. Without opening the file for editing, this command shows the contents of one or more files.<\/p>\n<p><u>Learn how to use the cat command in Linux in this article.<\/u><\/p>\n<ol>\n<li>Linux-running computer<\/li>\n<li>access to a command line or terminal window<\/li>\n<\/ol>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#Command_Syntax_for_Cat\" >Command Syntax for Cat<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#Examples_of_How_to_Use_Linux_Cat_Command\" >Examples of How to Use Linux Cat Command?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#1_Making_a_New_File\" >1: Making a New File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#2_Show_the_Contents_of_One_File\" >2: Show the Contents of One File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#3_Show_Contents_of_Several_Files\" >3: Show Contents of Several Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#4_Redirect_a_Single_Files_Contents\" >4: Redirect a Single File&#8217;s Contents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#5_Redirect_Multiple_Files_Contents\" >5: Redirect Multiple Files&#8217; Contents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#6_Show_the_Contents_in_the_Opposite_Direction\" >6: Show the Contents in the Opposite Direction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#7_Attach_the_contents_of_a_file_to_another_file\" >7: Attach the contents of a file to another file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#8_Add_Text_to_a_Current_File\" >8: Add Text to a Current File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#9_Combinational_Operations\" >9: Combinational Operations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#10_More_and_Fewer_Choices_Control_Big_Files\" >10: More and Fewer Choices (Control Big Files)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#11_Display_Line_Numbers\" >11: Display Line Numbers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#12_Display_the_Lines_End\" >12: Display the Line&#8217;s End<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#13_Display_TAB_Separated_Lines\" >13: Display TAB Separated Lines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#14_Eliminate_White_Space\" >14: Eliminate White Space<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/arzhost.com\/blogs\/how-to-use-linux-cat-command\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Command_Syntax_for_Cat\"><\/span><strong>Command Syntax for Cat<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><u>How to Use Linux Cat Command? Use the following syntax to use the cat command:<\/u><\/p>\n<ul>\n<li><code>filename cat [options] (s)<\/code><\/li>\n<\/ul>\n<p>[options] &#8211; This allows you to give the cat command additional instructions. Use the -n option, for instance, to show a file&#8217;s contents with each line&#8217;s number displayed:<\/p>\n<ul>\n<li><code>filename cat -n<\/code><\/li>\n<\/ul>\n<p>Enter the name of the file (or files) you want to display in the filename field. If you use more than one filename, each file will be displayed.<\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/-K7q_whImpE\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Examples_of_How_to_Use_Linux_Cat_Command\"><\/span><strong>Examples of How to Use Linux Cat Command?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This article lists 15 cat commands along with usage examples. Create a few test files and run the cat commands given below to test the commands.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Making_a_New_File\"><\/span><strong><u>1: Making a New File<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Using the cat command, you can make new files and add content to them. Make the example files test1.txt and test2.txt so that you can use them to test the other instructions.<\/p>\n<p><u>1: Launch the terminal and make the first file:<\/u><\/p>\n<ul>\n<li><code>txt, cat<\/code><\/li>\n<\/ul>\n<p><u>2: The desired text can be added by moving the cursor to a new line. Write a short sentence like this:<\/u><\/p>\n<ul>\n<li><code>First test file, this one.<\/code><\/li>\n<\/ul>\n<ol start=\"3\">\n<li><u> To exit the prompt and write the changes to the file, hold the Ctrl key and press d.<\/u><\/li>\n<\/ol>\n<ul>\n<li><code>Use the cat command to generate a new file.<\/code><\/li>\n<\/ul>\n<p><u>4: Carry out step 4 again to produce test2.txt. Run:<\/u><\/p>\n<ul>\n<li><code>txt, cat<\/code><\/li>\n<\/ul>\n<p><u>5: Type:<\/u><\/p>\n<ul>\n<li><code>Test file #2 is this one.<\/code><\/li>\n<\/ul>\n<p><u>6: Hit Ctrl+d.<\/u><\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Show_the_Contents_of_One_File\"><\/span><strong><u>2: Show the Contents of One File<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>Run the following command to use cat to display the contents of test1.txt:<\/u><\/p>\n<ul>\n<li><code>feline test1.txt<\/code><\/li>\n<\/ul>\n<p>The content appears in the output as shown in the image below.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Show_Contents_of_Several_Files\"><\/span><strong><u>3: Show Contents of Several Files<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>Run the following command to see what both files contain:<\/u><\/p>\n<ul>\n<li><code>txt, test1.txt, cat<\/code><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"4_Redirect_a_Single_Files_Contents\"><\/span><strong><u>4: Redirect a Single File&#8217;s Contents<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A cat can store the contents of a file in a file as opposed to showing them on the screen.<\/p>\n<ul>\n<li><code>txt &gt; test3.txt in a cat<\/code><\/li>\n<\/ul>\n<p><u>The destination filename will be created if it doesn&#8217;t already exist. You should see the contents of test1.txt if you run cat on test3.txt:<\/u><\/p>\n<ul>\n<li><code>fel test3.txt<\/code><\/li>\n<\/ul>\n<p><u>The result shows:<\/u><\/p>\n<ul>\n<li>If a file is exported that already exists, the contents will be overwritten:<\/li>\n<li><code>txt &gt; test3.txt in a cat<\/code><\/li>\n<li><code>fel test3.txt<\/code><\/li>\n<\/ul>\n<p>The following text is now present in the test3.txt file:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Redirect_Multiple_Files_Contents\"><\/span><strong><u>5: Redirect Multiple Files&#8217; Contents<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>You can combine the contents of several files into one:<\/u><\/p>\n<ul>\n<li><code>if (cat test1.txt, test2, test3)<\/code><\/li>\n<\/ul>\n<p><u>Show test3.txt&#8217;s content by using:<\/u><\/p>\n<ul>\n<li><code>fel test3.txt<\/code><\/li>\n<\/ul>\n<p>The screenshot below shows how the output displays the contents of both files.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Show_the_Contents_in_the_Opposite_Direction\"><\/span><strong><u>6: Show the Contents in the Opposite Direction<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"color: #000000;\"><strong><em><u>How to Use Linux Cat Command?<\/u><\/em><\/strong><\/span><u> Reverse order can be displayed for a file&#8217;s content using the cat command (by lines). Use tac (cat in reverse) to accomplish this:<\/u><\/p>\n<ul>\n<li><code>txt tac<\/code><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"7_Attach_the_contents_of_a_file_to_another_file\"><\/span><strong><u>7: Attach the contents of a file to another file<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>The cat command has the ability to append a file&#8217;s contents to the end of another file. Use a double &gt;&gt; symbol rather than a single &gt; sign:<\/u><\/p>\n<ul>\n<li><code>txt after cat test1.txt<\/code><\/li>\n<\/ul>\n<p><u>the test3 file by executing:<\/u><\/p>\n<ul>\n<li><code>fel test3.txt<\/code><\/li>\n<\/ul>\n<p>Test 3&#8217;s material should appear first, followed by Test 1&#8217;s content.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_Add_Text_to_a_Current_File\"><\/span><strong><u>8: Add Text to a Current File<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>A similar command can be used to add text to an existing file:<\/u><\/p>\n<ul>\n<li><code>txt &gt;&gt; cat<\/code><\/li>\n<\/ul>\n<p><u>The new line was added to the file:<\/u><\/p>\n<ul>\n<li><code>This is the second line in test file #1.<\/code><\/li>\n<li><code>d while holding down Ctrl.<\/code><\/li>\n<\/ul>\n<p><u>Verify the test1.txt file&#8217;s content:<\/u><\/p>\n<ul>\n<li><code>feline test1.txt<\/code><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"9_Combinational_Operations\"><\/span><strong><u>9: Combinational Operations<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>The cat command&#8217;s features can be mixed. To merge the output of two files and save<\/u> <u>the result in a new file, for instance:<\/u><\/p>\n<ul>\n<li><code>cat test1.txt, test2.txt, and finally test4.txt<\/code><\/li>\n<li><code>feline test4.txt<\/code><\/li>\n<\/ul>\n<p><u>You can also add additional files to the end of an existing file as an alternative:<\/u><\/p>\n<ul>\n<li><code>cat test2.txt test1.txt &gt;&gt; test4.txt<\/code><\/li>\n<li><code>feline test4.txt<\/code><\/li>\n<\/ul>\n<p>It should be noted that the files are added to the destination file in the order indicated.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"10_More_and_Fewer_Choices_Control_Big_Files\"><\/span><strong><u>10: More and Fewer Choices (Control Big Files)<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>If you use cat on a particularly large file, you&#8217;ll get a long, difficult-to-read string of data<\/u> <u>as a result. In order to divide it into pages, use | more:<\/u><\/p>\n<ul>\n<li><code>cat test4.txt | more<\/code><\/li>\n<\/ul>\n<p>This only shows the first page of the file. The next page will scroll when you touch a key.<\/p>\n<p><u>Use | less if you&#8217;d want to be able to move the display forward and backward.<\/u><\/p>\n<ul>\n<li><code>c a t e r t. t l e s<\/code><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"11_Display_Line_Numbers\"><\/span><strong><u>11: Display Line Numbers<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><em>Line numbers in the output may be valuable to you, particularly for big files. Add the -n option to the cat command in order to enable line numbering:<\/em><\/p>\n<ul>\n<li><code>txt, cat -n<\/code><\/li>\n<\/ul>\n<p><u>The output should resemble the illustration.<\/u><\/p>\n<h3><span class=\"ez-toc-section\" id=\"12_Display_the_Lines_End\"><\/span><strong><u>12: Display the Line&#8217;s End<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>With the command $, you can tell the cat to bold the end of each line and the spaces in between them.<\/p>\n<p><u>Use the command to do so:<\/u><\/p>\n<ul>\n<li><code>txt, cat -e<\/code><\/li>\n<\/ul>\n<p>The output displays one $ at the end of the sample file test1.txt because there is only one line in it.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"13_Display_TAB_Separated_Lines\"><\/span><strong><u>13: Display TAB Separated Lines <\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>With the cat command, you may choose to see both the text between the tabs and the contents of the file.<\/p>\n<p><u>In order to display tab-separated lines for a test run:<\/u><\/p>\n<ul>\n<li><code>txt, cat -t<\/code><\/li>\n<\/ul>\n<p>I stand in for the tab space within the text.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"14_Eliminate_White_Space\"><\/span><strong><u>14: Eliminate White Space<\/u><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><u>When using the cat&#8217;s -s option, blank lines are not included in the output:<\/u><\/p>\n<ul>\n<li><code>txt, cat -t<\/code><\/li>\n<li><code>List every CAT command<\/code><\/li>\n<\/ul>\n<p><u>Use the \u2014help command if you have difficulties remembering the options:<\/u><\/p>\n<ul>\n<li><code>aid for cats<\/code><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now that you know how to use the cat command in Linux, you should be able to utilize it effectively.<\/p>\n<p>Interested in learning How to Use Linux Cat Command? See our list of the Top Linux Commands Every User Should Know.<\/p>\n<p>Read more:<\/p>\n<ul>\n<li><a href=\"https:\/\/arzhost.com\/blogs\/linux-kernel-5-7-released\/\">Linux Kernel 5.7 Released with New Features and Improvements<\/a><\/li>\n<li><a href=\"https:\/\/arzhost.com\/blogs\/show-hidden-files-in-linux-command\/\">How to Show Hidden Files in Linux Command ?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you have ever used Linux, you have undoubtedly come across some code that makes How to Use Linux Cat Command? Concatenate is abbreviated as CAT. Without opening the file for editing, this command shows the contents of one or more files. Learn how to use the cat command in Linux in this article. Linux-running [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6196,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"table_tags":[],"class_list":["post-4483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/4483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/comments?post=4483"}],"version-history":[{"count":4,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/4483\/revisions"}],"predecessor-version":[{"id":8796,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/4483\/revisions\/8796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media\/6196"}],"wp:attachment":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media?parent=4483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/categories?post=4483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/tags?post=4483"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/table_tags?post=4483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}