/ factorpad.com / tech / linux-essentials / tee-command.html
An ad-free and cookie-free website.
Intermediate
Learn to route command output to two locations at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux tee Command Summary with Examples (3:46)
Our ninety-first word, or command to memorize is
tee
from our category
Workflow.
tee
allows you to route to both
standard output and a file.
-options | description |
---|---|
--help |
Print help screen |
-a ,
--append |
Append to file instead of overwrite |
Recall from videos (tutorials) #87 to #90, we're building a
script to demonstrate workflow, well now we'll cover the
tee
command and
add it to a script.
Before we start, it helps to think of commands as mini programs and
most follow this structure:
command -option(s) argument(s)
.
The tee
command has
4 options and the argument is a filename followed by
a second pipe. Think of it as if the second pipe can be for additional
processing by another command. And we'll see that in a second.
Like most commands, help is available with double-dash
--help
,
-a
appends, rather than overwrites
output to a file.
So why is tee
an important command?
Well, we can eliminate the need to write two separate commands to
route output to two different places.
And now you know how to do that.
Okay, the best way to embed this in your memory is by typing in your own terminal window.
Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.
Here we go. Let's ls
all of the
text files in our working directory.
And there are quite a few. You could pipe, or redirect this output to
another command, like the word count for lines using
wc -l
.
And get 42 files. So you've routed to another command.
Next, what if you wanted to stop after the first listing, write to a file, and then keep processing with another command? Hmmm.
So let's do it in our script, at the
bin
directory we have a script. It's
called funscript
.
(Below is the screen from within nano
.)
(Here we added lines about video 91 and below.)
Here I added a comment, text to explain the output, followed by the
ls *.txt
from earlier, and then
tee
it to a file, and then report out
the last 5 lines.
Okay, please pause if you'd like to catch up on your end and then we'll try it out.
(Hit Ctrl-x
to leave
nano
and
y
to confirm saving.)
And last, explicitly run with this, in the
bin
directory, then
funscript
.
And there's our output. As you can see, the explanation, it wrote to the file and gave us the last 5 lines. Very good.
Okay now you know how to use tee
.
And you know the syntax for commands, options and arguments.
One last tip about the tee
command. So the append option -a
is
especially helpful if you're interested in specific entries to logs,
for example.
Okay, thanks for visiting today. I hope this was a helpful
introduction to the tee
command.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
Our YouTube Channel has a growing group of very smart people sharing knowledge. We'd love for you to be a part of it. Subscribe now before you get distracted.
xargs
command,
hit Back.printf
command, click Next./ factorpad.com / tech / linux-essentials / tee-command.html
A newly-updated free resource. Connect and refer a friend today.