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
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
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
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.
- 5 Interesting Command Line Tips and Tricks in Linux
- 10 Useful Linux Command Line Tricks for Newbies
- 10 Interesting Linux Command Line Tricks and Tips Worth Knowing
- How to Run or Repeat a Linux Command Every X Seconds Forever
- 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.
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.
@Rick
We actually have a complete starters guide about the script command: https://www.tecmint.com/record-and-replay-linux-terminal-session-commands-using-script/
Thanks for mentioning it.
What about:
$ df -h > df.log
@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.
So ‘df -h; df -h > df.log’
The idea to the subject of the article, was to run a command, ONCE, outputting the results to the screen AND a disk file, at the same time! Not by running the command twice!
@Rick
Yap, well explained! This is the actual purpose of the article. Thanks for the useful feedback.
@Greg
I think this serves the same purpose also; you can choose which of the two works best for you.