Linux: How to redirect command output to a file and also view it on screen side by side ?

Here we will see a trick to save the command output to one or more files alongwith viewing the output on screen.

This commandis very ueful for logging and at the same time viewing the result on screen.

We will look at “tee” command usage and its utility here.

I. Save command/script output to a file.

[root@ngelinux ]# echo "Hello ! See the magic" | tee file10.txt
Hello ! See the magic
[root@ngelinux ]# cat file10.txt 
Hello ! See the magic
[root@ngelinux ]# 

[root@ngelinux ]# ./script1.sh | tee output.txt
Welcome to NGELinux
-------------------
[root@ngelinux ]# cat output.txt 
Welcome to NGELinux
-------------------
[root@ngelinux ]# 

II. Append the output in existing file.

[root@ngelinux ]# ./script1.sh | tee output.txt
Welcome to NGELinux
-------------------
[root@ngelinux ]# cat output.txt 
Welcome to NGELinux
-------------------
[root@ngelinux ]# 
[root@ngelinux ]# ./script1.sh | tee -a output.txt
Welcome to NGELinux
-------------------
[root@ngelinux ]# cat output.txt 
Welcome to NGELinux
-------------------
Welcome to NGELinux
-------------------

III. Redirecting output to more than one file.
We can mention any number of files here.

[root@ngelinux ]# ./script1.sh | tee -a output.txt output2.txt
Welcome to NGELinux
-------------------
[root@ngelinux ]# cat output2.txt 
Welcome to NGELinux
-------------------
[root@ngelinux ]# cat output.txt 
Welcome to NGELinux
-------------------
Welcome to NGELinux
-------------------
Welcome to NGELinux
-------------------
[root@ngelinux ]# 

Leave a Reply

Your email address will not be published.