Redirection Part 1

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello Cybrarians, and welcome back
00:00
>> to the Linux plus course here at Cybrary,
00:00
>> I'm your instructor Rob Goelz.
00:00
In today's lesson, we're going to be taking on
00:00
the first part in a two-part series on redirection.
00:00
Upon completion of this lesson,
00:00
we're going to be able to use redirection
00:00
for standard output as well as standard error.
00:00
We're going to combine standard error
00:00
and standard output redirection,
00:00
it's online stream, and then we'll also
00:00
cover standard input redirection.
00:00
With that, let's go ahead and get
00:00
started with some demo time.
00:00
Once again, here we are in
00:00
our center lesson environments,
00:00
and the thing we should know is that
00:00
standard output is also known as stdout.
00:00
This Number 1 is used
00:00
>> as the descriptor for this type of redirection.
00:00
>> Generally, with standard output,
00:00
we don't need to use the Number 1,
00:00
but it is helpful to know about
00:00
all these redirector numbers to
00:00
differentiate the different types
00:00
of redirection from each other.
00:00
Generally, when we're talking about standard out,
00:00
we're just talking about
00:00
outputting something to the screen.
00:00
For instance, if we were to cut this file, File 1,
00:00
and outputs the content to
00:00
the screen, that's standard out.
00:00
In Linux parlance, this is the terminal that
00:00
you're on, but generally,
00:00
you can redirect standard out to
00:00
something other than the terminal,
00:00
is probably the second most common form of
00:00
redirection behind command redirection.
00:00
Most of the time what you're doing
00:00
is redirecting standard output,
00:00
output to a file.
00:00
You want to put the command result into a file.
00:00
There are two different characters
00:00
>> that are used for standard output redirection.
00:00
>> The first is the greater than sign,
00:00
and the greater than sign is used
00:00
>> to redirect output to a file.
00:00
>> Now if this file doesn't exist,
00:00
it gets created,
00:00
>> if this file does exist, it gets overwritten.
00:00
>> Obviously, that's not optimal for all use cases,
00:00
we don't always want to overwrite the file
00:00
>> that we're trying to write content to.
00:00
>> The other character you can use
00:00
>> is a double greater than sign,
00:00
>> so two greater-than signs.
00:00
What this does is it appends
00:00
the redirected standard out output into a file,
00:00
so it just adds to the file,
00:00
it adds another line through it.
00:00
Let's see this in action.
00:00
Again, let's make sure
00:00
>> we're in my home directory here and we are.
00:00
>> If you do a cat on File 1, hit "Enter",
00:00
>> we'll see that that's from File 1,
00:00
>> if we had File 2,
00:00
we can see that that's from File 2.
00:00
If I wanted to append the content of File 1 to
00:00
File 2 with standard output redirection,
00:00
I can do cat File 1,
00:00
double greater than sign File 2.
00:00
Now and when I cat File 2,
00:00
I'm going to see that it has the contents of File 2,
00:00
and then appended on a new line is
00:00
the content is the information from File 1.
00:00
But now if I were to do cat File 1,
00:00
and then just do a single greater than sign to File 2,
00:00
and now if I do cat File 2,
00:00
what we'll see is that that actually
00:00
overwrites the contents of File 2.
00:00
Remember, single greater than sign
00:00
>> overwrites the directory
00:00
>> or the places you're trying to write the output to.
00:00
>> If you use a double greater than sign it appends.
00:00
With that, let's go ahead
00:00
>> and move on to our standard error redirection.
00:00
>> In standard error is known as stderr,
00:00
and it uses the Number 2 as a descriptor.
00:00
Now, when we're talking about standard error output,
00:00
we absolutely have to use the Number 2,
00:00
because it uses the same characters
00:00
>> and standard output.
00:00
>> It uses a greater than sign
00:00
>> and a double greater than sign,
00:00
>> so the only way that we can differentiate
00:00
between the two of them is by using
00:00
two greater than or two double greater than
00:00
>> to tell that that standard error output.
00:00
>> Just as with standard out,
00:00
standard error can be redirected to a file,
00:00
this is useful in case the utility that you're running
00:00
>> doesn't automatically log your errors,
00:00
>> that way you can just send errors to
00:00
the file and check them out later.
00:00
Again, we use two greater
00:00
than to redirect errors to a file,
00:00
but just as we saw with standard out,
00:00
that's going to overwrite the file.
00:00
Instead, it's just going to create the file
00:00
>> that doesn't exist,
00:00
>> and if it does exist,
00:00
>> it's going to overwrite all of the content in it,
00:00
>> but if we do two double greater than sign,
00:00
it's going to append the content to the file.
00:00
Let's go ahead,
00:00
>> and we're in our home directory here
00:00
>> or my home directory,
00:00
>> let's just do a cat on a file doesn't exist
00:00
>> and we'll call missing file,
00:00
>> and we see an error message,
00:00
>> no such file or directory.
00:00
>> Now if we did a cat missing file
00:00
>> and we'd sent that error message to File 3,
00:00
>> now when you do a cat on File 3,
00:00
we see that it has the air that we just specified.
00:00
We can also do this
00:00
>> by doing a double greater than sign,
00:00
>> so cat missing File 2 double greater than File 3,
00:00
and now we do cat File 3,
00:00
we see that it has both of those error messages
00:00
>> because it appended a new line to that file
00:00
>> with the next error message that we just output.
00:00
>> Now, let's take a look at combining
00:00
these two types of redirection,
00:00
and this is really helpful.
00:00
Standard error and standard out can be combined
00:00
>> using the characters ampersand
00:00
>> and then greater than sign.
00:00
>> This is useful when you want to log
00:00
all output from command for review later,
00:00
or if you want to run command
00:00
>> and you just don't care about seeing the errors
00:00
>> or output right then and there
00:00
>> you just want to save it for later to look at.
00:00
>> Taking a look at this,
00:00
let's just make sure of store home directory,
00:00
>> my home directory,
00:00
>> and we'll do cat File 1,
00:00
which we know exists,
00:00
>> and we'll do cat File 1 and then missing file,
00:00
>> which we know doesn't exist,
00:00
missing file, and we hit "Enter",
00:00
we see that it outputs the contents of File 1
00:00
>> as well as the error message from the missing file,
00:00
>> the file doesn't exist.
00:00
>> For our purposes, let's just go ahead and do
00:00
an ampersand greater than sign on
00:00
this and we'll send us all to File 3,
00:00
and now we cat File 3,
00:00
we're going to see that it
00:00
posted in the contents of File 1,
00:00
as well as the error from that file not being there.
00:00
Last thing we'll look at today
00:00
is standard input redirection.
00:00
Standard input is known as stdin,
00:00
and it uses the Number 0 as a descriptor.
00:00
Again with this,
00:00
>> we don't really need to know or use stdin,
00:00
>> and that is because standard input uses
00:00
the less than character
00:00
and the double less than character,
00:00
which is also known as a heredoc.
00:00
What we're going to do with this
00:00
for our example is we're going to cat
00:00
>> the file that I have in my directory called namefile.
00:00
>> This is an unsorted list of just names,
00:00
so what I could do is I can actually use
00:00
this list aim standard input
00:00
to send that to the sort command.
00:00
I can say hey sort command,
00:00
sort this list of files for our lists of names for me
00:00
>> in this file namefile by using
00:00
>> this single less than character,
00:00
and what it'll do is it'll turn around and output
00:00
that content to standard out.
00:00
It's just going to do the same thing
00:00
>> that a sort command would do,
00:00
>> but instead of me having to type in the contents
00:00
of the names I want sorted,
00:00
or to put that information
00:00
to sort by using command redirection,
00:00
we can just do a less than sign
00:00
and send it a file that we wanted to sort.
00:00
The really nice thing about
00:00
using input redirection is we can turn around
00:00
>> and create a file with a sort of content.
00:00
>> You do sorted_namefile,
00:00
and now when we cat sorted_namefile,
00:00
we'll see that that same sort function ran
00:00
>> on the inputted file in this sorted it
00:00
>> in an alphabet of fall for us.
00:00
>> That's just a really nice quick way to do
00:00
that work without having to do
00:00
>> a bunch of command redirection,
00:00
>> without having to grab
00:00
>> and move stuff around, that stuff.
00:00
>> Now, as I said, the second type of command
00:00
>> that's user or characters that are used
00:00
>> in input redirection is known as a heredoc,
00:00
>> and the way that this works is you basically
00:00
use these double less than characters
00:00
>> to send information to a command,
00:00
>> and then you also provide it with the delimiter.
00:00
For instance, I'm going to do cat double less than,
00:00
and I'm going to say that my delimiter is end of file
00:00
or ELF, and I want to hit "Enter",
00:00
it's just going to sit here and it's going to wait,
00:00
and it's going to accept the input
00:00
>> from my keyboard until I type EOF,
00:00
>> and then it prints all the output to the screen.
00:00
For instance, I could type a bunch of names
00:00
>> like rob, kat, dave, kris, becky, nicole,
00:00
>> and other careers with C,
00:00
and then I can type EOF,
00:00
and what I'll see is that gets sorted automatically.
00:00
But another nice thing is just like we saw earlier,
00:00
with the single less than infinity direction,
00:00
I can do the same thing here,
00:00
and I can just say namefile_new,
00:00
and then the same thing is going to be true.
00:00
I just type in rob, kat,
00:00
>> dave, kris, becky, nicole, kris,
00:00
>> and when I type EOF,
00:00
this actually just gets outputted to that file,
00:00
and we do a cat on namefile_new,
00:00
we'll see that it's all sorted
00:00
just like it would be to the screen.
00:00
With that, we've reached the end of this lesson.
00:00
In this lesson we covered redirecting standard out
00:00
>> using greater than a double greater than symbols.
00:00
>> We redirected standard error using two greater than
00:00
>> and two double greater than symbols,
00:00
>> and we talked about combining
00:00
standard output and standard error
00:00
>> that uses the ampersand greater than
00:00
>> an input redirection uses less than
00:00
>> and double less than,
00:00
>> also known as a heredoc.
00:00
Thank you so much for being here,
00:00
and I look forward to seeing you in our next lesson.
Up Next