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.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

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.

    Reply
    • @lukas

      Here, the intention is to run a command, view its output on the command line as well as save it to a file. What you have mentioned is simply output redirection.

      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.