[No audio]
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 with 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.
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 task simultaneously. Copies
the result into a specified files or
variable and also displays the result.
So if you 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 want 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 go in 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 in to the correct system.
Now let's get back to our command tee.
Now let's say if you want to echo
something on the screen, let's say we echo.
[No audio]
David Puddy is Eliane's boy friend.
David Puddy is just a character in a show Seinfeld,
and he was the boy friend of Eliane Bennis.
So it's just a statement that I'm putting in.
And then 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 verified 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 pipe, 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 a 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 greater than sign.
If that's what it's in your mind, that's fine.
Let's go ahead and remove eliane-david file,
so it doesn't exist. Yes.
So clear the screen.
Now run the same command by hitting up arrow key.
All right. 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 there. Perfect.
Now, what if you wanted to add
or append to the same file?
Because if you type, let's say,
[No audio]
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 a text,
pipe it and tee it,
end 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 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 want 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, 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 ls -l, and you could also output
to multiple files, meaning ls -l tee, to
let's say file1, file2, file3, and hit
enter, you 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
is 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 will 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 extend the permitted by law.
And so on, and so on.
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, a
couple of options, and I'm sure it will work just fine.
[No audio]