As a Linux admin, there are lots of events when you need to save a command’s output to a file, other than simply printing the output on the terminal. Unsure how? Let the Unix tee command do the trick.
On this tutorial, you’ll be taught some ways to make use of the tee
command to save lots of and append output to a file.
Prepared? Learn on to take your shell scripting to the following stage!
Stipulations
This tutorial can be a hands-on demonstration. Should you’d prefer to comply with alongside, make sure you could have a Linux system. This tutorial makes use of Ubuntu 20.04, however you should utilize any taste of Linux.
Redirecting Instructions Outputs (STDOUT/STDERR)
Earlier than working the tee
command, it’s essential to grasp how redirects work. In Linux, every thing is taken into account a file, together with gadgets, directories, and even the output of instructions. Because of this, you possibly can take the output of 1 command and use that output as enter for an additional command (file redirects).
There are various kinds of redirects, and each serves a function. However when working the tee
command, you primarily use two various kinds of redirects:
- Customary Output Redirect (STDOUT) (>) – The default output of a command.
- Customary Error Redirect (STDERR)(2>) – An error message generated by a command and displayed in your display screen.
Each redirects take and save outputs of instructions to a file, however you’ll see how redirects work under.
Run the ls
command with out arguments to checklist all contents (information and folders) of your working listing.
Under is the STDOUT of the ls command.
Subsequent, run the under command to purposely checklist the contents of a listing (fake_directory
) that doesn’t exist.
You’ll get an error message saying that the listing doesn’t exist. This output is the STDERR of the ls command.
Saving Instructions Outputs to a File
Suppose you want to save lots of the STDOUT of the ls
command to a file as a substitute of simply printing the output to the terminal. On this case, the redirect operator (>
) turns out to be useful.
1. Run the next command to save lots of/redirect (>) the STDOUT of the ls command to a file named ls_stdout.txt. Since this command saves the output to a file, you gained’t get an output in your terminal.
2. Now, run the cat command under to view the contents of your ls_stdout.txt file.
3. Equally, run the next command to save lots of the STDERR (2>) of the ls command to a file named ls_stderr.txt. Once more, you gained’t get an output in your terminal because the output is saved to a file.
ls fake_directory 2> ls_stderr.txt
4. Now, run the cat command under to see the contents of the ls_stderr.txt file.
Under, you possibly can verify that the STDERR of the ls command was save to the ls_stderr.txt file.
5. Run the under command to save lots of each (&>) STDOUT and STDERR of the ls command to a file named ls_alloutput.txt.
Why accept one when you possibly can have each, proper? Usually, you may need to retailer each the outputs in the identical file for troubleshooting or logging functions.
6. Lastly, run the next cat command to view the ls_alloutput.txt file’s content material
Connecting Applications with Pipes
Other than simply saving outputs to a file, you possibly can accomplish way more utilizing pipes, like connecting packages. In real-world eventualities, you’ll most definitely use pipes along side file redirects and the tee
command.
A pipe (|
) is a mechanism for inter-process communication that permits you to join one command’s output to a different command’s enter. In different phrases, the pipe takes the STDOUT of 1 course of and connects that STDOUT to the STDIN of one other course of.
Run the next grep command to seek out all of the strains containing the phrase tcp within the /and so on/providers file.
Now, run the identical grep
command under. However this time, pipe the output of the above grep
command to the much less
command to show the search outcomes one display screen at a time.
grep tcp /and so on/providers | much less
How does the piping work? As you possibly can see under, while you use the pipe character (|), the output of the grep
command is saved in a short lived location. The output is then handed as an enter to the much less
command.
Combining the Unix Tee (tee
) Command, Pipes, and Redirects
Thus far, you’ve discovered how the tee
command, pipes, and redirects work. However why not use all three collectively for higher management of command outputs?
You beforehand used the piping with redirects, and that’s an enormous step. This time, you’ll use piping and the tee
command to ship one command’s output to a different for additional processing.
Run the under command to ship the output of the ls
command to the display screen and the end result.txt
file. The output of ls
is additional piped to the grep
command to filter solely the strains that comprise the phrase ip
.
ls | grep 'ip' | tee end result.txt
Now, run the cat
command to confirm the contents of the end result.txt file.
Appending Output to Present Recordsdata
You beforehand saved the output of instructions to a file, however are you able to replace an present file as a substitute of making a brand new one? Sure! Let the tee
command do the magic.
By default, the tee
command overwrites the contents of an present file. However in lots of instances, you may need to append the output of a command to an present file. Resembling logging all server occasions in real-time to a log file.
With this characteristic, the log file will get appended with new entries at any time when an occasion happens on the server.
Run the under command to take the output of the ls -lR command and append the output (tee -a) to the end result.txt file’s content material. This command doesn’t present output because you’re sending the command output to a file.
ls -lR | tee -a end result.txt
Now, run the cat
command to confirm the contents of the end result.txt
file.
As you possibly can see under, the output of the ls -lR
command is appended to the top of the end result.txt file.
Utilizing the tee
Command in Conjunction with sudo
Many instructions require elevated privileges or sudo
entry, like performing modifications contained in the /and so on listing. The query is, does the tee
command work hand in hand with sudo
? Sure!
Prepending sudo to the tee command permits a non-root person to jot down information to a file that solely a root person can modify. For example, you need to allow the person ata to jot down information to the /and so on/ata.conf file. However, on the identical time, you don’t need to give the person ata root privileges.
1. Login to your machine with a non-root person.
2. Subsequent, run the next command with out sudo to jot down the textual content information to the /and so on/ata.conf file.
echo "information" > tee /and so on/ata.conf
As you possibly can see under, you’ll get a Permission denied error since ata person doesn’t have write entry to the /and so on/ata.conf file.
3. Run the identical command under as you probably did in step two to jot down the textual content information to the /and so on/ata.conf file. However this time, prepend the sudo command to the tee command. Doing so permits the person ata to finish the command (elevated) as a non-root person.
echo "information" | sudo tee /and so on/ata.conf
Lastly, run the cat command under to confirm that the ata person might write the textual content “information” to the /and so on/ata.conf.
Conclusion
All through this tutorial, you’ve discovered learn how to use the Unix tee command to jot down information to information and the way it works along side different instructions. At this level, you need to have a robust mixture of instructions you should utilize to handle enter and output on the Linux command line surroundings.
You’re like a sushi chef, fully controlling the elements and proportions that go into every dish. You’ll be able to customise every order on the fly to fit your buyer’s tastes. The one restrict is your creativeness (and possibly the scale of your terminal window).
Now, go forth and create some wonderful command line concoctions!