Press "Enter" to skip to content

How to Display and Capture Command Output in Linux/Unix Shell

When we executes a command in Linux/Unix shell then it displays the output on screen and if you want to capture the output also in a log file (text file) then you can use the tee command. The tee command generates the shell command output on screen and as well as in a text file.
The below example is given to run the ls command to list all files in a directory on the screen and also saves the output to a file ls123.log, which can be viewed later:
$ ls -altr /myfolder/files | tee /home/files/log/ls123.log
You can see in above command that pipe | is used to separate and add to tee command. Before pipe is a your command and after pipe is to log the output of a command in a log file.
Now you can move to directory /home/files/log/ to see the contents of a ls123.log file, generated from the command.
Linux/Unix Command Examples

Vinish Kapoor

FoxInfotech.in is created, written, and maintained by Vinish Kapoor. It is built on WordPress, and hosted by Bluehost. Connect with Vinish on Facebook, Twitter, and GitHub.

You may also like: