{"id":12599,"date":"2019-03-28T03:22:50","date_gmt":"2019-03-28T03:22:50","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12599"},"modified":"2019-03-28T03:22:50","modified_gmt":"2019-03-28T03:22:50","slug":"how-to-save-command-output-to-a-file-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-save-command-output-to-a-file-in-linux\/","title":{"rendered":"How to Save Command Output to a File in Linux"},"content":{"rendered":"<p>There are many things you can do with the output of a command in Linux. You can assign the output of a command to a variable, send it to another command\/program for processing through a pipe or redirect it to a file for further analysis.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-io-input-output-redirection-operators\/\" target=\"_blank\" rel=\"noopener\">Learn The Basics of How Linux I\/O (Input\/Output) Redirection Works<\/a><\/p>\n<p>In this short article, I will show you a simple but useful command-line trick: how to view output of a command on the screen and also write to a file in Linux.<\/p>\n<h3>Viewing Output On Screen and also Writing to a File<\/h3>\n<p>Assuming you want to get a full summary of available and used disk space of a file system on a Linux system, you can employ the\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-check-disk-space-in-linux\/\" target=\"_blank\" rel=\"noopener\">df command<\/a>; it also helps you determine the file system type on a partition.<\/p>\n<pre>$ $df\r\n<\/pre>\n<div id=\"attachment_26587\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/08\/Check-Filesystem-Disk-Space.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-26587\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/08\/Check-Filesystem-Disk-Space.png\" alt=\"Check Filesystem Disk Space\" width=\"682\" height=\"230\" aria-describedby=\"caption-attachment-26587\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-26587\" class=\"wp-caption-text\">Check Filesystem Disk Space<\/p>\n<\/div>\n<p>With the\u00a0<code>-h<\/code>\u00a0flag, you can show the file system disk space statistics in a \u201chuman readable\u201d format (displays statistics details in bytes, mega bytes and gigabyte).<\/p>\n<pre>$ df -h\r\n<\/pre>\n<div id=\"attachment_26588\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/08\/Disk-Space-in-Human-Readable-Format.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-26588\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/08\/Disk-Space-in-Human-Readable-Format.png\" alt=\"Disk Space in Human Readable Format\" width=\"682\" height=\"230\" aria-describedby=\"caption-attachment-26588\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-26588\" class=\"wp-caption-text\">Disk Space in Human Readable Format<\/p>\n<p>Now to display the above information on the screen and also write it to a file, say for later analysis and\/or send to a system administrator via email, run the command below.<\/p>\n<pre>$ df -h | tee df.log\r\n$ cat df.log\r\n<\/pre>\n<div id=\"attachment_26589\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/08\/Linux-Command-Output-to-File.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-26589\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/08\/Linux-Command-Output-to-File.png\" alt=\"Linux Command Output to File\" width=\"682\" height=\"439\" aria-describedby=\"caption-attachment-26589\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-26589\" class=\"wp-caption-text\">Linux Command Output to File<\/p>\n<\/div>\n<p>Here, the magic is done by the\u00a0<strong>tee<\/strong>\u00a0command, it reads from standard input and writes to standard output as well as files.<\/p>\n<p>If a file(s) already exists, you can append it using the\u00a0<code>-a<\/code>\u00a0or\u00a0<code>--append<\/code>\u00a0option like this.<\/p>\n<pre>$ df -h | tee -a df.log \r\n<\/pre>\n<p><strong>Note<\/strong>: You can also use\u00a0<a href=\"https:\/\/www.tecmint.com\/pyd-command-to-check-disk-usage\/\" target=\"_blank\" rel=\"noopener\">pydf an alternative \u201cdf\u201d command<\/a>\u00a0to check disk usage in different colors.<\/p>\n<p>For more information, read through the\u00a0<strong>df<\/strong>\u00a0and\u00a0<strong>tee<\/strong>\u00a0man pages.<\/p>\n<pre>$ man df\r\n$ man tee\r\n<\/pre>\n<p>You may also like to read similar articles.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/5-linux-command-line-tricks\/\" target=\"_blank\" rel=\"noopener\">5 Interesting Command Line Tips and Tricks in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/10-useful-linux-command-line-tricks-for-newbies\/\" target=\"_blank\" rel=\"noopener\">10 Useful Linux Command Line Tricks for Newbies<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/linux-command-line-tricks-and-tips-worth-knowing\/\" target=\"_blank\" rel=\"noopener\">10 Interesting Linux Command Line Tricks and Tips Worth Knowing<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/run-repeat-linux-command-every-x-seconds\/\" target=\"_blank\" rel=\"noopener\">How to Run or Repeat a Linux Command Every X Seconds Forever<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/display-linux-command-history-with-date-and-time\/\" target=\"_blank\" rel=\"noopener\">Set Date and Time for Each Command You Execute in Bash History<\/a><\/li>\n<\/ol>\n<p>In this short article, I showed you how to view output of a command on the screen and also write to a file in Linux. If you have any questions or additional ideas to share, do that via the comment section below.<\/p>\n<p><a style=\"font-size: 1rem;\" href=\"https:\/\/www.tecmint.com\/save-command-output-to-a-file-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are many things you can do with the output of a command in Linux. You can assign the output of a command to a variable, send it to another command\/program for processing through a pipe or redirect it to a file for further analysis. Suggested Read:\u00a0Learn The Basics of How Linux I\/O (Input\/Output) Redirection &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-save-command-output-to-a-file-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Save Command Output to a File in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12599","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12599"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12599\/revisions"}],"predecessor-version":[{"id":12600,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12599\/revisions\/12600"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}