Hello, everyone, and welcome back.
In this video, we are going to learn input and output redirects.
There are three different type of redirects and Lennix the first one is standard input, which in short,
as studied in and it has file descriptor number zero.
The second one is standard output, and it has followed the script, a number as one and the third one,
a standard error.
And it has file descriptor number as to please do remember everything in Linux is considered as a file,
like a regular file as a file.
A directory is a file.
And all those devices attached to your Linux system, for example, a monitor, keyboard, mouse or
any of those peripheral devices that are considered are seen by the operating system as a file.
So when they file when we actually write something on the screen from the keyboard that is coming in
standard input and it is actually knocking or knocking on the door for a while, zero file descriptors
zero.
And when we are showing that output on the screen, that output is actually going to the output descriptor
number one.
And two is if there is any error, it will show up as the file descriptor to.
We'll learn more about it in the next slide.
For now, we'll grab on to the output, which is a standard out one.
So by default, when running a command, its output goes to the terminal.
So we see that whenever you type anything on your screen as you are typing on your keyboard, IT output
shows on your screen.
So that's input is your keyboard and the output is your screen.
So the output of a command can be routed to a file using that greater than a simple sign.
So if you are typing something, let's say if you're typing a command, you could out the output of
that that you see from the command to another file.
For example, let's say if you run ls minus L command and you run it on your screen, you will see the
list of files.
Right, but you don't have to see it.
You can rotate to a file, save it to a file, and for that you have to use a single uppercase sign
which is called single redirect or the output.
Same way if you're running the command p W.D. then you could do the same thing.
You route the output to a file name, whichever file name that you decide to pick.
If using the same file for additional outputs or to appended to the same file, then use double size,
double symbols of greater signs.
For example, if you want one else minus L.A. and you want its output to also go to the same file name
listing's then use to greater than size.
And if you want to use a command, for example, Echo and you want to do Hello World, and you want
that output to go to the file name find path, which we used earlier for P WD, then you got to use
that to greater than size.
Now the question is, why can't you use one greater than signs or one redirect?
You can, but if you use only one and then it will overwrite your file, whatever, the content of the
file will be overwritten.
So if you don't want to get rid of the content of the file, you don't want to delete it, then you
use two grand sides.
So let me show you real quickly in my Linux machine how that works.
So I have my Linux machine right here.
I'm going to log in.
OK, so the first thing is, if I do X minus L, you'll see all these files and directives that show
up.
Now what if I wanted to move or a route to this output, to a file so I could do is this one cell output
with just a single output redirect and pick any name of your choice?
I'm just picking a listing's hit enter.
Now, you see, you don't get the output on the screen on the terminal because it has been rerouted
to the filename listing.
You could view the content of this file by using the command seerat, which is the command is used to
read what's inside of a file.
So you do cat listings and you'll see.
The contents of this file, this is it's not actually the output of ellis' mysel, but now this has
become the content of this file now.
Similarly, if I wanted to use a command P WD, which tells me my path where I am right now, then I
could route that output as well so I could do a route that to a new filename find path.
Now you could do the same thing, cat find path and you will see it will give me the information that
output before which are routed to this one.
All right.
Now to redirects, which is to appending to the file.
So I wanted to run this command, Ellesmere's L.A. and by the way, when you run L'Esperance, L.A.,
it will give you all the contents of your directory, including the hidden files as well.
Hidden files are those that has doc in front of it.
You see right now, all those files are showing.
If I just simply want LSW, L.A., it will not show me the hidden box.
So in order to view the hidden files, I have to use Ellis' minus L.A..
Good.
Now I want to output this to the same file listing.
I could do this minus L.A. and a single redirect to Listing's.
If I do that, then the previous content that I had in listing will go away.
So if I wanted to keep those content as well as add these content, then I have to use to redirect it.
And now if you go to Cadd on this thing, you're going to see a lot of output.
That's more the screen.
Let's go up, scroll up, see, the first content is still here, the second content are coming right
here, the both output into a single file because we use to redirect.
Let's say if I use one redirect, for example, I use less one redirect, whether that's output is going
to look like this or else one redirect to Listing's.
And now, if I wanted to view the contents of listings, you'll see it only has those contents.
Everything else is gone.
That's why it is important that you use to redirect instead of one if you don't want to override the
content of the file.
Similarly, we have echo files.
So let's say I wanted to use Echo.
Hello, world, which is going to be which is going to echo back what I'm writing, so echo how the
world is going to echo back.
World, if I wanted to report it to my file, I have to use to redirect.
If I don't want again, if I don't want to overwrite that oil and I do find path, that same file that
I use hit enter.
Now I could Dukat find Pat and you'll see it has the path which I used it for.
This is the output of PWP and this is the output of Eckermann.
So that's the standard output.
Now, let's moving on to.
Input, which is standard input and the descriptor files, the script for this is zero input is used
when feeding files, contents to file or a script.
For example, let's say if you wanted to cat or view the foulest contents of a file listing, then you
could actually use Cat Command and you could specify the file name.
But in reality, if you just actually wanted to feed it bad, you could also do that by cat feeding
a bad the content.
Same thing if you wanted to use the mail program, for example, in our Linux machine, mail would give
us this option to to actually in indirect bring the content of a memo or a file into that program.
For example, if I'm using mail minus like I'm sending email with the subject line office memo and I'm
sending it to all the users of the company, ABC dot com.
Now, what is going to be the contents of that letter?
That content of the letter is actually written inside of the mimolette.
I could type all the content, but it's better I just redirect the content.
So that's what that input comes in.
OK, so let me quickly show you again by going back to politics machine right here, I did Cat find
part, right?
It shows me that TBWA Output and the Echo command that we ran, the contents are in there now.
I could also do cat input, fine pet.
And it would give me the same information.
All right, moving on to the third one, which is the error, so it's just standard error and it has
a file descriptor of two.
So when a command is executed, we use a keyboard and that is also considered standard input, which
is zero.
And that command output goes on the monitor and that monitor is standard output one.
So if the command produces an error on the screen, then it is considered as standard error we can use.
It redirects to root those errors from the screen.
If we don't want to see those output of a command that has an error and we could rule it to a different
file.
For example, let's say if I wanted to do what is the content of a root directory?
Now, of course, root directory is owned by root, not by a regular user.
It will produce an error.
I could root that to a file, but I have to specify how am I wrong?
I'm only routing the standard error, not the entire output.
So that's why I specify to criticize, assign and the file name of error file.
Similarly, I could use a program like Toweled Localhost and its error message if it's not going to
be able to connect to that host.
I don't want to see that on the screen.
I just wanted to go into the error.
Well, let's go back to our Linux machine and let's quickly take a look how we could.
Use the standard error.
All right, so first thing is, let's say if I wanted to run this minus El Slash route, this command
is basically what it's trying to do is it's trying to list the contents of root directory.
And I am being logged in as who am I am logged in as I absol.
So I'm trying to find what's inside of Slagel directory, which is owned by route, by the way.
So let's see if I do this myself route.
See, give me permission denied.
This is standard error, the error that you see on the screen, which is telling you what would happen
when you ran the comment.
Now, if you do not want to see that error message, then you could do is.
I'll put that to.
Aerofoil, that's just a file name I picked now that output is gone in there, if I wanted to view what's
in that file, I could Dukat our file and you'll see that error message that was produced by the command
is actually routed into the error file.
Now, I would use another command, for example.
I wanted to tell that localhost is just a small program which actually allows you to connect from one
machine to another, just like MSH, which, by the way, will be covered later on.
So telnet localhost.
Which is my own host, if I hit enter, you'll see it's giving me that it is trying.
But then it says, hey, connect to address this connection, refuse, so this is actually the error
message, then it says it's trying again and it is trying the second time with this IP.
It produces the same message, connection refuser.
You can not connect you.
So it means from this output, we have two errors right now.
I wanted to view these.
I don't want to see these errors on the screen.
For that I have to do is to let localhost.
I want standard error, which is file descriptor number two to output that to error file hit.
Enter now on the screen I'm only seeing trying one and trying one twenty seven zero zero one and error
message.
Where did it go to Cat File.
It went into that file.
So please do remember these three input and output redirects.
These are very important.
You will be using them when you have to create scripts that are very useful in creating scripting and
automating tasks.
I'll see you in the next lecture.