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.
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.
Check Filesystem Disk Space
-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
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
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
--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.