Welcome to section seven.
In this section you will learn about commonly used Linux and AWS commands.
I've carefully chosen some commands that you will use the most while managing
your Linux hosts in aws. In this video,
you'll learn about 10 common Lennox commands that will allow you to explore and
manage your Lennox host. Okay,
so our plan is to go over these 10 commands and find out what you can use them
for.
You'll practice diving deep on these commands by using the man pages and help.
You'll run each command and learn about common use cases for them.
Okay, so there's clearly a lot of Lennox commands you could run on a host.
So I've kind of chosen down cut, I have chosen cut.
I've dwindled it down to about 10 commands that you use a lot.
All these commands can be altered by using different arguments and should work
across all distributions of Lennox.
I won't go into a lot of detailed here about each command as we'll explore them
later in the terminal. But here are the 10 commands. They are Ls,
cp, cat, MV, control r, rm,
find grip, tail and less. Okay,
let's jump over the terminal and get started.
Okay, so let's start off with the command. We've already been using Ls,
but instead of just showing you the command again,
which you've already done a few times,
I wanna show you how you can learn more about a command by using the man page
and help manual. So in your terminal,
let's go ahead and type man Ls and let's jump into the man page.
Okay, so here you can see, uh,
the man page is divvied up by these different sections.
You have the name synopsis, description, uh, et cetera. So we can hear,
we could see LS list directories.
You could see that we have a synopsis that kind of gives you how you can use the
command. And then there's a description.
And you could see we have all these flags here
of different things you could do. You can print the author of the file heroes,
like the dash dash author. You could do dash, dash color, um,
which happens to be the default for us. Um,
you can do just list directories by themselves, no contents. Um,
but we're gonna focus in on a couple here,
a couple flags that we were gonna want to do.
So what we want to do is we wanna do the H for human readable.
So we can see here in the man page the dash H or dash dash human dash readable,
print the sizes in human readable format, example like bytes, uh,
kilobytes, megabytes, uh, gig, uh, gigabytes, et cetera.
And you can see that, uh,
you have to h only works if it's using the dash L is
with it as well. So we, we want to do that one.
And then the other one we want to do, which is ignore. Um,
and as you can see here, this ignore pattern,
this is good if you do not wanna list, list implied entries inside the,
so if you're listing a directory, but you wanna ignore one file,
you can use this dash, dash ignore, or dash capital I, either one. Okay,
so in the man page,
it tells you right here at the bottom you can use Q for quit.
So let's go ahead and get out of there. I am going to clear the screen.
Okay, so let's test out the first flag, which was human readable.
So we're gonna do dash L dash lh and we're gonna do it right
here in the EC two users home directory.
And you can see here that this GG SH five,
if you remember from the earlier videos,
this was the guessing game that we created. Um,
it is 283 bites and that is all because of the H
flag.
And you can see the directory here of our campaign repos 217.
Okay, so let's try the uh, dash H human readable first.
So we're gonna use the dash h remember, we need to have the L in there as well.
And we have the two files in our e c two user home directory.
As you can see here, the gg, which is if you remember from uh,
previous videos, is the guessing game. And it is 283 bites.
And you can see our companion directory. Here's two 17. Um,
so that just gives you a size, which is nice if you're looking at stuff on this.
Now, let's say we want to do what we just did,
but this time we want to,
we want to ignore the GG fox. We, we don't, we don't really care about it.
So you, what you could do is you could do the dash, dash ignore equals,
and then what you want to ignore.
This could be a pattern or it could be the actual name of the file.
And in this case we wanna do the name of the file as you see when we now do an
ls.
And here I'll hit the up arrow one more time and then we're gonna do a dash l
here as well. So you get the long looks similar.
You could see that we are not seeing the the guessing
game file. Okay, lemme clear the screen.
The other thing that we want to do is I wanna show you that each command, most,
most all Lennox commands are gonna have a help, um, style menu.
Some may be dash h, some may be dash, dash help. You'll have to explore. Um,
they're not all consistent, but we see here we did Dashs, lh,
let me see how scroll up here. Uh, Dashs help.
You can see it prints a lot of what the, uh,
description was inside the man page. And it gives us these,
the flags and the other things, the arguments we can pass to the L ls. Um,
so you could use either one. If you wanna learn more,
more in depth about a command or something, I would use the man page.
If you just need a quick like, oh, I forget what that flag is to ignore files,
you could use the dash dash help and you don't have to go through as much.
All right, let me clear the screen. Let's move on to the next command.
And that next command is cp.
So the CP command copies files and directories as you already know.
But there are a few flags you can use to expand on CCPs usability. Um,
you can copy directory in its contents from one place to another while
preserving permissions,
which is important because if you're copying files from your,
that you created and you want, you're copying to a shared directory.
If you don't use the preserve, whatever that directory's uh,
ownership model may be, it may inherit if you're doing using it pseudo,
it may inherit those permissions.
Instead of preserving what you have on these files,
you can also copy, uh,
a directory in all its contents to another destination target.
So what we're gonna do here is, um, I'm gonna use the CP dash dash help command
if I can spell it right. And as you can see here, so we're getting a lot,
we're getting a lot what we would see in the man page.
One thing we're gonna focus on is here, we're gonna use the dash r.
And this case is gonna be copy directives recursively,
because we want copy to actually go inside a directory and copy all the contents
of the director as well as the directory. So we need a dash R there,
we have here, which is the dash P is preserved the attributes,
mode ownership timestamped if possible. So that is what talking about, um,
earlier.
And then the V we're gonna do is verbose so you can see exactly what it's doing.
Now a lot of stuff's gonna run by the screen, um, but that's okay.
So one thing that we we're, we're gonna want to do here is if you re,
if you recall here, I'll do an ls.
You can see that we have that our companion repository.
Let's go ahead and make a backup of this.
And so what we can do is we can say CP dash RVP for recursive
verbose and keep the permissions if possible. And then we're gonna do,
instead of calling that, we're gonna say backup.
So what this command says is we're gonna copy the original companion repository,
the hands-on Lennox for DevOps cloud engineers,
and we're gonna put it in another directory named Hands-on Lennox for DevOps
Cloud Engineers backup, which does not exist yet.
So I'm gonna hit return. A lot's gonna go by the screen,
but let's go ahead and do that. So as you can see with the v with the verbose,
it basically tells you that I'm copying this file from the
original source into the new destination.
You can see this one as the backup directory.
So let me clear the screen really quick.
So if you're not sure about clear screen,
you can just do control L as a shortcut.
And let's go ahead and do an ls.
And as you can see now we have both directories,
one's an exact copy of the other. Okay,
so the other flag that's interesting about CP that we can use is the dash I or
Interactive Flag.
And a lot of other file directory manipulating commands all have this
interactive and it allows you to like prompt you when you're doing something,
protect you from overriding stuff. For example, we see here we had the,
we saw the guessing game file. Let's do,
we're gonna do dash IgG sh,
and then let's just for the lack of creativity, let's just call it GG two sh,
okay, nothing happened, right? And that is because we'll not do ls.
That is because the GG two file did not, um, exist before.
So there's no reason for copy CP to actually prompt us.
Do you sure you wanna overwrite this file? And, and that is cuz it didn't exist,
which makes sense. So,
but if I clear the screen control L and I hit the up arrow till I
get to the CP dash I Gigi, and if I do this again, watch what happened.
CP now asked me like,
do you want to overwrite this file again because we're gonna copy it again.
Now of course these files are both the same, so it's not that big of a deal.
But now you could do the dash y and it's yes, and if we look,
everything is still there. So this is a, a good use case here.
If you want to copy files and you want to be safe,
even though it's a little bit, uh, it has to prompt you for it.
But this way you can be make sure that you're not overriding something you
shouldn't. Alright, lemme clear the screen. All right,
let's move on to the next command. So the third command is the cat command, um,
which you've already used and as you remember,
it can kanate files to stand it out.
But there are a couple of nice flags you can use to make it a bit more useful.
For example, if we do man dash cat, our man space cat,
one of the ones that I like to do is the dash N here on the description.
You can see right here and I'll, I'll just bring it up right here.
The dash N is the number all the output lines,
which I think is actually really helpful.
So if you're reading a big line and you're sharing it or you're trying to figure
out, uh, where something is on the line number, you could do that.
So let's go ahead and then we're,
we're gonna do is we're gonna do a dash n we're gonna use the cat command dash n
and then let's just look at the gg sh file the guessing game file again.
Okay. And as you see here, when you cat it with the dash n,
you can see now each of these uh, lines are numbered.
Makes it a little bit cleaner when you're talking, uh, about things.
The other thing that's really interesting about Cat as well is you can actually
cat two files at once. So if we do gg sh,
and then let's just pick, oh,
let's just pick the Readme that's in the, the companion repository.
You could see now
the guessing game file was catted first and it ends right here at the exit
zero. Okay,
so let's just cat the guessing game file from here and then let's,
oh, I don't know,
let's pick something in section four and let's do the file and directory
ownership owner permissions text. What this basically does,
it just shows you what each of the uh, when you do an LS um,
al and you're looking at the file directory owner permission,
this basically just gives you what each column represents. So as you see,
we're gonna print out,
we're gonna concatenate both these files out to the screen.
Okay? So what you can see here, okay,
so what you can see here is the first file ends at exit zero,
which is our guessing game.
And then the output of the second file is right below it.
So obviously very helpful if you're looking at two files and you're trying to
get information from both of them, you could cat it out, um,
and also you could add the line numbers here to get you both best of both
worlds. Okay, let me clear the screen and let's move on to number four.
The number four command is mv, which you've already used.
But one interesting feature you could do when using the move command is to use a
dash dash backup flag.
And this will create a backup of the destination targeted.
Cuz remember when you move a file,
whether you're copy is basically taking one file and renaming it to
another, but you're taking the contents with the two. So if that other file,
the destination or target file exists, you're basically overriding it.
So this is a nice feature as well to keep you from,
from accidentally overriding something you want.
So to do this we're gonna use a few commands we've already learned so far.
So I'm gonna test, I'm gonna create a test file and a test BK file for backup.
Okay, so touch just like the cat command and other units commands,
you can put two files that you want to create at the same time.
And if we just do an LS, you can see that they are there. Okay?
And then we're gonna,
we're basically gonna echo and if you remember from the bash series we're gonna
echo, echo means just it's going to uh, print something out,
but we're gonna use a little redirection. And then,
so we're gonna say new and then we're gonna say greater than.
And what this means is say echo the new string into something.
And in our case we want to echo that into the test file.
And so if you just cat test, you could see that it has new inside of it. Okay,
so we're gonna echo backup the word backup into the test
BK file. And if we do test cat test bk, you can say it's a backup.
All right, lemme clear the screen here again.
So we're gonna use the move command.
So what basically what we would wanna do is we're gonna move whatever's in test
into test bk.
And if we use the dash B flag here for backup,
it should go ahead and create a copy of our backup file
before it overrides it with the test file. Okay,
so let's go ahead and hit return. Okay, no errors, that is good.
So let's hit ls.
And as you can see over here now on the test bk there is the
test BK file. And then there's a test BK tilda in that. Let's go look at that.
So test BK should be what the new test file was.
So it should have contents of new. Now if we look at the test BK TILDA file,
it is a copy of the original test backup file.
Remember we echoed backup into that.
So this would be a good command to use if you ever need to.
You're moving files around a lot,
but there's some that you want to keep a copy of that you wanna roll back to in
case there's a problem. This is just an easy flag to use with the MV command.
All right, lemme clear the screen. Let's move on to command five.
Okay, so command five, while not necessarily a command per se,
is probably the command I use the most when I'm terminal and that's terminal.
I use this every day.
This will allow you to search your terminal history for past commands using
partial matches.
So to activate this search you can just dorl R and you can see now
it says reverse I search and I is for in case insensitive.
Let's look at the last command. We just did the move dash B oh,
you could see it already comes up and I could hit the return
key here and it would basically do that.
I could keep hitting control R and basically it'll keep going
through my command history looking for move dash commands.
It does a partial Mac. Now I could backspace, let's try cat.
So we can see that there's a manca.
If I do control R again,
I could see that it is doing other cat commands of other
times I've, I've typed this into our FA history.
So this is extremely useful. And then control C to get out of it.
And once you find the command, like for example,
lemme go do a control R and then let's do cat and I hit return.
Once I find the command, uh, due as a control r I can hit return,
it'll run that command. Uh, lemme clear the screen here.
So use control R when you wanna search your history,
you can't think of a command,
you can only think of partial matches and this will save you a lot of time
of um, having to look up Google or, uh,
stagger looking for how to run commands when you've already run it before.
All right, let's move on to the next.
Our six command is rm and let's go ahead and let's bring up demand page for
RM just for good practice.
And we can see here RM removes files or directories and
there is a lot of flags under the description.
Now this time there's an option section after description.
And you can see here there's a force prompt before every removal.
That's kind of like that interactive, the dash I there, um,
you can see right there and there's a recursive. So if you wanna remove,
so when you wanna remove a directory,
which we'll do you need to dash r recursive because you can't remove a directory
that is empty. That that, sorry, that is not empty. All right,
so we're gonna hit Q to get out of here and then let's go ahead and clear the
screen.
So let's go ahead and remove the backup directory here that
we made for the hands-on Lennox for DevOps, cloud engineers backup.
So to do that we're gonna use the RM command.
This time we're gonna use the dash R for recursive cause we wanna move all the
files inside the directory plus the directory and we're gonna use dash F because
we don't wanna be prompted for every file or anything that comes up.
So if we dash F, it just forces it, don't prompt me, just remove it all.
We know what we're doing. And then, so here we're gonna say backup.
So this command,
we're gonna remove all the contents and the directory for the backup copy that
we made earlier. So I'll hit return
and if we hit ls,
you could see now the directory is gone while the original still says okay,
so you will use RM minus rf, you'll use RM when removing a file,
um, as you have done previously.
They're both a command that you're gonna use often. Alright,
let's move on to the next command. Alright, let me clear the screen.
Okay, uh, command number seven is the fine command. Um,
this is great for finding files of directories inside your file system.
There is a plethora of flags that you can use,
but we're only gonna focus on like finding some files and directories plus,
we'll,
we'll add a little special thing in here where you can find files over specific
size. So find as its first option, we will take where you wanna search,
you wanna search locally and the local directory in,
you wanna search some other director in the file system and then it takes other
flags. But so like I said, we're gonna focus on finding a file first.
And so we will use the fine command,
we're gonna use a.as the first argument we passed through CZs is gonna say, Hey,
search in the local directory you're in.
We'll use the dash type here cuz we wanted to tell fine we're looking for a file
so we can, you know,
obviously if you just don't specify if it's file or directory,
it'll search both. But here we,
we can speed it up by saying we just want a file.
Then we're gonna use the dash name cuz we need the name of the file.
And then let's just look for guessing game right now in our local directory.
Go ahead and return. As you can see, it returns the file that it found.
Okay, so let's just hit the up arrow once and instead of the type F,
let's change it to type D.
And this is saying let's search for the type of a directory.
Now you would expect this to not return anything and if that's what you're
guessing you were correct because obviously the guessing game is a file,
not a director. But as you could see, if you want to,
to search for let's say an Etsy directory in the local directory,
that does not exist. But if you went,
but if you went to the slash you wanted to search in a different area,
like slash you could then you would return the um,
the directory itself. Okay, let's go ahead and clear.
I wanna show you something I think is, let me clear this screen right here.
I think this is an interesting uh,
command because sometimes as a Linux administrator or DevOps engineer,
you will get on a box,
you get an alert that the box is running out of disk space.
And one way that you can actually find this stuff is by using the fine
command. And let's say you wanna find all the files over a specific size.
And so in our case,
we are gonna look for files in the var log directory that are over,
let's just say 10 mags here we have to use pseudo because some of those logs in
that in the VAR log are, are uh, need elevated permission. So we'll do this.
So we'll do find bar spa, uh, slash log.
We're gonna say type
F and then we're gonna do dash size.
And then here we're gonna say plus cuz we want it larger then.
And then we're gonna use 10 meg. Okay, so when you hit return here,
as you can see we got two files that were returned to us. One is a,
they're both journal files from the journal system D journal,
and as you can see, so those files are both over 10 megs. Um, and here l let me,
we can go ahead and let me copy this and if we do the dash
LH command again choose to a human readable size, we hit enter.
We could see that this file is indeed over 10 megs. It's 72 megs.
All right, that was fine. Let's move on. Let me clear the screen.
Okay, our eighth command is gre.
So the GRE command is one of your go-to commands in your tool belt when you need
to search for something inside files inside like your source code,
you can use this for searching for failed os and uh,
the on like the secure log or like say you're searching an engine X, uh,
or Apache logs, you can look for non 200 s in there.
So there's lots of different use cases here.
So one of the things that we will look for first is basically searching,
using a pattern and we're gonna search the our vlog secure file,
which handles like the authentication stuff that comes in on Amazon Lennox.
So we're gonna use grip and then we're gonna use double quotes here because
there is a space in between the our search pattern that we're looking for.
And this is gonna be sessioned open and we just want to basically look for all
the, the lines, the log lines insecure that say SESSIONED open in them.
There should be quite a bit here. Um,
but it's gonna vary depending on your system on how many times you've logged in
and out of course. So we'll go ahead and hit enter. And as you can see here,
we have a lot of, um, output here,
especially all like the Pam Munich's session opened for roots. So this,
every time we use pseudo, as you can see here, it opens one,
the s SSH session here is opened up.
So there we dwindled it down to only what we were really looking for
inside a log that maybe,
so you could see if you had a log with tons of FI lines in it and you're only
looking for a a, a few certain things, GRE is a great way to dwindle that down.
All right, lemme clear the screen. So the next uh,
two flags that are important here with GRE is dash R,
which you remember from other news command means recursive and it means the same
thing here.
We want greep to search a whole directory and then you could use Dash I if
you want to do case insensitive, meaning like,
I don't know if this is capitalized or not capitalized. I don't care.
Show me both. So in this case, what we're gonna look for,
if you remember when we did the, and when we did Lenox security,
we ran a test PAM file every time we logged in V ss h,
we could search for that line in all of the Pam DOD directory and we could use
it as a partial search.
So let's do pseudo cuz we need elevated privileges cuz it's in the PAM
directory. We'll do dash RI and we're gonna say Test Pam.
We don't know what else we couldn't, you know,
we can't remember what else was on that line.
So we'll just do a partial match and then we're gonna say everything inside the
test Pam and then the PAM Dotd directory. So as you can see,
there it is and we found it in the S S H D file and
the colon just says the line it's on. But obviously that's great, but like,
let's say it was a big file and we, we wanted to see what, well,
what line number is this on? You can add the dash N in here. And as you can see,
it tells us now we're on line.
This line that had found the test underscore pam search criteria is on line
19. All right, let's clear the screen. Let's move on to number nine.
All right, the next command is use.
When you wanna output a number of lines from a file or you want to say
you want to follow a a log file, you, you're looking for debug errors,
you're looking for, uh, security implications,
and you just want to continuously follow the output of a file in real time.
You could do this with the tail command and let's go ahead and look at tail.
So let's do man tail
and it says output the last parts of a file. Um, as you see here,
there's a bunch of different flags here.
We are gonna focus on just two of these in the description,
the dash F or dash follow and the dash n or dash
dash lines equal. Um,
and so as you can see the dash follow just outputs appended data as the file
grows. So as you know, as new lines come to the bottom of the file,
you'll be able to see them in real time. Dash N says, Hey,
I want to output the last few lines of a file or this many, uh, lines of file.
The default is 10. And so we'll, we'll output,
they'll say like the last one or something. Okay,
so hit queue to get out of our man page, clear the screen we're,
and we're gonna use the secure log again.
So which means we'll have to use pseudo.
So we're gonna tail F and we're gonna follow a log. Now, once I hit return,
it's basically gonna, uh,
it's gonna block while it's waiting for new lines to come to the end of the
file. Um, as you can see here,
I can hit return a few times and it's just holding here with the cursor
blinking. It's because it's waiting for new, uh,
lines to show up in this vlog file. Obviously we can sit here and wait,
it may take a long time. So I'm, I'm not gonna do that to you, but just to know,
you can use the dash F to get out of the tail follow you use the control c
command to con get yourself out of it. All right, let me clear the screen.
And so the next one we're gonna do is let me uh, hit the up arrow.
We'll go back,
we'll get rid of the dash F and in this case I'm gonna say give me the last line
of secure log file. And as you can see here,
it only printed out the last line in the the secure file,
which happens to be the pseudo command I just ran, which, uh,
I did not plan that, but irony. So there you have tail,
very useful command when you're troubleshooting or looking for security related
issues, you will use it quite often. All right, let me clear the screen.
Let's head on to the final command. Okay,
so we've reached the end of our command and this last command is called less and
it allows you to tra file forwards and backwards.
It also lets you search and file inside the file as well.
So let's go ahead and we are gonna use the command less and let's go pick
a file here. And, um,
let's just do the lab terraform the read me file in there.
So when I hit return, it's gonna put me in a,
like a pager almost where it's gonna open the file and we're gonna be in the
file kind of almost very man page similar where here we are
in the file and as you can see we haven't, it didn't just spit it out.
And then the command end,
we are doing this and there's different commands that we can look in here.
So for example,
we can do this forward slash here like I've done in the terminal and I can
search from the top down through the file and then let's search for the word
key, hit return. And now it finds the first, second, third,
and all that. Basically they're highlighted here. Um,
in case it's maybe not showing up your screen, it's recognizing all the,
the words with key in it. Maybe they're, you know, it different things.
Like here it says create SSH keys.sh and the word key is obviously in that these
are partial matches as you can imagine. So if you hit the end key,
it'll keep searching for that key from top to bottom to keep going all the way
down. And so if you just keep doing it, it'll keep finding key,
keep hitting end.
And if you do all the way until you hit the bottom of we've hit the bottom of
the file and you can see here it says pattern not found. Okay,
well we searched one way from top down,
we can now search the other way back up and you could do that with the question
mark. So you could do shift question mark.
Now let's search for the AW w s command.
Now this is search from the bottom of the file which we had reached all the way
to the top. And as you can see, this will, uh, you can see right here we found,
uh,
one instance of the S SSH AWS term and if I hit N it'll keep
searching up to the top of the file. And if I hit one more time,
now we've reached the top of the file and now we can't look any further.
So we've done everything we can.
So to get out of less you can hit the Q command.
And so now we're out of the less things. Another thing that you can do,
which is kind of like cat,
is that you can open up two files unless at the same time.
So let's open up the guessing game file and then let's look on hands.
Uh, let's just go to section four something and let's look at the password
database file. Okay, this is just a helper file, uh, that I've provided.
So you can see what the format of the, that the pass Etsy password file is.
So if I hit return,
you could see that we've opened the guessing game first and it tells us here at
the bottom file one of two and then, then tells us what the next file is. If
you do the shift colon n command,
it'll bring you to the next file in the list. And if you do shift colon p,
it'll bring us back to the first file in the list that we are on ls.
So it's a good way if you need to open up two files and you're looking at, um,
looking at you need something from each of the files,
this is a good way to go back and forth and kind of see that. Okay,
hit Q to get out. Let me clear the screen. Alright, so there we have it.
There's our 10 most common commands. Let's head back over and do a recap.
Okay, so let's talk about what we did in this video.
You learned about frequently used commands in Lennox. We went over 10 of them.
You practiced finding information about some of these commands using man pages.
Finally, you ran each command and learned how to use them.
In the next video,
you'll learn about some common AW S C L I commands that can duplicate what you
did in the AW S web console earlier. See you soon.