Splitting Streams with tee

When you redirect the output to a file, it no longer displays on the screen. To see the output on the screen and send it to a file, you could use the cat or less commands to view the file, but you can also use the tee command. The tee command gets its name from plumbing—a T-shaped pipe fitting that splits the water off in two directions. The tee command takes input and splits the stream to the screen and to a file.

Execute the history command and pipe its data to the tee command:

 $ ​​history​​ ​​|​​ ​​tee​​ ​​commands.txt

Instead of redirecting the output to a file, you’re piping it to tee, which receives the data as standard input. This time, you see the results on the screen. The commands.txt file will also have the latest version of your history inside.

The tee command overwrites the target file each time. But if you use the -a flag, you can append to the file instead:

 $ ​​date​​ ​​|​​ ​​tee​​ ​​-a​​ ​​commands.txt

The date displays on the screen and is appended to the end of the file:

 $ ​​tail​​ ​​-n​​ ​​2​​ ​​commands.txt
  191 history | tee commands.txt
 Sat Mar 2 03:53:24 UTC 2019

The tee command has another benefit—you can use it to write to places where normal redirection won’t let you. Remember in Creating Files on the Rest of the Filesystem, you tried to create a file with content in the /var folder but it didn’t work? Time to review that.

Try to save the history to a file in the /var directory. The command fails because your user doesn’t have access to write a file there:

 $ ​​history​​ ​​>​​ ​​/var/commands.txt
 -bash: /var/commands.txt: Permission denied

You might think that you can use sudo to get this to work, but sudo runs the history command as a privileged user rather than your user, and the redirection isn’t part of the sudo process:

 $ ​​sudo​​ ​​history​​ ​​>​​ ​​/var/commands.txt
 -bash: /var/commands.txt: Permission denied

But you can get around this using sudo tee:

 $ ​​history​​ ​​|​​ ​​sudo​​ ​​tee​​ ​​/var/commands.txt

This time, the file gets created. This works with echo as well:

 $ ​​sudo​​ ​​echo​​ ​​"Line one"​​ ​​>>​​ ​​/var/mylog.txt
 bash: /var/mylog.txt: Permission denied
 $ ​​echo​​ ​​"Line one"​​ ​​|​​ ​​sudo​​ ​​tee​​ ​​-a​​ ​​/var/mylog.txt
 Line one

One drawback to this approach is that you still see the output on the screen. But you can suppress the output by sending it to /dev/null instead of a file:

 $ ​​echo​​ ​​"Line two"​​ ​​|​​ ​​sudo​​ ​​tee​​ ​​-a​​ ​​/var/mylog.txt​​ ​​>​​ ​​/dev/null

You can use tee to create multiline files with sudo, similar to how you did it with cat:

 $ ​​sudo​​ ​​tee​​ ​​-a​​ ​​/var/mylog.txt​​ ​​<<​​ ​​EOF​​ ​​>​​ ​​/dev/null

You’ll be presented with a blank cursor, as tee is waiting for data from standard input. Type the following lines, pressing Enter after each line:

 Line three
 Line four
 EOF

The two new lines get written to the file.

If all of this seems too much like a hack, it’s because it is. It’s often just easier to create files in your home directory and use sudo to move them where you want them:

 $ ​​echo​​ ​​"some text"​​ ​​>​​ ​​~/myfile.txt
 $ ​​sudo​​ ​​mv​​ ​​~/myfile.txt​​ ​​/var/myfile

You’ve worked with two of the three standard streams in detail. You know how to redirect streams to files or other programs, so let’s look at how to handle error messages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.31