And we're going to go through
how to manipulate file output.
Typically in Linux,
the standard input is the computer or your mouse.
The standard output is the screen,
but for the command line purposes
it's going to be the display on the CLI.
Now we can change these standard inputs
and outputs using the angle brackets.
For example,
if we're going to use this software
listing that I created earlier,
the standard output is going to be the screen,
the command line interface,
but I can use the same output, and put it in another file.
So we can do software listing one, right.
And the output, we're going to use the right angle bracket.
We want to let it know, hey,
we want to send the output to this test file.
Now, if we look and we see the test file's created,
and we try to view the test file,
we'll see it copy right over for us.
So using the angle bracket to the right,
it replaces what's in that new file,
or creates a file if it doesn't already exist.
Now, if you want to add something to it,
we can use the echo command.
If you want to add specific strings.
So let's say we're going to test.
And now let's do this again.
Now you see it appended the new text
to the bottom of the file.
So it's very important to realize
if you want to add information,
you have to use two angle brackets versus one,
'cause one will replace everything.
So just to use a more clean example,
just say we want to create a file
and we want to have it insert information about the user,
a make of a computer, and the serial number.
And again, we want to add this to, call it testfile2.
Okay.
Now again,
we're going to view this using cat, test,
we did a tab key, and we see it input it to the file.
So again, this operator is very useful.
You don't always have to use a text editor like VIM or Nano,
what you can do is type directly
on the screen it's something quick,
use the right angle bracket operator
or the double one if you want to append it.
Remember,
a single right bracket operator is going to erase
the contents of a file and replace them with your own.
So let's test this out.
Same thing, USER1, MAKE1, serial number one.
Now when we look at the contents of this file
we'll see that it's replaced.
But if we want to add it,
operator, do it again, now added it.
Again,
that's just another example to further demonstrate
the use of the right angle bracket operator.
Now, next we're going to use the translate command.
Now this command is going to be used to translate
one character to another,
and we're going to use it with the left angle bracket operator.
Okay?
So we're going to just, again,
we're going to look at our test file in here.
Let's see here.
Do test file two.
Okay.
So we see we have this information in there.
So just say we wanted to change
all the Us to lowercase Us.
So we're going to do, translate here,
and the first character's going to be one to be replaced.
So all uppercase Us,
and we'll change it to lowercase Us.
And we're going to do that for our testfile2.txt.
And we can see that now it output the result
showing that the users now have a lowercase Us.
So again, we can do the same thing.
Just to make it a little weird for the uppercase Ms,
change those to lowercase Ms.
And again,
these files are usually used to change things
from lowercase to uppercase.
But again, you see, this is just showing the output,
and it's not actually changing
the content of the test file itself.
Now just say sticking to this same example that we did,
want to change the output and put it in another file.
We can do that same thing.
So again, we want to translate the Us,
or the uppercase Us, to lowercase Us.
And the file we want to use is testfile,
and we want to put it in another file.
We're going to name this testfile3.
Now we'll see the output is changed,
and "saves" because it is sent to another file.
Okay.
Again, you'll see the contents of the first file,
testfile2 remains the same.
But we can change that information
and send it to be saved to another file.
So again, once we read it,
if you're looking at the command
we're reading it from left to right,
so we're going to translate the uppercase Us
to lowercase Us in the text file number two,
and send it to test file number three.
So next one I'll work on another file that I created,
this computer lists of users, operating system,
and the make of the computers.
What we're going to do is, I want to sort them really quick.
We're just going to show the output,
and see how we can change the standard
input and output with these as well.
Okay.
So we see that on the first column
it sorted the object in alphabetical order.
Now, again, we see that the header was included,
but we're going to use another example
where the header is not included.
So the name will stay at the top,
and it will sort all the actual fields below.
Now, to do that,
we're going to use the piping command.
So what that does is it redirects
the output from one into another.
So we're going to use the tail, use -n,
so the number of lines, plus three.
So we're going to specify this.
And we're going to use the computer text file.
So that output, we're going to sort that output.
On the first column,
so prior we have the name title
associated with the fields below,
but now it's no longer there.
So again, reading that command from left to right,
you'll see that we're only going to see the bottom 10,
and we're not really concerned about that
because we have less than 10 in here.
The number of lines is going to be the top three, right?
So that means we're going to include line three and below only.
So again, if we were just to capture this out,
we'll see the number of lines
'cause I think it's very useful to see that,
and then run the sort again.
We can see that it started with line three.
So you see John is included
in this result, and it went down.
And we sorted everything with column one.
Next we're going to use the T command.
Now the T command simply does an output redirect
to a file or directory, but also displays it on the screen.
So you could have both.
And you can see what is being sent to the particular file.
So earlier when we used the echo command,
it didn't show us what it actually sent to the file,
but with the T command,
it's going to show us what it sent to the file.
So for this example we're just going to show
the list of files in this folder.
And we're going to just want to copy that list in this folder,
and we're going to send it to another folder
that we're going to name, backup directory.
Now it's letting us know,
we sent the list from this directory to backup directory,
and this is what it looks like.
So if we do a cat, backup directory,
we see the display starting from here.
So again,
it does nothing more than just show us at the same time,
it redirects it to a particular file.
And the last thing I'm going to go over
is how to pipe grep and cut, and use those together.
So we're going to check our secure file
and that pretty much stores all
of our authentication attempts.
So we're going to pull out some
of the information after we've searched through the file
for a particular type of error that we're looking for.
So we're going to find a password check fail,
and then we're going to condense the information,
'cause we don't need everything
that's going to be presented to us.
So for example, we're going to look at the file,
and we see that it gets us a lot of information,
and some of them might be unnecessary
for what we're looking for,
so we're going to condense that.
So the first thing we want to do is,
we're going to do sudo, we're going to grep,
we're going to search through this.
We want to look for password check, the fail.
We'll look in the var/log/secure,
and we see it returns only the instances
of a failed password attempt.
All right.
Now, we don't need all this information,
so what we're going to do is cut some of this.
And so that's where we're going to use the cut command.
And so I'm going to show what it does by itself.
Going to sudo cut, var/log/secure.
And this D is the delimiter that we're going to use.
So the delimiter that we're going to use is a space.
Delimiter meaning just something that separates fields,
or just say in the English language,
a space separates words.
So for this search that we're going to
use the delimiter, it's going to be the space.
So pretty much what's going to let us indicate
is which fields that we want to specify for the result.
So in this example,
we're going to specify fields five through 12.
And the fields again,
are associated with the spaces in between.
So now I cut off some of the beginning, and most of the end.
So we see that it's a better look for us.
Now let's combine those together.
So we're going to make a smaller output.
So we're going to do, sudo grep, new password,
again, that's what we're looking for.
Check failed for our log secure file.
Now again, now we're going to pipe this.
So pretty much send that information to the cut command,
and the cut command is going to look for spaces,
and is going to use fields five through 12 for the return.
Now look at that,
a much more clean output for exactly what we're looking for.
All right, we went over how to use the echo command,
and use the right bracket angle operator
to send information to a file.
The left angle bracket operator to send
that input to another command.
We also went through how to translate
a few files using the uppercase lowercase.
And how to condense information using the cut.
And how to show our redirection,
the output that is being sent
to the file using the T command.
And I'll see you in the next lesson.