Hello and welcome to Command Line.
In this video, we're going to go
over command redirection.
I'm Christopher Haler.
And let's begin this
hour, learning objectives for this
video. We're going to understand the
purpose of redirection and why we
should do it and how we can do it
properly.
Learn what this the greater
than simple does, learn what
the double greater than simple
does and what the less
than symbol does.
So which operand appends
to an existing file?
Is it a single less
than B A double less
than C and double greater
than or D a single
greater than?
The answer is C, the double greater
than what this does is it will
append to an existing file,
whereas the single greater then will
overwrite an existing file if it
exists already.
The single less than is
used for input into
a different command and the double
less then is not a valid operand.
So the redirection, the whole
purpose of redirection from the
command line is that we can use
the output and either save
it or we can direct input
using the less than
symbol so we can specify
a different standard or standard
error levels of redirection as
well instead of just the normal
output.
So the useful way to be able to
either gather information
from the output of command that we
can search through later or save
it and keep it in a different place.
So the single greater than what
this does is it just simply takes
the output from a command
will stuff it into a file on the
system.
If the file does not exist,
it will create it automatically.
Now, the big thing to keep
in mind single greater then is
that it will overwrite a file if
that file does exist.
So that can be a problem
if we want to keep the
rest of the information that file
already.
So let's go ahead and give a
quick demonstration.
So I'm going to you there
so we can see what's inside of this
now, let's say Echo
hello, and we're
going to use the single ampersand
and put it into
hello to you
or excuse me, not an ampersand, but
single grader then and now.
If we do another year, we could see
we have hello to text
and let's do type
hello. And we could see hello
there.
And now if we do this again
but instead of hello
we have hi
now with the single grader then it's
going to overwrite that low
and now we can do type again.
We can see the Hillo is gone and all
we have left is a high.
Now in order to overcome
this, we can use the double grid in
it. And what this does appends
to the end of the file if it does
exist. So we don't want to overwrite
everything in the file already.
We use the double greater than and
we'll automatically put it at the
bottom of the file so we don't keep
overwriting something that we want
to keep in there already.
And just like the single greater
than it will create a file if it
doesn't exist already.
So let's give a quick demonstration
of the double greater then and how
we can use it to not overwrite what
we're working on.
All right. So let's say
this didn't overwrite
and the double grader then
and load up to 60.
Now, if we do type four.
Hello, we can see we have this
didn't overwrite on the
end of the file
that was already there and it did
not overwrite the high that we
previously just did.
So this is very useful to keep in
mind as we are
redirecting output into files that
we don't want to override, things
that we want to keep in there.
And we had the single graded.
And what this does is it is used
to provide input from a file
into a command.
And this file must exist
because you are providing it has
input and it will not
change the input of the file.
So it will leave the file.
The same will simply provide that
file as input into
the command so we can
use the sort command and
the less than to say, hey, I'm
pushing this file that text
into the sort command,
so let's give a demonstration and
then I'm going to pull up my Linux
machine.
And just like always, I'm going to
type LS to see what is inside
of this directory.
And now let's say I want to sort of
Latin words that you
sort
and Latin words that txt
with my less than simple
and look that goes ahead takes it as
input sorts Morlot from
pretty useful.
And now we can also redirect
standard error from
commands as well, and this is
something you may have seen already
where if there's a lot of error
text, especially if you're doing a
search and
the command doesn't have full access
to the system, it will give you
error saying, hey, I can't reach
this. And that can take up a lot of
screen information.
So we can do is we can redirect
that standard error into a place
that doesn't exist on Linux.
That is Dev no
dev signal and anything sent
to this directory on
Linux is immediately dropped and
just ignored.
And now in order to specify a
standard error, we're going
to use the two and then the
single greater then.
And this says, hey,
for any errors that you get while on
this command is running, I want you
to take it and spit the
standard error into this
specific location and using
the div. No, it says
just get rid of it.
So this is a useful way to be
able to ignore all of the
error information.
So let's say I'm going to use my
Linux machine and I'm going to
search for my program
on the root of the drive.
Now, as you'll see,
let's take the report
I my program
forward, slash the route.
Now you'll see I will have
a lot of
this, my bad history,
and now I should have a lot of
permission denied, not go.
So there's way too much going on
right here.
It's taken over my entire terminal.
I, I'm going to use controls
to stop this before it takes
up more space and then
do clear Nancy
less so that I can get back to that
kind of default position
on the terminal.
Now, if I do
too, Divx
know the saying,
I want you to take these standard
error and push it into Dev.
No.
So as we'll see, we won't get all
those errors because it's
all the error messages that
it gets automatically sends to
that.
There's no directory
where it is dropped by the limbic
system.
So that way we can focus on directly
just what the output
we have.
So here we go. So we can already see
some different
information coming back.
And we don't have all these error
messages coming all over the screen
and taking over our
our our command
window. Excuse me,
I supposed to system, which often
overrides an existing file.
Is it a single less than
the less than
C a single greater
then or D a double greater than
the answer is C a single greater
than this will overwrite an existing
file and it will not
append to the end of the double
greater than will append to the end
of a file if you want to keep it.
But if you don't care too much about
it, or you just need to create
a new file, the single greatest
work, exactly what you needed to do,
and the single less than
is used to provide input
to a command not to
get the output for it.
All right. So in this video, we
learned the purpose of redirection
and how we can use at the command
line, learned what the single
greater then does learn with the
double greater than does and how it
happens to the file and keeps the
file and understood with the single
less than does and how we can use
it to our advantage.
And I hope to see in the next video.