Instructor: Standard output to a file
using the command tee.
Now, we have learned how to actually populate a file
using the redirect commands, like the greater-than sign.
You echo something.
You output to a file using the greater-than sign.
Now there's another way that you could do the same thing,
but using the command tee.
What tee command does,
tee command is used to store and view,
both at the same time, the output of any command.
So in other words, the command is named
after the T-splitter used in plumbing.
So you've seen the T-splitter, right?
So you put in the water from one side,
it comes out from the both sides.
So this tee command works pretty much the same way.
So it basically breaks the output of a program
so that it can be both displayed on the screen
and saved into a file.
It does both the tasks simultaneously,
copies the result into a specified files or variable
and also displays the result.
So if you'll look at the picture,
you'll see right here we have a command.
So whatever the command you run,
you run echo, you run ls, pwd, chmod,
or whatever the command that you run, right,
you put a tee in front of it.
And then, it will not only send the output on the screen,
but it would also put it into a specified file,
the file that you wanted the output to go to.
So that's how the tee command works.
The reason we want the tee command
over the greater-than sign command,
which both do the same thing,
is because if you wanted to run a command
and you wanted to output to a file,
you also wanted to view, while you're running the command,
you wanted to view how or what the output looks like.
And whatever that goes into the file, actually,
going the right output that you're looking for.
Okay, so let me go into my Linux machine,
and I have it right here open.
Let's clear the screen and type whoami.
And you will see the name of the user account
that I've been logged in with.
Which directory I'm in?
I am in my /home/iafzal directory.
Of course, one more thing you should also check
is the hostname that you logged in.
The hostname is the correct hostname,
which is MyFirstLinuxVM.
These are the few things, of course,
you need to check all the time
to make sure that you are logged into the correct system.
Now, let's get back to our command tee.
Now let's say if you wanted to echo something on the screen.
Let's say we echo,
"David Puddy is Eliane's boy friend."
David Puddy is just a character in a show, "Seinfeld",
and he was the boyfriend of Elaine Benes.
So it's just a statement that I'm putting in.
And when you hit Enter,
and you will see that same echo on the screen.
Now, we could use the greater-than sign to output to a file.
So let's say if I wanted to output to a file eliane-david.
This is the name of the file, right?
I could do that when I hit Enter.
You'll see it has added this text
to a file name eliane-david.
Now what if I verify
by running the command cat eliane-david?
And you will see, the file has the correct text
that I wanted to have it in the file.
Perfect. Now if I replace it, the same thing.
Let's clear the screen one more time,
and run the command one more time.
And you will see, when you run this command with uppercase,
sorry not uppercase, with redirect sign,
it actually runs it.
But it doesn't throw anything on the screen.
But if you replace this with | and tee,
then you will not only see the output
going into eliane-david file,
the output will also show up on the screen.
So hit Enter, and you'll see
"David Puddy is Eliane's boy friend."
So it is going both ways.
As I said, that tee is like a plumber's T
that goes both ways.
So when you do cat eliane-david,
"David Puddy is Eliane's boy friend."
So now, don't be confused that,
"Hey, we ran that command earlier,
and it has that same text message
that we ran it with a greater-than sign."
If that's what it's in your mind, that's fine.
Let's go ahead and rm eliane-david file.
So it doesn't exist.
Yes, so clear the screen.
Now, we're on the same command by hitting Up Arrow key.
Alright, echo "David Puddy is Eliane's boy friend."
So, and tee eliane-david. This is the file.
Hit Enter.
Okay, now do cat david.
Sorry, eliane-david.
And you'll see, the file is there. Perfect.
Now, what if you wanted to add or append to the same file?
Because if you type, let's say,
echo "David and Eliane"
into the same file and hit Enter,
and you'll do cat eliane-david,
you'll see it removed the older file
and created a new file with a newer output.
So you want to append it to a file
that the older text message does not get deleted.
For that you would have to use a command,
same command but with -a.
So hit Up Arrow key and go back
to the same command that we ran earlier,
and this time type tee -a.
Same thing, echo "David Puddy is Eliane's boy friend."
This is just text.
Pipe it and tee it, and with the option -a.
And the file name, same file.
And now go ahead and hit Enter.
Now go ahead and do a cat on eliane-david file,
and you will see it has two lines.
First line is that we created without -a option,
and second line we created with -a option.
So that's how you append, or add, or create a new file.
Now let's try to see how many characters do we have
in eliane-david file.
So the command is wc -c.
And you will see it has 52 eliane-david characters.
We could also output that out
that whatever that came on the screen to another file.
So I use this command, which can be discussed,
or which I will cover later on, of course.
This command actually count every word
in the file called eliane-david,
and it will output on the screen.
You could actually redirect that output
using a tee command to another file as well.
Now let's say if you wanted to do ls -l,
and you see that listing, entire listing, of your directory.
So let's say if you wanted to have all that with the tee,
meaning I wanted to view it,
and at the same time I want to put it to a file.
So I'll put it as, I'll name it listdir.
And hit Enter, and you will see the entire listing
from here till bottom.
And you'll also see your file listdir.
If you do cat listdir and hit Enter,
and you will see the same listing.
So let's clear the screen,
and now hit Up Arrow key to cat listdir file again.
And you will see that ls -l output.
So basically, it's outputting on the screen,
and it is outputting to a file.
If you wanted to do the same thing, let's say echo -l.
And you could also output to multiple files,
meaning ls -l tee, to let's say,
file1 file2 file3.
And hit Enter.
You'll see, it actually ran,
and it has all the listing of all three files as well.
And when I do cat file1, it has the same content.
cat file2, it has the same content.
And so on to cat file3, same ls -l output.
So this is how the tee command works.
If you wanted to get more help
or more options on the tee command,
you simply type tee --help.
And you'll see, it will tell you exactly the options
that are being offered by the help command.
See, -a is to append, and -i is to ignore,
and version, and help.
So if you type tee --version
to see the version of this command,
you'll also see that this command version is 8.22.
And it tells you, "This is a free software:
you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law,"
and so and so and, "Written by Mike Parker,
Richard M. Stallman, and David MacKenzie."
So these are the people who actually built this command.
Again, since Linux is an open source,
you could even create your own command, write your own code,
and introduce it to the Linux world.
So that's how the tee command works.
I hope you understand.
If you have any questions, you could just shoot me an email.
And try it out.
Try it with different options, couple of options.
And I'm sure it will work just fine.