How to Save Command Output to a File in Linux

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: Learn The Basics of How Linux I/O (Input/Output) Redirection Works

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.

Viewing Output On Screen and also Writing to a File

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 df command; it also helps you determine the file system type on a partition.

$ $df
Check Filesystem Disk Space
Check Filesystem Disk Space

With the -h flag, you can show the file system disk space statistics in a “human readable” format (displays statistics details in bytes, mega bytes and gigabyte).

$ df -h
Disk Space in Human Readable Format
Disk Space in Human Readable Format

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.

$ df -h | tee df.log
$ cat df.log
Linux Command Output to File
Linux Command Output to File

Here, the magic is done by the tee command, it reads from standard input and writes to standard output as well as files.

If a file(s) already exists, you can append it using the -a or --append option like this.

$ df -h | tee -a df.log 

Note: You can also use pydf an alternative “df” command to check disk usage in different colors.

For more information, read through the df and tee man pages.

$ man df
$ man tee

You may also like to read similar articles.

  1. 5 Interesting Command Line Tips and Tricks in Linux
  2. 10 Useful Linux Command Line Tricks for Newbies
  3. 10 Interesting Linux Command Line Tricks and Tips Worth Knowing
  4. How to Run or Repeat a Linux Command Every X Seconds Forever
  5. Set Date and Time for Each Command You Execute in Bash History

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.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

8 thoughts on “How to Save Command Output to a File in Linux”

  1. You also could use the script command to capture both user input and program output to a user provided filename, or “typescript” by default.

    “$ man script” for more information.


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.