The Linux tee command can be used to redirect the standard output of one command to one or more files and or commands. In this post I will be going over several examples of the Linux tee command combined with both pupping and redirection of standard out pout two many files at once in one command line.
In order to use The Linux tee command first I need some standard output. So for the sake of an example say I just want to create a file called disk.txt from the output of the df command, but I also want to standard output of the df command to continue along to the console on top of that. For that I just need to pipe the output of the df command to the tee command, and then just give one file name to which I want the output of df saved.
This will result in the standard output of df saved in the disk.txt file, and on top of that the output will continue on to the console as usual. So it results in a tee of sorts, there the standard output of df is going from df to the file disk.txt, and along to the output of the console on top of that.
By default the tee command will overwrite a file each time rather than appending data to a file each time it is called. So if for some reason I want to append data to a file rather that write over it each time there is an append option for the tee command itself, but there is also of course redirection appending of standard output also.
However it might be best to just learn more about how redirection works in Linux. The nice thing about redirection is that it will work with all commands even if they do not support an option of appending to a file rather than overwriting.
If I want to write two more than one file then I just need to give more file names.
However if I want to run the output from df threw some commands first for each file that can be done, it just means I need to do a little redirection.
The Linux tee command can be used in combination with piping and redirection to create two or more files from the standard output of one command. In addition I can not just redirect to files but also run the results threw some additional commands like grep.
So say I want to take the output of the free command and use grep to take just the line on memory and write that to one file, and then take the same standard output and again use grep to write a file with just the output from the swap line to another file, and finally just write the full standard output to one last file. This can be done with a combination of piping, and redirection.
Contents of mem.txt:
Contents of swap.txt:
Contents of free.txt:
To the Linux tee command is one of several commands that come to mind when it comes to learning a thing or two about piping, and redirection of standard output from a command. One post of mine that is worth checking out is one the Linux xargs command that has to do with using standard output from a command as arguments for a command rather than piping it to the standard input of a command. Another related post that is worth checking out would be my post on linux redirection of standard output.