{"id":18819,"date":"2022-10-04T15:55:13","date_gmt":"2022-10-04T15:55:13","guid":{"rendered":"https:\/\/linuxways.net\/?p=18819"},"modified":"2022-11-04T02:51:21","modified_gmt":"2022-11-04T02:51:21","slug":"redirect-output-command-file-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/linux-commands\/redirect-output-command-file-linux\/","title":{"rendered":"How to Redirect the Output of a Command to a File in Linux"},"content":{"rendered":"<p>Whenever we run a command on a Linux terminal, its output is usually displayed on the terminal as soon as the said command finishes its execution. However, at times, we might want to save this output somewhere else to use it later on. Therefore, today, we will show you the different methods through which you can redirect the output of a command to a file in Linux Mint 21.<\/p>\n<h2><strong>Methods of Redirecting the Output of a Command to a File in Linux Mint 21<\/strong><\/h2>\n<p>To redirect the output of a command to a file on a Linux Mint 21 system, the two most commonly used methods are shared in the following:<\/p>\n<h2><strong>Method #1: Using the \u201c&gt;\u201d Operator<\/strong><\/h2>\n<p>You can use the following command to redirect the output of a command to a file in Linux Mint 21 without displaying the output on the terminal:<\/p>\n<pre>\r\n$ ls &gt; output.txt\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"26\" class=\"wp-image-18825\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-1.png 353w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-1-300x22.png 300w\" sizes=\"auto, (max-width: 353px) 100vw, 353px\" \/><\/strong><\/p>\n<p>This command redirects the output of the \u201cls\u201d command to a file named \u201coutput.txt\u201d without displaying anything on the terminal as shown in the following image. If the \u201coutput.txt\u201d file is already present in your system, this command simply overwrites its contents. Otherwise, it will first create this file and then save the output of the \u201cls\u201d command to this file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"43\" class=\"wp-image-18826\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-2.png 366w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-2-300x35.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/p>\n<p>Now, to confirm whether the output of our command has been redirected to this file or not, we use the following command:<\/p>\n<pre>\r\n$ cat output.txt\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"28\" class=\"wp-image-18827\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-3.png 352w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-3-300x24.png 300w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><\/strong><\/p>\n<p>This command displays the contents of this file which is in fact the output of the \u201cls\u201d command as shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"389\" height=\"267\" class=\"wp-image-18830\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-4.png 389w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-4-300x206.png 300w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><\/p>\n<h2><strong>Method #2: Using the \u201ctee\u201d Command<\/strong><\/h2>\n<p>If you wish to redirect the output of a command to a file in Linux Mint 21 while also displaying that output on the terminal, you have to use the following command:<\/p>\n<pre>\r\n$ ls | tee result.txt\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"391\" height=\"24\" class=\"wp-image-18832\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-5.png 391w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-5-300x18.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/strong><\/p>\n<p>The output of this command is shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"265\" class=\"wp-image-18836\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-6.png 446w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-6-300x178.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/p>\n<p>However, to confirm if the output is also redirected to a file or not, we run the following command:<\/p>\n<pre>\r\n$ ls\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"252\" height=\"21\" class=\"wp-image-18839\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-7.png\" \/><\/strong><\/p>\n<p>This command tells us if an output file is created within our current working directory or not from which we will be able to verify the redirection of the output of our command. You can see in the following image that an output file named \u201cresult.txt\u201dis successfully created.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"540\" height=\"103\" class=\"wp-image-18843\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-8.png 540w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18819-8-300x57.png 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/p>\n<p>You can get further confirmation by looking at the contents of the \u201cresult.txt\u201d file with the help of the \u201ccat\u201d command as we did in our first method.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>By picking any of the two methods that are explained previously, you can easily redirect the output of a command to a file in Linux. In this guide, we used the \u201cls\u201d command for the sake of demonstration. However, you can redirect the output of any command of your choice to a file in Linux Mint 21 by making use of these methods.<\/p>","protected":false},"excerpt":{"rendered":"<p>Article guide on the different methods to redirect the output of a command to a file in Linux Mint 21 \u2013 using the &gt; operator and the tee command.<\/p>","protected":false},"author":3,"featured_media":18878,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1049],"tags":[],"class_list":["post-18819","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-commands"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=18819"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18819\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18878"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=18819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=18819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=18819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}