Redirect output to both the console and a file using the TEE command in Linux

Home > Search > Linux commands
  by

The tee command can be used to both redirect text to a file and to also display the text in the Terminal. In this example, the text Hello World is both redirected to example.file and is also displayed in the Terminal.

[user1@server1 ]# echo "Hello World" | tee example.file
Hello World

 

The cat command can be used to ensure that the text Hello World was redirected to example.file.

[user1@server1 ]# cat example.file
Hello World

 


Append

By default, the tee command without any options will overwrite the target file. The -a or --append option can be used to append text to the target file. In this example, the text How are you today is appended to example.file.

[user1@server1 ]# echo "How are you today" | tee -a example.file
How are you today

 

The cat command can be used to ensure that the text How are you today was appended to example.file.

[user1@server1 ]# cat example.file
Hello World
How are you today


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments