{"id":188,"date":"2024-01-17T23:36:41","date_gmt":"2024-01-17T23:36:41","guid":{"rendered":"https:\/\/resellerjungle.com\/articles\/?p=188"},"modified":"2024-01-17T23:38:31","modified_gmt":"2024-01-17T23:38:31","slug":"cat-command-in-linux-with-examples","status":"publish","type":"post","link":"https:\/\/resellerjungle.com\/articles\/cat-command-in-linux-with-examples\/","title":{"rendered":"How to View the Content of File using cat command"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>cat<\/code> command in <a href=\"https:\/\/resellerjungle.com\/#\" data-type=\"link\" data-id=\"https:\/\/resellerjungle.com\/#\">Linux <\/a>is a command-line utility that is used to concatenate and display the content of files. The name &#8220;cat&#8221; stands for concatenate, but it is commonly used for other purposes as well. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The primary function of the <a href=\"https:\/\/resellerjungle.com\/articles\/create-file-using-echo-touch-tee-cat\/\" data-type=\"link\" data-id=\"https:\/\/resellerjungle.com\/articles\/create-file-using-echo-touch-tee-cat\/\"><code>cat<\/code> command<\/a> is to display the contents of one or more files to the terminal. It can also be used to concatenate the content of multiple files and create a new file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s worth noting that while <code>cat<\/code> is a simple and versatile command, there are other commands like <code>less<\/code>, <code>more<\/code>, and <code>head<\/code>\/<code>tail<\/code> that offer additional features such as pagination, scrolling, and viewing specific parts of a file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"311\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/cat-command-1-1024x311.jpg\" alt=\"\" class=\"wp-image-217\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/cat-command-1-1024x311.jpg 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/cat-command-1-300x91.jpg 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/cat-command-1-768x233.jpg 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/cat-command-1.jpg 1316w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#basic-syntax-of-cat-command\">Basic Syntax of cat Command<\/a><\/li><li><a href=\"#practical-examples-of-cat-command-in-linux\">Practical Examples of Cat Command in Linux<\/a><ul><li><a href=\"#1-how-to-view-the-content-of-a-single-file-in-linux\">1.How to View the Content of a Single File in Linux<\/a><\/li><li><a href=\"#2-how-to-view-the-content-of-multiple-files-in-linux\">2.How to View the Content of Multiple Files in Linux<\/a><\/li><li><a href=\"#3-how-to-view-contents-of-a-file-preceding-with-line-numbers-in-linux\">3.How to View Contents of a File preceding with Line Numbers in Linux<\/a><\/li><li><a href=\"#4-how-to-create-a-file-and-add-content-in-linux-using-cat-command\">4.How to Create a file and add content in Linux Using cat Command<\/a><\/li><li><a href=\"#5-how-to-copy-the-contents-of-one-file-to-another-file-in-linux\">5.How to Copy the Contents of One File to Another File in Linux<\/a><\/li><li><a href=\"#6-cat-command-can-suppress-repeated-empty-lines-in-output\">6.Cat command can suppress repeated empty lines in output<\/a><\/li><li><a href=\"#7-how-to-append-the-contents-of-one-file-to-the-end-of-another-file\">7.How to Append the Contents of One File to the End of Another File<\/a><\/li><li><a href=\"#8-how-to-display-content-in-reverse-order-using-tac-command-in-linux\">8.How to Display Content in Reverse Order Using tac Command in Linux<\/a><\/li><li><a href=\"#9-how-to-highlight-the-end-of-line-in-linux\">9.How to Highlight the End of Line in Linux<\/a><\/li><li><a href=\"#10-a-command-line-option-in-cat-command-in-linux\">10.-A Command Line Option in cat Command in Linux<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"basic-syntax-of-cat-command\">Basic Syntax of <code>cat<\/code> Command<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><br>The basic syntax of the \u2018cat\u2019 command is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &#91;OPTION] &#91;FILE]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">[OPTION] : represents various command-line options.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">[FILE] : the name of the file(s) to be processed. Let\u2019s explore some of the common uses of \u2018cat\u2019 along with examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"practical-examples-of-cat-command-in-linux\">Practical Examples of Cat Command in Linux<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-how-to-view-the-content-of-a-single-file-in-linux\">1.How to View the Content of a Single File in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The most basic use of \u2018cat\u2019 is to display the contents of a file on the terminal. This can be achieved by simply providing the filename as an argument:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file_name<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example: If our file_name = articles.txt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat articles.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"55\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/1-1024x55.jpg\" alt=\"\" class=\"wp-image-201\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/1-1024x55.jpg 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/1-300x16.jpg 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/1-768x41.jpg 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/1.jpg 1144w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Note: <code>ls<\/code> command is used to display all files and directories in the current location.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-how-to-view-the-content-of-multiple-files-in-linux\">2.How to View the Content of Multiple Files in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file_name1 file_name2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example: If we have two files , file1 and file2.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1 file2<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"73\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/2-1024x73.jpg\" alt=\"\" class=\"wp-image-203\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/2-1024x73.jpg 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/2-300x21.jpg 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/2-768x55.jpg 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/2.jpg 1293w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-how-to-view-contents-of-a-file-preceding-with-line-numbers-in-linux\">3.How to View Contents of a File preceding with Line Numbers in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Adding the -n option to cat introduces line numbers, making it convenient to identify and reference specific lines within the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -n file_name<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example: If our file_name is file2.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -n file2<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"50\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/3-1024x50.jpg\" alt=\"\" class=\"wp-image-204\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/3-1024x50.jpg 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/3-300x15.jpg 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/3-768x38.jpg 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/3.jpg 1343w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the cat command, used with the redirection (>), allows you to create a new file named \u201carticles\u201d and input content directly into it. The subsequent ls command lists all files in the current location.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-how-to-create-a-file-and-add-content-in-linux-using-cat-command\">4.How to Create a file and add content in Linux Using <code>cat<\/code> Command<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to create a new file or overwrite an existing file with new content, you can use \u2018cat\u2019 with the output redirection (<code>><\/code>):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat > newfile_name<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example: If we want to create a newfile_name = articles.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat >articles<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ls<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will allow you to type text directly into the terminal, and when you press Ctrl + D, the entered text will be saved to new_file.txt.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ls<\/code> command is used to display all files and directories in the current location.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-how-to-copy-the-contents-of-one-file-to-another-file-in-linux\">5.How to Copy the Contents of One File to Another File in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As the name suggests, \u2018cat\u2019 can concatenate multiple files into a single file. This example illustrates how to copy the entire content of \u201cfile1\u201d into \u201cfile2\u201d using the cat command along with redirection (>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1.txt file2.txt > merged_file.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"145\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/4-1024x145.jpg\" alt=\"\" class=\"wp-image-207\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/4-1024x145.jpg 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/4-300x42.jpg 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/4-768x109.jpg 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/4.jpg 1306w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This command combines the content of file1.txt and file2.txt into a new file named merged_file.txt.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-cat-command-can-suppress-repeated-empty-lines-in-output\">6.Cat command can suppress repeated empty lines in output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The -s option comes in handy when dealing with files containing repeated empty lines. It suppresses these repetitions, providing a cleaner output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -s file_name<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"74\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/5-1024x74.jpg\" alt=\"\" class=\"wp-image-209\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/5-1024x74.jpg 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/5-300x22.jpg 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/5-768x56.jpg 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2024\/01\/5.jpg 1256w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Will suppress repeated empty lines in output<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-how-to-append-the-contents-of-one-file-to-the-end-of-another-file\">7.How to Append the Contents of One File to the End of Another File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to add the content of one file to another, \u2018cat\u2019 can be used along with the append (>>) operator:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file_name1 >> file_name2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat file1 >> file2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will append the content of <code>file1<\/code> to the end of <code>file2<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-how-to-display-content-in-reverse-order-using-tac-command-in-linux\">8.How to Display Content in Reverse Order Using <code>tac<\/code> Command in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The \u2018tac\u2019 command is the reverse of \u2018cat\u2019 and is used to display the content of a file in reverse order. The syntax is simple:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tac file_name<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command will print the content of \u2018file2\u2019 in reverse order, displaying the last line first, followed by the second-to-last line, and so on.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tac file2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-how-to-highlight-the-end-of-line-in-linux\">9.How to Highlight the End of Line in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The \u2018-E\u2019 option in the \u2018cat\u2019 command is used to highlight the end of each line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -E \"filename\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will display the content of \u2018articles1\u2019 with a \u2018$\u2019 character at the end of each line, indicating the line\u2019s terminate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-a-command-line-option-in-cat-command-in-linux\">10.<code>-A<\/code> Command Line Option in <code>cat<\/code> Command in Linux<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The \u2018-A\u2019 option allows you to combine the effects of \u2018-v\u2019, \u2018-E\u2019, and \u2018-T\u2019 options. Instead of writing \u2018-vET\u2019 in the command, you can use \u2018-A\u2019:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat -A \"filename\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will display the content of \u2018filename\u2019 with non-printing characters visible, line endings highlighted, and tabs displayed as \u2018^I\u2019.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The cat command in Linux is a command-line utility that is used to concatenate and display the content of files.&hellip;<\/p>\n","protected":false},"author":4,"featured_media":214,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":15,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":218,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/188\/revisions\/218"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media\/214"}],"wp:attachment":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}