{"id":48,"date":"2023-12-20T00:57:01","date_gmt":"2023-12-20T00:57:01","guid":{"rendered":"https:\/\/resellerjungle.com\/articles\/?p=48"},"modified":"2023-12-20T00:57:04","modified_gmt":"2023-12-20T00:57:04","slug":"create-file-using-echo-touch-tee-cat","status":"publish","type":"post","link":"https:\/\/resellerjungle.com\/articles\/create-file-using-echo-touch-tee-cat\/","title":{"rendered":"How To Create A File In Linux: Echo, Touch, Tee and Cat Commands"},"content":{"rendered":"\n<p>Files are one of the most important objects of any operating system and&nbsp;<a href=\"https:\/\/resellerjungle.com\/\" data-type=\"link\" data-id=\"https:\/\/resellerjungle.com\/\">Linux&nbsp;<\/a>is not an exception. Files provide a reliable way of storing data in a persistent manner.<\/p>\n\n\n\n<p>Linux uses plain text files to store important configurations. For example, the&nbsp;<strong>\/etc\/hosts<\/strong>&nbsp;file stores static table lookup for hostnames, the&nbsp;<strong>\/etc\/crontab<\/strong>&nbsp;file contains instructions for the&nbsp;cron daemon, and so on.<\/p>\n\n\n\n<p>Certainly, we can use graphical tools to create files. However, the same can be achieved using the command line interface as well. <\/p>\n\n\n\n<p><strong>Echo<\/strong>: The <code>echo<\/code> command in Linux is used to display text or output to the terminal or to redirect it to a file. It&#8217;s a fundamental command that&#8217;s often used in scripts or when interacting with the command line.<\/p>\n\n\n\n<p><strong>Touch<\/strong>: The <code>touch<\/code> command is used to create new empty files or update the timestamps (access and modification times) of existing files. It&#8217;s a versatile command that serves multiple purposes related to file handling.<\/p>\n\n\n\n<p><strong>Tee<\/strong>: In <a href=\"https:\/\/www.squarebrothers.com\/\" data-type=\"link\" data-id=\"https:\/\/www.squarebrothers.com\/\" target=\"_blank\" rel=\"noopener\">Linux<\/a>, the <code>tee<\/code> command is used to read the standard input and write it to both the standard output (usually the terminal) and one or more files simultaneously. This command is particularly useful when you want to view the output of a command on the screen while also saving it to a file for later use.<\/p>\n\n\n\n<p><strong>Cat<\/strong>: The <code>cat<\/code> command is a versatile utility used to concatenate and display the content of files. Despite its name, which stands for &#8220;concatenate,&#8221; <code>cat<\/code> is primarily used for displaying text files, creating new ones, combining copies of them, and redirecting output in the terminal.<\/p>\n\n\n\n<p>In this easy-to-follow guide, we will discuss various ways of creating a file in Linux.<\/p>\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=\"#1-create-an-empty-file-using-redirection-operator\">1. Create an Empty File Using &gt; Redirection Operator<\/a><\/li><li><a href=\"#2-create-file-and-write-content-using-redirection-operator\">2. Create File and Write Content Using &gt; Redirection Operator<\/a><\/li><li><a href=\"#3-create-files-using-touch-command\">3. Create Files Using touch Command<\/a><\/li><li><a href=\"#4-create-files-using-tee-command\">4. Create Files Using tee Command<\/a><\/li><li><a href=\"#5-create-a-file-using-cat-command\">5. Create a File Using cat Command<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-create-an-empty-file-using-redirection-operator\">1. Create an Empty File Using &gt; Redirection Operator<\/h2>\n\n\n\n<p>In Linux, the redirection operator&nbsp;<code>(&gt;)<\/code>&nbsp;is used to&nbsp;redirect the output of a command&nbsp;to a file instead of displaying it on the terminal.<\/p>\n\n\n\n<p>The same&nbsp;<code>(&gt;)<\/code>&nbsp;operator is also used to create a file if it doesn\u2019t exist already. However, it makes the file empty if it exists already. Hence one should be very careful while&nbsp;using the redirect operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &gt; resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"198\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1-1024x198.png\" alt=\"\" class=\"wp-image-51\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1-1024x198.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1-300x58.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1-768x149.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1-1536x297.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/1-1.png 1903w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-create-file-and-write-content-using-redirection-operator\">2. Create File and Write Content Using &gt; Redirection Operator<\/h2>\n\n\n\n<p>Sometimes, we want to create a non-empty file quickly. In such cases, you can use the output redirection operator&nbsp;<code>(&gt;)<\/code>&nbsp;to create a file and write content to it using the&nbsp;echo command&nbsp;as shown.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com is a Best Business Web Hosting\" &gt; resellerjungle.txt\nhead resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"63\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1-1024x63.png\" alt=\"\" class=\"wp-image-52\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1-1024x63.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1-300x18.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1-768x47.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1-1536x94.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/2-1.png 1902w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>It is important to note that in this example, we have used the&nbsp;<strong>echo<\/strong>&nbsp;command to create a file. However, we can redirect the output of the&nbsp;other Linux commands&nbsp;as well to create a file.<\/p>\n\n\n\n<p>Also, it is important to note that the&nbsp;<code>&gt;<\/code>&nbsp;redirection operator is used to overwrite the contents of an already existing file, which cause data loss if the operation is performed carelessly.<\/p>\n\n\n\n<p>In such a case, we can use the&nbsp;<code>&gt;&gt;<\/code>&nbsp;redirection operator, which is used to append the contents to the existing file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"resellerjungle.com is a #1  Business Web Hosting\" &gt;&gt; resellerjungle.txt\nhead resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"170\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1-1024x170.png\" alt=\"\" class=\"wp-image-53\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1-1024x170.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1-300x50.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1-768x127.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1-1536x254.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/3-1.png 1902w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the above output, we can see that the new line gets appended at the end of the file.<\/p>\n\n\n\n<p>It is worth noting that, just like the redirection operator, the append operator will also create an empty file if it doesn\u2019t exist already.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-create-files-using-touch-command\">3. Create Files Using touch Command<\/h2>\n\n\n\n<p>One more way of creating a file is using the&nbsp;touch command, which offers the safest way of creating an empty file because it never overwrites the existing file. Instead, it just updates the time stamp (access time and modification time) of the existing file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"117\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1024x117.png\" alt=\"\" class=\"wp-image-54\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1024x117.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-300x34.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-768x88.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4-1536x175.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/4.png 1903w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-create-files-using-tee-command\">4. Create Files Using tee Command<\/h2>\n\n\n\n<p>Similar to the redirection operator we can also use the&nbsp;tee command&nbsp;to create a file. The tee command writes the output of the command to the standard output stream as well as the file.<\/p>\n\n\n\n<p>For example, to create a file named \u201c<strong>resellerjungle.txt<\/strong>\u201c, use the&nbsp;<strong>tee command<\/strong>, which will be ready to accept input.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tee resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"61\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/5-1024x61.png\" alt=\"\" class=\"wp-image-55\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/5-1024x61.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/5-300x18.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/5-768x46.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/5-1536x92.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/5.png 1900w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you want to overwrite the content of a file using the tee command, you can use the following command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"Overwrite file using the tee command\" | tee resellerjungle.txt\nhead resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1902\" height=\"142\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/6-1024x76.png\" alt=\"\" class=\"wp-image-56\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/6-1024x76.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/6-300x22.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/6-768x57.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/6-1536x115.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/6.png 1902w\" sizes=\"auto, (max-width: 1902px) 100vw, 1902px\" \/><\/figure>\n\n\n\n<p>In this example, we can observe that the&nbsp;<strong>tee<\/strong>&nbsp;command overwrites the contents of the&nbsp;<strong>resellerjungle.txt<\/strong>&nbsp;file which was created and updated in earlier examples.<\/p>\n\n\n\n<p>To append the contents to the existing file, use the&nbsp;<code>-a<\/code>&nbsp;option of the&nbsp;<strong>tee<\/strong>&nbsp;command, which allows us to append the data at the end of the existing file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"Append data using the tee command\" | tee -a resellerjungle.txt\nhead resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"89\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/7-1024x89.png\" alt=\"\" class=\"wp-image-57\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/7-1024x89.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/7-300x26.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/7-768x67.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/7-1536x134.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/7.png 1900w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-create-a-file-using-cat-command\">5. Create a File Using cat Command<\/h2>\n\n\n\n<p>We can use the combination of the&nbsp;cat command&nbsp;and redirection operator to create a file. For example, the below command creates a new file if it doesn\u2019t exist already.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"76\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/8-1024x76.png\" alt=\"\" class=\"wp-image-58\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/8-1024x76.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/8-300x22.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/8-768x57.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/8-1536x114.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/8.png 1902w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here, the terminal waits infinitely for the user input. We have to press&nbsp;<code>Ctrl + D<\/code>&nbsp;after entering the required text to save the contents to the file:<\/p>\n\n\n\n<p>The main advantage of this approach is that it allows us to create a multi-line file using an interactive way. Just like the redirection operator, we have to use this method very carefully as it overwrites the existing file.<\/p>\n\n\n\n<p>In a similar way, we can use the combination of the&nbsp;<strong>cat command<\/strong>&nbsp;and append operator to append the contents at the end of the existing file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt;&gt; resellerjungle.txt<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"91\" src=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/9-1024x91.png\" alt=\"\" class=\"wp-image-59\" srcset=\"https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/9-1024x91.png 1024w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/9-300x27.png 300w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/9-768x68.png 768w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/9-1536x137.png 1536w, https:\/\/resellerjungle.com\/articles\/wp-content\/uploads\/2023\/12\/9.png 1901w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Just like in the previous example, we have to press&nbsp;<code>Ctrl + D<\/code>&nbsp;to append the contents to the file after entering the required text.<\/p>\n\n\n\n<p>In this guide, we discussed how to create a file using the Linux command line interface. Linux beginners can use one of the methods to create a file from the terminal.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We hope you&#8217;ve found this useful!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Files are one of the most important objects of any operating system and&nbsp;Linux&nbsp;is not an exception. Files provide a reliable&hellip;<\/p>\n","protected":false},"author":3,"featured_media":64,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[8,11,9],"class_list":["post-48","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-echo","tag-tee","tag-touch"],"_links":{"self":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/48","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":7,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":75,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/posts\/48\/revisions\/75"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media\/64"}],"wp:attachment":[{"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/resellerjungle.com\/articles\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}