We'll now look at how to redirect,
input and output in Linux to understand input and output redirection.
Let's take a look at a utility like sort.
First of all sort expects to receive some text input
and usually it will get this input from a file. So let's assume we have file dot text
with the following content.
Now if we run a command like sort file dot text,
then the following happens,
sort gets input data from file, do text
and then it orders it properly and generates
some text output with the sorted results.
Now many Linux utilities work this way.
So what's with all this input output redirection stuff?
And what use does it have?
Well, think about what sort did here, it ordered our numbers correctly.
But then all it did was display this output on the screen.
However, what if we want to save these ordered results?
Well, we can do that with output redirection
with a command like this.
Now this output instead of being displayed on the screen and
then lost is saved to sorted file dot text instead,
text output is redirected to that file.
Now it's not necessary that the file exists beforehand.
The redirection can automatically create it.
Now, why is it called redirection?
Because programs have a default location where they send output,
they'll normally send it to our terminal window on our screen.
Since we change this default output location, we direct it somewhere else.
So we call this output redirection
to redirect the output to a file.
Simply add a greater than sign to the end of your command and
specify the target file where you wanna redirect or save that content.
It's important to understand that when you redirect output this way,
the file is overwritten.
Now, to test this out, we can try a command like this
and we'll run it five times in a row
and we'll see that only the last generated output remains in our file.
Every time output is redirected with the greater than sign,
old content gets lost and new content gets written
and sometimes this is undesirable.
Now, we can change this behavior instead of overwriting,
we can add or append new output with two greater than signs.
Let's also run this five times.
And now we'll see that each new line of output is added at the end of the file.
The top line is the oldest content. The one at the bottom is the newest.
Now to summarize the greater than sign is used to redirect output to a file.
Now, alternatively, this can also be written like this
with a one before the redirection character
this would generate the same result.
But let's look at what this means.
Now, a program like sort must know where its input comes from.
It must also know where to send the output that it generates.
But output is of two different types. One output is for normal text and data
that was successfully processed.
The other output is for error messages,
warning messages or anything that signals something went wrong in some way.
And that's why we used one with the greater than sign followed by
a file name to signal that we want to redirect normal output.
In this case.
Now,
when a utility tries to figure out where its
input comes from and where output should go,
it looks at these things
but output is of two types.
One output is for normal text data that was successfully processed.
The other output is for error messages,
warning messages or anything that signals something went wrong in some way.
And that's why we use the one and greater than sign followed by
a file name to signal that we want to redirect normal output.
In this case.
Now,
when the utility tries to figure out where its
input comes from and where output should go.
It looks at these things
STD in
which is short for standard input,
std out
which is short for standard output
and then TD error, which is short for standard error.
The less than sign is used to indicate standard input and
the greater than sign is used to indicate standard output.
Since there are two types of output,
the greater than sign can then be prefixed with one or two digits,
prefix one for standard output and prefix two for standard error.
So an overly simplified explanation is this,
the program asks where should I send these errors?
And Linux says, take a look at how standard error is set and send it there.
So then we use something like two greater than sign errors dot text.
And we basically set standard error to point to this file
and the application now knows where to direct its errors.
And now we can recap to redirect input. We'll do greater than
and then the file name you want to direct it to,
to redirect normal output.
We can do greater than file dot text or one greater than file dot text
and to redirect errors and warnings.
We'll do two greater than and then errors dot text.
But why would we want to redirect errors?
Well, if you tested commands from our previous lessons,
you might have noticed that some finding
rep commands displayed a lot of permission,
denied errors.
Now, error messages are normally useful, but in this case, they just get in the way
they make it harder to spot our results.
Now,
the good news is with input and output redirection tricks we can get rid of this mess.
For example, we can type in a command like this
now we have clean output that only includes the
results we are looking for no more error messages
and the magic happened right here.
Now two signals that this refers to standard error,
where error messages and warnings should be sent,
the greater than sign indicates that we want to redirect this.
And Dev Knoll is the location where we chose to redirect our standard errors.
Now, with this,
we redirected error messages offscreen to Dev Knoll or the black hole of Linux,
whatever you send to Dev Knoll is simply discarded.
This special slash Dev slash knoll file is often used
for such purposes just to get rid of unnecessary output.
Now,
we can also redirect normal output and error output
both at the same time to two different files
to overwrite old file contents.
We would use this
to add orin new contents at the bottom of
the file while keeping the old content unchanged.
We would use a command like this
and notice that the only difference is the single,
greater than sign or double greater than sign.
Now, if we take another look at the output generated by our grep command,
we realize that normal messages are intertwined with error messages
if we'd want to save all the output exactly like we see it.
Now, what command would we use? We would use this one
and two greater than ampersand
one has to be at the end.
This will ensure that everything that we see on screen that includes
a standard output in the error messages are all redirected to a file
and I'll explain the syntax in a few moments.
Now,
if you're not sure you can remember this order
of where to place these two redirection rules,
then you can use an alternate command.
In this way,
you'll see the one and then the two and you'll know it's the natural order
for these numbers which will help you place
the redirect rules in their proper spots.
Now, let's see what exactly is happening here.
Well, first with one greater than sign L
output dot text,
we are saying standard output should go to the alloutput dot text file.
And with the second part,
we're saying standard error goes to standard out.
And in this case, Ampersand
one instructs our command to redirect to standard out instead of a file.
Remember, AMPERSAND
one is standard out. Ampersand
two is standard error.
So with standard error going into standard out,
the error messages get mixed with normal messages
and now that we have the mix that we need,
all of this will go to all underscore output dot text.
If you wrongly place the two greater than Ampersand
one in the first spot, then the following happens.
Basically, it redirects errors to the current location of stand out,
which is on screen
because standard out at this point still directs to
its default location since it hasn't been redirected yet.
So instead of errors ending up in alloutput dot text file,
they'll end up on the screen and get lost.
Now we explored output redirection.
But why would we need to redirect input
well,
commands like sort already know they should take input data from file dot text.
But here's an example of one good use case for input redirection.
Think of an imaginary command where you type in send email someone at example.com.
Now by default, it expects input from your keyboard.
So you can manually type in the content of the email.
It does not let you pass a file as input like sort does.
But with input redirection,
we can trick it into thinking we type something on our keyboard.
Now with the help of the less than sign we redirect input from file to program,
the effect will be exactly like we typed everything out
everything that's stored in that emailcontent dot text file,
you will rarely need the redirect input.
But when you find yourself not being able to
pass a file to the utility without regular approaches,
you might want to try this.
There's also a more often encountered use case for
input redirection but this time not from a file.
And this is called here document or here doc for short with something like
eof we signal that the input, we want to pass ends before the last line where we type
eof
now any text can be used here instead of ef
you could type in end or delimiter or whatever.
But eof is commonly used this way,
we can type multiple lines of text to be passed in as the input data to this command.
The advantage is this can contain both the command and the input.
It should work with,
grouped into a single block of text that can be easily
copied or pasted shared on the internet and so on.
Now, a similar variant called a hears string looks like this.
With this, we can pass a single line a string as input to our program.
Whatever follows the three less than signs is the input.
Now here, the benefit is easier to observe.
Normally BC is a command-line calculator that you open up,
you enter its environment and then you type in mathematical expressions
and this forces you to go through multiple steps,
you enter a command to open BC, then you type in your mathematical expression,
then you exit BC, which might be inconvenient in some cases.
Now, with the here string,
you can pass input without needing to open the
utility and use the keyboard to type into BC.
And such things might be useful in scripts where you want to automatically solve
some math expression and get the result but keyboard input can be available.
Now, most Linux utilities are very small and usually deal with a single task type.
Let's think of two of these programs grep for searching and sort for sorting text.
It might seem limiting that each application can only do a single kind of task.
But here's an example,
we could search for all uncommented lines here to find settings that
are applied by this file and it's easy enough to read through.
But now imagine that there are 1000 different settings here and 1000 lines of text,
we keep having to scroll up and down again and again,
to look at the different things that we're interested in.
They are entirely disorganized.
So we wouldn't know where create home or encrypt method might be
or what line they might be placed at.
Now, if we could sort all the variables here alphabetically,
it would make our lives much easier
because we could scroll to the top where we would expect create
home to be and scroll a bit down for Encrypt method.
They should be close to each other since the alphabetic order is AB CD E.
So encrypt should be right after anything starting with the D letter.
But if grep can only search but not sort output, how do we solve this problem?
Well, we have the sort utility
but this means that we have to somehow send output from grep directly to sort.
And sure enough, we can do that with the pipe sign.
And this is called piping output
from program A to program B on the command-line
and just like that problem solve.
But
now notice how the columns are misaligned.
Some are further to the right some are further to the left.
How can we solve this? Well, we can pipe it to yet another program,
we can keep adding as many pipe signs as we want.
And in this case with grap, we find our text,
then we pipe the output to the sort utility to alphabetically sort everything.
And then finally we pipe from sort to the column
utility which will arrange all of our columns nicely.
Each program gets output from the previous program on the left works on it,
then sends it to the next program on the right.
And now it looks perfect. And we noticed that piping output can be very powerful.
That's because it basically allows us to interconnect many programs and do much
more complex things than a single utility can do on its own.