Skip to content
CBT Nuggets
DemoBook a Demo

Understand Linux CLI Fundamentals

The skill 'Understand Linux CLI Fundamentals' covers essential Linux command line operations, including directory navigation, file manipulation, and text processing using tools like grep, sed, and awk. It emphasizes scripting and automation to streamline IT workflows and manage enterprise systems. Learners will explore infrastructure as code concepts, build CI/CD pipelines for automated testing, and focus on secure deployment practices. The course provides practical lab environments to reinforce these skills, ensuring a comprehensive understanding of Linux CLI fundamentals.

Full skill from CompTIA AutoOps+ (AT0-001). Preview the IT training 23,000+ organizations trust.

57m

Skill 1 of 33 in CompTIA AutoOps+ (AT0-001)

Introduction to AutoOps+

In this video we discuss what we can expect from the AutoOps+ examination. We briefly talk about the core skills tested and walk through the creation of our lab environment. Check it out!

Knowledge Check

The course recommends setting up a home lab environment using Ubuntu installed on a virtual machine

Listing and Changing Directories

Now that we have our lab environment up and running, let's get to the fun stuff! In this video we begin learning the commands on the Linux command line interface (CLI) to change directories and list their contents.

Knowledge Check

Which command is used to print the current working directory in a Linux command line interface?

Exploring Other Commands

Let's now explore some more common Linux commands! In this video we discuss how to perform the following tasks:

  • Creating directories
  • Deleting directories
  • Creating files
  • Copying files
  • Moving files
  • Echoing text and redirecting outputs
  • Reading the content of files

Check it out!

Knowledge Check

Which command allows you to create a directory along with any necessary parent directories in Linux?

Filtering Text with Grep

Now let's turn our attention towards using one of the most powerful and useful commands in the Linux CLI - grep. This particular utility allows us to filter text data in a variety of different ways. Let's have some fun and explore!

Knowledge Check

Which command allows you to search for a pattern in a file while ignoring case sensitivity in Linux?

Sed and Awk

In this video we learn how to use two more popular command line tools - "sed" and "awk". Sed is a stream editor and allows us to manipulate text streams in a powerful and flexible manner. Awk is also a text processing tool, but specialises in managing "structured" data - namely, the type of data you would see in logs or spreadsheets.

Knowledge Check

What does the '-n' option do when used with the 'sed' command in Linux?

Using the Pipe

Finally, the last tool we will explore in this skill is the use of the pipe utility. The pipe allows us to chain together multiple different CLI commands in unique and expressive ways. As such, it is one of the most powerful tools on the Linux CLI. Check it out!

Knowledge Check

What does the pipe symbol '|' do in a Linux command line?

Validation

Let's now validate what we've learned within this skill!

Instructions:

Launch the lab below. Once loaded, double click on the Ubuntu1 shortcut on the desktop. This will open an Ubuntu virtual machine. Next, open up a terminal window and perform the following actions:

  • Print the current working directory
  • List its contents
  • Create a folder called "validation-folder" and change into this directory
  • Create a text file inside this directory called "validation.txt"
  • Use the nano text editor to edit this file and enter the following text:

Learning Linux is fun!

  • Next, save the content and exit the nano editor
  • Use grep to search the content of the text file for the presence of the word "linux" - ensure that the search is case insensitive.
  • Next use sed to substitute the word "fun" for the word "awesome" - ensure that the content inside the text file itself is modified.
  • Finally, navigate to the home directory and delete the "validation-folder" directory - including all of its content.

Check out the video solution below!

Knowledge Check

How much experience do you have with the Linux CLI?

This interactive assessment is available in the full learning experience.

Want to answer questions like this yourself?
with no purchase required. Already have an account?

View Transcript

Introduction to AutoOps+

0:00Hey friends and welcome to the AutoOps Plus course here on CBT Nuggets.

0:05Alrighty oh so what I want to do in this video is just to kind of tell you

0:10about some of the things you can expect within this particular course

0:14as well as how you at home can set up your lab environment

0:18so that you can more easily follow along with what I'm doing

0:21myself on my own system. Now that said what I will say is that this course is

0:25going to provide to you a lab environment whereby even if

0:29you do not have your own home lab set up you will have access

0:33to the labs within CBT Nuggets to practice all the commands

0:36and all the configurations that we go through. However I do always recommend

0:40you have your own home lab environment because it gives

0:44you a little bit more freedom a little bit more flexibility and

0:46access to play with things and to really master

0:50all the concepts that we're going to be talking about. Okay so with that said

0:53John shut up let's talk about what we're going to be learning within this

0:56particular course. So we can see here straight away the

0:59skills that we're going to be learning as it's outlined via CompTIA as we're

1:02going to learn to use scripting and automation that's

1:06going to help us streamline our IT workflows manage enterprise

1:10systems. Sounds pretty fun we're also going to learn how we can automate

1:13configuration provision and maintenance using infrastructure as code and modern

1:19toolchains that we'll talk about what infrastructure as code actually

1:22means later on within this course but suffice to say it's a cool

1:26concept and it's very very fun to work with. We'll also be doing things like

1:30building CI CD pipelines to automate testing ensure that we

1:33have quality and improve the collaboration and

1:37workflow between our teams and of course like anything we

1:40always want to be automating and managing our infrastructure securely so

1:44we're going to have a focus on secure pipelines allow you to deploy

1:48your configurations in a safe and efficient manner. So by all

1:53means I would encourage you to go to the CompTIA website look up the

1:56AutoOps Plus course and look at all the exam details

1:59and basically if you go to the website you're going to get a lot more

2:02detailed information about the things that you can expect within

2:06this examination and in fact if you go down to the bottom of the page you will

2:10see that we actually have the AutoOps exam objectives now as I

2:13record this the exam has not yet been released so

2:16this here is just a draft version but perhaps if you're watching this in the

2:20future you will see the final objectives

2:23actually printed on here so again if we click this

2:25what it's going to do is give you a full breakdown about all the technologies

2:28and all of the objectives within the examination itself

2:32so we can see we have tons and tons and tons of stuff to get to

2:36all of which is going to be a lot of fun now what I will say though is that in

2:39this particular skill what we are going to be focused in on

2:42are command line utilities okay now the reason why we are actually

2:46starting with this is because in my opinion this is the foundation of

2:49everything that we're going to be doing so with that said before we begin

2:53diving in to the actual hands-on configurations on the command line

2:57what I want to do is just to tell you a little bit about this lab

3:00environment alrighty oh so what I will say is that

3:03with respect to the configurations that we're going to be doing

3:07the examples I will be showing you will be built on

3:10Ubuntu so Ubuntu is a Linux based environment

3:13and quite honestly the commands you're going to be seeing here will be

3:16applicable across pretty much all Linux distributions but that said Ubuntu

3:21is a nice friendly distribution so I kind of feel there is a very good

3:25entry point so what I would do is I would suggest you go

3:27and go to the website click on download Ubuntu

3:30and what I will say is you can install this directly in a bare metal

3:34environment but what I will say is that my strong strong recommendation

3:38is that you install this within a virtual machine

3:41the reason being is because when you're dealing with these commands when you're

3:44dealing with all these new tools you can often make

3:48well you can make mistakes and it can basically kind of break your system

3:51and quite honestly that is not going to be fun whereas if we have a virtual

3:55environment if you break something it's easy to

3:57just roll back into an earlier snapshot and get back to

4:00your safe configurations so basically that is my recommendation

4:04I suggest you download an Ubuntu machine and quite honestly

4:07my real recommendation is to download two of these virtual machines actually

4:11because what that means therefore is that we can actually test the

4:15communications between two different devices now what I will say is that for

4:19the vast majority of this course you're going to have a main machine

4:23one of these VMs is going to be the one that you do pretty much all of

4:26your configurations on okay so you don't necessarily have to have two

4:30but just in the corner cases whereby we want to have a little bit of networking

4:34a little bit of communication it's going to make things a little bit

4:37easier now that said again I stress if you only just have one machine in

4:42the lab environment that I have built for you in this

4:45particular course you will have access to two different machines so again as a

4:49fallback you can always use those two and those

4:51will be two Ubuntu machines just like I'm recommending in this

4:55particular video so friends with that said

4:58with all that said let's now in the very next video get to the fun stuff and

5:02begin using the Linux command line interface

Listing and Changing Directories

0:00All right, so let's get to the fun stuff.

0:01So what I have here is my Ubuntu virtual machine.

0:04All I'm going to do is take it down to the bottom left of the screen.

0:07Click this right here.

0:08So it's going to go here and I'm going to search for terminal window,

0:11which will give us our little command line box.

0:14And all I'm going to do here is to full screen this.

0:16All right, so the first thing we want to understand is

0:18whereabouts are we in the actual directory structure?

0:21And we want to be able to move through that directory structure

0:24just like if you're on the graphical user interface.

0:27You can, you know, open up a folder like so

0:29and you can click into these folders, you know, double click this here

0:33and you know, you can select a file or drag and drop files, that type of thing.

0:36We want to be able to do this within the actual command line interface.

0:39And again, understanding where we are is very, very important.

0:43So the first command I'm going to look at is one known as PWD.

0:47This is print the working directory, meaning that

0:50show me the directory that I'm in right now.

0:52So let's go and see this.

0:54Since if I just say PWD and hit enter, this is the directory structure.

0:58Now, my little name here is just ipv0.

1:01This is the name that I use online for my GitHub, my YouTube, that type of thing.

1:05And basically, this is the username that I created on the system.

1:09So what I have here is my home directory.

1:11This is the full path home and then the name of the user.

1:14So you're at home, you should see the home directory and then your own username.

1:18So that shows us whereabouts we are.

1:21And if I want to see the contents that are inside of this directory,

1:24what I want to do is I want to list the contents.

1:27And the way I can do this is by pressing the ls command.

1:30Now, if I do this, we're going to see here,

1:32these are all the folders that are inside this particular directory structure.

1:36So inside my home directory, I've got a desktop folder,

1:39documents, downloads, music, so on, so forth.

1:41Now, before I actually go on to the next command,

1:44let me show you what we can do here.

1:45We can also use a particular switch,

1:48meaning that we can ultimately modify the impact of this particular command

1:51by just adding in some additional options.

1:54Now, before I do this, I just want to go briefly in

1:56and just show you this particular page.

1:58See if I say man and then specify the actual command that I want to be using,

2:03I will look at the manual page for that particular command, okay?

2:06So if I say man and then say I want to see the manual page for the ls and hit enter,

2:11it's going to go and tell me what this particular tool does.

2:14So it's going to list content directories.

2:16And then we see all of these additional options right here.

2:19So again, if I just scroll down, for example,

2:21and check this out right here, if we do a dash lowercase l,

2:25this is going to use a long listing format.

2:28So let's press Q to quit, go back to the page.

2:30And if I just do ls dash l and to enter,

2:34once again, we're going to see the content of the directory,

2:36but we're getting a longer listing.

2:38We're seeing when the directory structure was created.

2:41We're seeing the permissions on the left-hand side.

2:44We're seeing the owner and the group to which that directory belongs.

2:48And we're seeing information about the size of the directory as well.

2:51Now, if I wanted to move into one of these directories,

2:53say, for example, go into, let's say, the documents directory,

2:57what I want to do is I want to change directories.

3:00And the command for that is going to be cd.

3:01So let's say cd and then give the name of the directory.

3:04So I can say cd documents.

3:06And what you will notice, by the way,

3:08is that once I have typed enough letters,

3:10I can just hit the tab button,

3:12and it's going to go and autocomplete the rest.

3:14So if I hit enter here,

3:16I've now changed into the documents directory,

3:18and I can confirm this with the pwd commands.

3:21If I enter here, we can see I'm actually inside documents,

3:24which is inside my home directory.

3:26Now, if I want to go back a directory,

3:28move back to where I was,

3:30I can say cd space dot dot.

3:32That is going to allow me to jump back at a level, okay?

3:34So if I hit enter and do a pwd,

3:37once again, I'm back in my home directory.

3:39Now, let me show you something that we can do here.

3:40We also have this concept of an absolute path

3:43and a relative path.

3:44So when we're talking about an absolute path,

3:46we're talking about providing the full directory structure, okay?

3:50Whereas, if we happen to specify the relative path,

3:54we specify the path relative to where we already are

3:57in the directory structure.

3:59Now, again, that might sound a little bit odd,

4:00so let me go and tell you what I mean.

4:02So right now, if I go and do cd documents,

4:05and to enter, what I just used was a relative path.

4:08I could just shortcut it to say documents

4:10because I'm already inside home ipv0.

4:14So again, if I go back and I do pwd,

4:16what I could also do if I wanted to give the absolute path,

4:19I could say move into home ipv0 and documents like so.

4:24And again, I'm back in that same directory.

4:26And again, I can do cd back back.

4:27There we go.

4:28Now, it might seem a little bit strange

4:29as to why you would ever want to do the long version,

4:32you know, specifying all of these additional directories, okay?

4:36But ultimately, it just gives you a little bit of clarity

4:38and certainty depending on whereabouts you are.

4:40So say, for example, I do cd forward slash.

4:42What this does, it takes me to the root

4:44of the directory structure.

4:45So if I go and do a listing here,

4:48this is me at the very root of the tree.

4:49Now, inside of here, we have all the folders

4:52that exist on the file system itself.

4:54So what you'll notice is that my home directory

4:56is in, well, it's in the home folder.

4:58And inside that, we have a directory for my own username.

5:01And we can see here, this directory is right there.

5:04It's in home, okay?

5:05So in fact, you know what?

5:06Let me do a long listing.

5:06So say, for example, I went into,

5:08or don't know, let's just maybe say

5:09I go into the temp directory, okay?

5:11So down here, see this one right here?

5:13Let's go inside here.

5:14So if I just say forward slash tmp, cd into that,

5:18I'm now inside this directory.

5:20But now let's say I wanted to go

5:21into the documents directory inside my own home.

5:25Well, relative to where I am,

5:26it may just be easier to specify the full absolute path.

5:31Say, for example, let's say I don't care

5:32where I am right now.

5:33I just want to go to this exact place.

5:35So if I do this right here, it doesn't matter where I am.

5:37I could do a pwd, and I'm now back in.

5:40Again, let's repeat that process.

5:41If I do cd forward slash,

5:43and I go back into the temp directory,

5:46what I could do now is give the relative path,

5:47which would mean I could say cd.

5:49I have to go back a directory to get me to the root,

5:52and then say forward slash home,

5:55forward slash ipv0, forward slash documents.

5:58So as you can see in this case,

6:01the relative path is actually longer

6:02by having to supply these additional characters, okay?

6:06That's right here.

6:06So let's just go and hit enter, see where we end up.

6:08Again, as we're gonna see here,

6:09we're now back in the same position.

6:11Now, a little shortcut that you can do

6:13if you want to go back to the previous directory

6:14you were in, regardless of where it is,

6:16if you do cd and then the hyphen,

6:19that'll take you back to the previous directory.

6:20So that was gonna be in the temp directories.

6:22If I hit enter here, we're gonna see here,

6:24I'm now back in tmp, like so.

6:26Now, another good shortcut you can use

6:28to get back to your home directory

6:30is just by typing the command cd.

6:32So regardless of whereabouts you are,

6:33say for example, I do cd back back.

6:36So say for example, I go into the user directory

6:38and then go further deeper.

6:39I go into lib, do a listing,

6:41now go into, I don't know, let's say locale, do a listing.

6:44As we can see right here,

6:45I'm getting deeper and deeper

6:46into this particular directory structure

6:48inside the user directory,

6:50more and more of the subfolders.

6:51Now, as opposed to me having to go cd back back

6:54to go into the previous directory

6:56and then back back and then back back

6:58and then saying home ipv0,

7:00as opposed to that, if I just say cd,

7:02regardless of where I am and specify nothing else,

7:06it's gonna take me back to that home directory.

7:07So it's an easy way to get back

7:09to where you want to go to.

7:10And again, if I just do cd hyphen,

7:12takes you back into the previous directory,

7:14which is this one right here.

7:15And again, just to be clear here,

7:17if I just want to go into my documents directory,

7:20if I just try to do cd documents

7:22and to enter, we're gonna see here,

7:24there is no directory inside

7:26of the locale directory called documents, okay?

7:29So it's gonna give me an error.

7:30So this is why you have to supply the full path

7:32or give the correct relative path

7:34depending on whereabouts you are in the system.

Exploring Other Commands

0:00Alright, so let's go and check out some more commands then. So what I'm going to do here is I'm going to go back to my home directory and just press CD.

0:06Alright, so here I am back here and if I do a listing we can see all my directories.

0:10Now what I want to do is I want to go and create my own directory.

0:14Now the way I can do this is via the mkdir command and again if we want to see how to use it we can say man mkdir and to enter and it says here this is the command to make directories.

0:24And again if we look at the options we see we have these additional switches such as the dash p flag.

0:29So say for example I use this I can also specify the parent directories that I want to be created.

0:34So we'll get to see what that looks like.

0:36So inside of here I will say mkdir and then give the name of the directory that I want to create.

0:41So let's just call this one testdir, okay.

0:44So if I hit enter and do a new listing we're going to see here I now have this new directory structure right here.

0:50Now again we just saw we had the dash p flag this allows us to create multiple different sub directories at the time of creation.

0:57So again if I just said say for example create directory let's just maybe call this one I don't know let's just say bladder okay I'm running out of ideas.

1:06And inside of this I tried to create a smaller folder let's just call this one subfolder 1 and then maybe create a directory inside that called subfolder 2 that type of thing.

1:15If I hit enter here we're going to get an error.

1:17However if I arrow back up and I just do the dash p flag to allow me to create the parent directories.

1:23If I hit enter now it's going to be accepted and if I do a listing we're going to see here we have the bladder directory so I can go into it.

1:30And if I do a listing we have folder 1 inside or subfolder 1.

1:34So I can go inside here and do a listing we have a new folder inside and that is going to be subfolder 2.

1:39So we're going to see here this is now where I am in the directory structure.

1:43So once again if I just do cd go back to the home directory there I am right there.

1:47Now right now we've created some directories if I want to create some files inside these directories I can use the touch command.

1:54So say for example I go into the test directory like so do a listing we don't have anything.

1:59But if I say touch and just call this one file1.txt and to enter do a listing we have now created that file right here.

2:06And again how can we tell if it's a file or a directory?

2:10Well if we use the long listing by doing dash L we're going to see here at the very end when it happens to be a file we're just going to notice a little dash right here okay.

2:18Whereas if I do cd dot dot to go back do a listing if I do a long listing notice this right here okay.

2:25All these objects bladder all the way down to videos these are all directories because at the very end they have this d as opposed to the hyphen.

2:32And again you can see this quite clearly if I create another file inside the home directory this time and to enter do a long listing.

2:39We're going to see here this one here doesn't have the coloration and at the end it just has the hyphen so it is a file it's not a directory.

2:46So let's just say I wanted to copy a particular file and move it into another directory.

2:51Well right now we have this one right here example.txt let's say I wanted to leave it there but also make a copy of the same one inside let's maybe go with the let's say the pictures directory.

3:02Well I can say cd or cd cp should I say for copy then give the name of the file that I want to copy and then specify whereabouts I want to copy it to.

3:11And again the concepts of absolute paths and relative paths are still at play here as well.

3:16So if I want to just be using relative paths I can just specify the name of the file directly or I could say forward slash home forward slash ipv0 forward slash example.txt.

3:25In this case here we're inside that directory so I'm just using the shorthand.

3:29Where do I want to copy again I can just use the relative path and just say I want to copy into the pictures directory.

3:36So if I enter do a listing the file is still here as we can see however a new copy of it should also exist inside the pictures directory as we see right here.

3:45Now if I want to delete a file i.e. remove a particular file all I have to do is use the rm commands.

3:51If I say rm example.txt do a listing suddenly it's been deleted.

3:56If I do cd back to my home directory listing as we can see the original version still exists but we deleted the copy of it right here.

4:03Now if I wanted to move a file as opposed to copying it I could use the mv commands.

4:08I can say mv example and then specify where I want to move it to so let's maybe move this into the templates directory like so.

4:15So if I hit enter do a listing we're going to see here it's gone because it now lives inside this particular directory like so and if I do a listing there it is right there.

4:23Now again if I go back to my home directory just by saying cd so here we are right here.

4:28If I do a listing let's say I wanted to delete a folder okay so if I just said rm on tester for example and to enter it's going to give me an error saying this is a directory.

4:40So there's two ways in which we can solve this we could say rm dir and just say tester so that means remove directory.

4:46If I hit enter what you're going to notice is that it's not going to allow me to delete this directory with this command because the directory is not actually empty.

4:54So if the directory is not empty what I want to do is I want to say the rm command but I want to use the dash r flag for recursively delete.

5:03So what that means is delete everything and everything is inside it so if I now say delete tester like so it's going to delete directory as well as everything within it.

5:13Whereas just the rm dir command if the directory happens to be empty so say for example I create a new one just called let's just call this john dir there we go right there.

5:21It's not got any files inside of it it's not got any subfolders in it so it is just empty so if I do rm dir on john dir and to enter we're going to delete it no problem at all.

5:31Now another command we can use to ultimately print text onto the terminal is I can use the echo command so if I can just say echo hello my name is john and to enter what it's going to do here it's going to take this and return it back to me on standard output.

5:46Now the cool thing about this is what we can do is we can actually use this and redirect to output so that it doesn't go on to standard out but instead it goes inside a particular file.

5:55So let's just go and create one called I'll just say greeting.txt do a listing so there is a file right now it's not got anything inside of it but what I could do is I could say echo hello my name is john mcgovern pleased to meet you.

6:10And as opposed to just you know printing it out like this instead if I actually just take this command and I use the right arrow what it says here is take this particular input and I want you to put it into whatever file you specify and again you can use relative or absolute paths.

6:27In my case here I will just use the relative path so put it into this particular file right here if I hit enter you'll notice it didn't actually print back to me instead the content now is inside this particular file.

6:39So the way we can open particular files is by using the cat command for concatenate if I just say cat and then say greeting.txt we should see what is inside this particular file and that's what it is right here this is now written inside of this.

6:51And if I happen to arrow up and delete this write in some new content so I'll just say hey my name is Simona.

6:58If I hit enter here you notice the content goes into the file again but the thing is this here has now overwritten what was previously in this particular file so if I happen to cat it again we're going to see here the old information is deleted that has been replaced with the new content.

7:12Now if I want to append to stuff that is already inside the text file as opposed to overwriting it I can use my double arrow so check this out if I now arrow up and I just do double arrows and I just say I live in the United Kingdom.

7:26If I hit enter here again it doesn't print on the screen but if I happen to look inside this file again and to enter as opposed to this line just overwriting the top one instead is appended to on a second line.

7:38And again as always if you're curious about particular commands by all means I encourage you to read the man pages like for remove as it says right here it tells you what the description is what it actually does and we can see here all of these additional options that you can use with this particular utility.

Filtering Text with Grep

0:00Alright friends, let's check out more commands. So in this case here, what we're

0:03going to do first is to look at a text editor. So a built-in text editor within

0:09the Ubuntu system is one called nano. So if we just say nano and then give the

0:13name of the file we want to be opening or creating, it's gonna effectively open

0:17up and allow us to type away as we wish. So let's check it out. If I do a listing,

0:21what we can see right here is we have our greeting.txt. If I want to open up

0:25in the text editor, I can just say nano greeting.txt and enter and this opens me

0:30into this text editor and I can just use my arrows to move around like so and if

0:35I want to type away, I can just press return to get a new line and just say

0:38this is just some more text and I can just keep adding this as I ordinarily

0:43would with a text document, okay? Now if I want to save this, what I'm gonna do is

0:46hold down control and then press the letter O. Now we're gonna see here it's

0:50gonna say what is the name of the file you want to write. Automatically it's

0:54gonna keep the same names. I'm not gonna touch it and just press return. When I do

0:58this, it's now written as it says down here and if I want to escape and leave

1:02the text editor, I can just press control and then X. So now I've modified this and

1:06if I open up the file and look at it, as we're gonna see here, I've now added this

1:10additional stuff. So now that comes to us using what is known as the grep

1:14commands. Now grep allows us to search for particular patterns in files or in

1:19particular inputs. So let's say we're trying to look for the name Simona in

1:22this text file. Now I know clearly we can see that it is there, it's very

1:26obvious, but you can imagine this is, you know, maybe thousands of lines long, okay?

1:30We're trying to find something. So what I can do here is I can just say grep and

1:34then I specify the pattern I want to be looking for. So I'm trying to find

1:37Simona. So I'll put that in my quotation marks and then I'll specify the target

1:42that I want to be searching through, which in this case here is the greeting.txt

1:46file. So if I hit enter here, as we're gonna see here, it's gonna go and

1:49highlight any instances of that particular match. So again, if I happen to

1:53open this up again, okay, go back in here and let's just add in, let's just say

1:57Simona loves working with Linux. Again, I just write it out by pressing

2:02ctrl-o, hit enter, ctrl-x to escape. If I rerun the patch, or not the patch, not the

2:09patch, the pattern match, it's a bit of a brain fog there, okay? Let's try to run

2:13this again, hit enter. As we're gonna see here, we have a match on this line and we

2:18also have a match on that line. Now notice this, okay, it's actually printing

2:22out each of the lines and not just the word itself. So we can see here, this line

2:26prints out and this line, and obviously we can see we get the highlighting, but

2:30notice the middle line in the original text document, I live in the United

2:34Kingdom. That is not present, okay? So we're just seeing the match and the

2:38corresponding line that the match resides on. Now one thing to note here is

2:41that the grep utility happens to be case sensitive. So if, say for example, I go

2:46here and I say I want to search for Simona, and I hit enter, it's gonna say

2:50well I didn't find anything like that. Now I just said grep is case sensitive,

2:55but that's not entirely true, it's just case sensitive by default. If I use a

3:00particular option known as the iflag, that is going to go and ignore case

3:04sensitivity. And again, how would I know this? I could say man grep to look at the

3:09utility, and as we're gonna see here, this is going to print the lines that

3:12matches patterns, and if I look at the options and just scroll down, look at

3:16this one right here, dash I ignore case. So let's go and try this then, okay? So if I

3:20try this again, and I do dash I, I'm saying I don't care about the case. As

3:25we're gonna see here, we're now actually getting the matches. We're not actually

3:29seeing the line numbers themselves, okay? So if I want to also see this, I can use

3:33the dash nflag. I will say search case insensitive, show me the line numbers for

3:38any matches of Simona in the greeting.txt file, okay? So if I enter here, it's gonna

3:43say here, this is the line that it's on, this is the actual match has been

3:47highlighted, it's on line one, and we've also got another match on line four, and

3:52this is the line in which that second match also resides. Now say for example I

3:56wanted to just count the amount of matches in a text document, what I could

4:00do is I could arrow up, and this time here I'm just gonna say grep for Simona,

4:04and I'll say do it case insensitive, and I want you to count the amount of hits

4:08that you actually get. So if I hit enter, it's gonna say I found two of these

4:11within the actual text documents, whereas if I happen to arrow up and remove the

4:15case insensitivity, and to enter it's gonna say I found nothing because we're

4:19now searching on case sensitive once again. Now let's say for example I wanted

4:23to do an inverted match, meaning that I want to find everything that does not

4:27match this particular pattern. So again, just before we begin, let's look at the

4:31text document. Okay, so just say greetings, there it is right there, I've got four

4:35lines. Let's say I wanted to see all the lines in which Simona is not actually

4:40present, so what I could do here is I could say grep, let's do it case

4:44insensitive again, and this time I'm going to say dash V, okay, that's gonna

4:48allow me to invert the actual logic here, and I'll say I'm gonna search for

4:51Simona, and I'm gonna target the greeting.txt file once again. If I hit

4:56enter, it's gonna show me the two lines in which Simona is not present, and again

5:00if I happen to arrow up and remove the case insensitivity, we should have no

5:05matches for Simona, that means we should print out all four lines as we can see

5:09right here. Now let's say I wanted to just search for the actual word itself

5:13and not show the entire line, okay, so let's say grep, again let's search with

5:16ignoring the case, and now I'm gonna say dash O, that's gonna say only the actual

5:21match should be shown here. So again I'm gonna look for Simona inside the

5:26greeting.txt file, if I hit enter here, it's just gonna show those particular

5:30matches and not the entire line, and again if I want to see the lines that

5:33they happen to be on, I can say dash N for the line number, if I hit enter here

5:38it's gonna say look, Simona is found on line 1 and line number 4. Now check this

5:43out, if I wanted to perhaps maybe have another particular file, another text

5:47file, let's just say nano, and I'll create a new one just called, let's just say

5:51secondexample.txt, okay, I'll just say my name is John, just say I love Linux, and I'll just also say

5:59and I also love Python. Okay so ctrl O to write, ctrl X to escape, so we can see

6:04here this is the content of this particular file, we also have the

6:07greeting.txt, okay, so let's just search for any instances of the word Linux, but

6:11we want to search in two different files, okay, so what I could do is I could say

6:15grep for the word Linux, and I want you to search inside the greeting.txt file

6:21as well as the secondexample.txt file, if I hit enter, look at what it says here,

6:26okay, it shows me inside greeting.txt, this is the actual match right here, and

6:31inside this particular text file, this is the actual match, and again we can keep

6:36adding our flags, I can say, you know what, I also want to see the line number, okay,

6:40so I hit enter, we're gonna see here, it's on line 4 of this particular file, and

6:44it's also on line 2 of that particular file, but it gets better, as opposed to me

6:50actually specifying explicitly the files I want to be using, I can also use

6:54a wildcard, what I can do here is I can say grep, show me the line number, I want

6:58to search for Linux, but the thing that I want to be searching is anything that

7:02ends in .txt, so all this represents is anything, so look at the naming

7:07conventions, this here, okay, greeting.txt, it ends in .txt, see that right there, so

7:13this part here, we're saying it doesn't matter what that looks like, or what it's

7:16spelled like, as long as it ends in .txt, we're gonna include it, same with this,

7:20also ends in .txt, so by virtue of just shorthanding this, we're actually

7:25going to be searching these two files, if I hit enter here, then it goes right there.

Sed and Awk

0:00All right, so we have a few more commands to look at and the next one is going to be the

0:04SED command. So let's go and check it out then. So all I'll do here is clear my screen

0:08and I'll say man for the SED command. So we can see here this is a stream editor for filtering

0:15and transforming text. So let me go and give you an example of that. Okay, so if I press

0:19q to quit and do a listing, let's once again see what we have in this greeting.txt file. Okay,

0:25so check this out. If I just say SED and within my quotation marks, I say 1p, okay,

0:32and then I'm going to specify the name of the file that I want to be operating on. So check

0:36this out. If I hit enter, look what's happened. Okay, what you may notice is that we have a

0:40duplication on the very first line. Now the reason for that is because when I said SED,

0:44I'm saying I want you to print out the first line in this particular file, which is what it's done

0:50right here and then automatically it goes and prints out the entire contents of that file,

0:55okay. Again, if we just add it up and I say let's go with 2p, what do you think it's going to do?

1:01If I hit enter, what it's going to do, it's going to go and print out the file as well as

1:05the second line as well. Okay, so we get a duplication right there. Now in this case

1:08here, if I just want to only print out that particular line, I can say "-n". And again,

1:14the way I know this is if I go into the man page and I just say "-n", what it's going to do,

1:18it's going to suppress the automatic printing of the pattern space. Okay, so as opposed to

1:22seeing everything, we're just going to get the match. So now if I just say SED-n 2p, I'm just

1:28saying print out the second line in this particular file right here, which is going to be I live in

1:33the United Kingdom. Arrow up, let's try 1p, as you expect, it's just going to be printing out

1:37the very first line in this particular file. Now say, for example, I wanted to use another

1:42expression, let's just say 1d and I just target the particular file, what do you think is going

1:48to happen this time? If I enter here, all it's actually done is kind of delete that particular

1:52line in the standard output. Okay, so in the printing of the text, but the actual file itself

1:57remains unmodified like so. We still have my name is Simona, but we just printed out everything

2:03except from the very first line. And again, if I arrow up and I say SED-2d, it's pretty much saying

2:09print standard output, but remove the second line, which should be I live in the United Kingdom. So

2:14let's go and hit enter. As we're going to see here, that is the one that has been removed. Now,

2:19like I say, when we do this, we are completely unmodifying the actual file itself. However,

2:23we do have an option to insert and inject our actions into the file itself. So let's see this,

2:29okay, if I just want to say SED and I say dash I and I say, let's go with 3d, okay, and then I

2:36specify the name of the file itself. What I'm going to do here is I'm going to target the third line

2:40for deletion, and I actually want to insert that into the actual text file itself. So really modify

2:45this, okay, so if I hit enter here, as we know, this was the original text file, but if I now cat it again

2:51and to enter, look at this right here, the third line, which was this one, this has actually been

2:56removed. Now check this out, we can also do more things with SED. Let's maybe say I wanted to

3:01replace all the instances of the word Simona with the word John, okay. So what I can do here is I

3:07can say SED and then in my quotations I will say substitute and I'll say forward slash and I'll say

3:12I want you to substitute the word Simona and then I'll say replace it with the word John, okay, so

3:17this is pretty much the format S forward slash what you want to target forward slash what you want to

3:23replace it with and end with a forward slash and then again what do you want to be targeting? I

3:28want to be targeting this particular file and if I hit enter, what it's done, it's printed the

3:32information to standard out, but has that actually modified the file itself? Well, if I happen to open

3:37it up, as we can see, it's not actually done this, so if I want to actually inject that change,

3:42what am I going to do? I'm going to say dash I like so and to enter, arrow up, cat it and now the file

3:48has actually been modified like so. Now let me just highlight something to you, okay, if I just open

3:52this up again and let's maybe on the same line just say I know John is a super common name, okay,

3:59which which is unfortunately, so let's just press ctrl o and enter and then press ctrl x to escape,

4:06so now what we're going to see here is in this particular file, notice this right here, okay,

4:11we've got two instances on the same exact line, so if I try to modify this, okay, now what I'm going

4:17to do here, just for simplicity, I'm going to remove the dash I flag and what I'll now say is

4:22anytime you see the word John, I want you to replace it with the word Simona. Now the way I

4:26phrased this was a little bit inaccurate and you'll see why, okay, so if I just hit enter here,

4:31nothing is going to happen because I actually made a mistake there, what I should have done,

4:36this is a good learning lesson, always make sure we have that forward slash at the end there,

4:40otherwise the expression is going to be unterminated, so let's try it again and look at

4:46this right here, hey my name is Simona, I know John is a super common name, sentence doesn't make much

4:52sense, I live in the United Kingdom, Simona loves working with Linux, so we can see here on this

4:58instance, John was changed to Simona and we can see here on this instance, John was also changed

5:04to Simona, but the other instances on the same line, it didn't actually happen, so what it's doing

5:09here, it's finding the match, the first match on that line and doing the substitution, then it moves

5:14to the next line and does the search, so it's not doing a global change, instead it's just finding

5:19the very first match on that particular line, now if you only have one instance on each line, well

5:25it can work as we already seen, but in this case here, not quite so, so what I could do is I can

5:30modify the query and I can also add in a forward slash g, now what I'm saying here is that I want

5:36this actual expression to be applied globally, which means all instances that you find it, so

5:41every time you see a John, I want you to change to the word Simona, so if I hit enter here, now look

5:47at this right here, makes much more sense, hey my name is Simona, I know Simona is a super common name,

5:52which it's not, it's a very unique and lovely name, unlike John, and it says I live in the eddy kingdom,

5:57Simona loves working with Linux, now again, if we just look at the text file itself, that has been

6:04undisturbed, it's not changed, but what I can do, as we know, is I can go and actually inject it directly

6:09into the file with the dash i flag, if I hit enter here, all instances of John have been changed to

6:15Simona in the actual text file, so if I happen to open up one more time, there we go right there, so

6:21that's how we can do that with the said utility, and the next one we're going to look at is the

6:26awk utility, which is an awkward name for a file, for a file, for a command, so let's say man awk

6:34and to enter, as we're going to see here, this is going to allow us to pattern scan and process

6:38language, now in this case here, the benefit of awk is really in using structured data or working

6:44with structured data, things like comma separated value files, excel spreadsheets, that type of thing,

6:49when you have things like columns, you'll see why, okay, so if I press q to quit, all right, so let's

6:54maybe create a new file just called info.txt, okay, it doesn't matter so much, and let's just say the

6:59first column can be John, I'll specify my location is in the UK, and I like to teach Linux content,

7:05okay, then we'll have James, James is in the USA, James likes to teach Windows stuff, and we'll now go with

7:12Lalo, Lalo is also in the USA, and Lalo likes to teach Cisco stuff, and we'll have Simona, Simona is also

7:18in the UK, and Simona likes to teach CompTIA stuff, okay, so right now what we have here is a text file

7:24which is kind of structured, okay, so the first column right here, this is all the names, and do you know

7:30what, let me get a bit of a thinner pen for that, it's a little bit ugly to look at, so the first column

7:34here, this is just names, then we have a new column, which is going to have all of our locations, then we

7:41have this last column here, which is going to be kind of like their specialities, okay, okay, now again

7:46it's not the prettiest to look at, but it is structured in some kind of way, so let's try and

7:51manipulate it using awk, so what I'm going to do here is press ctrl-o to write, ctrl-x to escape,

7:57here is the text file, and we're going to operate on it using awk, so I'm going to say awk, and I'm

8:02going to have my single quotation marks like so, and then I'm going to say in my curly braces, what I

8:07want to do is I want to print, and I'll say $1, now what I'm doing here is I'm saying

8:13print the first column, okay, so check this out, if I enter here, nothing's going to happen, because

8:20I'm going to have to press ctrl-c to interrupt, I forgot to specify the file, which is a bit silly,

8:24so try this again, info.txt, and to enter, look at this, what we're getting here is just the

8:30information from the very first column, and you can imagine if I go up here, and I said you know what,

8:34make this number two, we're going to get all the information in the second column, and again,

8:40arrow up, we can see three, we get the information in the third column, and if I try number four,

8:45as you may expect, we're not going to get any information, because we've run out of columns,

8:49so what I could do, is I could add information and concatenate this, okay, so check this out,

8:54what I can do here, let's say awk, and I'll say print, and then I'll have my quotation marks,

8:58I'm going to say hey, my name is, in my quotation marks, and I'm going to go and add in dollar sign

9:05one, then I'm going to have my quotation, have a space, just say I live in, and then I'll have my

9:12quotations, and I'll say dollar sign two, and then I'll say and I love teaching, and then I'll say

9:20dollar sign three, so now I will close my curly brace, and then close my single quotation marks,

9:26and then I'm going to specify the file that I want to operate on, which is going to be info.txt,

9:30if I hit enter here, look at this right here, we're now saying hey, my name is John, I live in the UK,

9:34I love teaching Linux, then it says hey, my name is James, live in the USA, love teaching Windows,

9:39so all we're doing here is substituting in, in each instance, the first column, which is all

9:45this stuff right here, then we go in, substitute in the second column, like so, and then finally,

9:50we go and substitute in the third column, as we can see right here, and once again, as we know,

9:56what we can do is we can redirect the output and put it into a text file, let's just call this

10:01trainers.txt, if I hit enter here, doesn't print out, but what we've done here is written this

10:07into a little text file, like so, and then of course, what we can now do is do things like

10:10grepping for this, okay, so I can say grep for the UK, in trainer.txt, if I hit enter here,

10:16as we can see right there, we've searched for it, it's now in a text file, nice and neat.

Using the Pipe

0:00Okay so the last command that we're going to look at in this particular skill is the pipe utility

0:05and that is this little arrow here little arrow what am I talking about this little symbol here

0:10that's like a straight down line okay this is the pipe symbol now what this does it allows us to

0:15take the output of something and then feed it as input into something else now again this sounds

0:21pretty weird when I say that but as you see examples it should become a little bit clearer

0:26so let's just do the examples and dig in so check this out think about it let's say I would like to

0:33use the grep filter but I don't want to grep on a text file I want to grep and maybe the output

0:38of a particular command so for example I say cd into the root directory and I'll go into user and

0:46I'll go into let's say bin do a listing and let's just maybe say I wanted to find something inside

0:52this particular directory okay let's just maybe say python okay now right now as we can see there's

0:57quite a lot of stuff here so it might not be so obvious if we happen to have this particular thing

1:01so what are we doing here we want to do a listing so we'll do a listing in fact you know what let's

1:05do a long listing so here is all the stuff right here and if I want to now filter this I can issue

1:10this command take its output and then use the pipe symbol and then feed it in as input into something

1:16else so now what I can say is take the output of ls-l and then use the grep filter to filter for the

1:23word python okay so now if I hit enter here check this out here are all of the things in my directory

1:29listing that actually match this particular pattern so we can see here here are all the python outputs

1:34inside this particular directory so that's an example okay all right so let me go and show you

1:38another example and we'll use a command we've not actually seen yet okay so if I say ps and I say

1:44aux or aux what I'm going to do here is show all the processes on my system with the ps command

1:51and these options here aux allows us to see all the processes for all users I want to see it in a

1:58user-friendly format so we'll see things like cpu and memory usage and we also want to see processes

2:04that are not actually attached to the terminal so all background processes background services

2:09that type of thing so if I just enter here check this out we get a ton of information coming on

2:13the screen like so okay so this command shows me a lot of stuff now again let's say I wanted to use

2:19the grep filter on the output of this particular command how would we do it we're going to use

2:25the pipe to take this particular output and feed it as input for the grep filter so let's just say

2:30in this output obviously we can see it right here but let's just say I wanted to find out where bash

2:35is in my processes so we can see here here it is with the process id of 18965 so what I could do

2:42here is I can say psx and I'm going to go and grep for bash like so and to enter as we can see

2:48right here this is the output and we can see we found it with the process id of 18965 so now I've

2:55found the process id if I want to kill that process I can say kill minus nine and then give the process

3:00id now that I've found it I can say 965 and to enter what we're going to see here we kill the

3:06bash terminal straight away so clearly as we can see right here the actual use of the pipe utility

3:12what this does it allows us to take the output of a particular command and feed it as input into

3:18another command again it doesn't just have to be grep filters we can also use other commands

3:23all right so let's say for example I want to create a little file full of passwords okay so I'll say

3:27nano passwords dot txt and let's just imagine that inside this text file you had a dump of people's

3:33common passwords so let's say someone's got a password called password1 let's just say someone's

3:38got mypass someone's got jonpass someone else is also using password1 which is pretty bad

3:43someone's got a bit of a better password with this one right here okay try a little bit better

3:49all right also check this out okay let's just save this like so we have our little file that's got a

3:53password dump in it now let's say we wanted to take this and we wanted to sort it so that it's

3:58in alphabetical order and also to remove any duplicates so what I could do as we saw is we

4:04can use cat passwords to actually output the contents here and then what I can do is I can

4:10take that output and use it as input into the sort function okay so if I just say sort like so

4:16what we're going to see here is we're going to get it in alphabetical order so we can see abcs at the

4:20top and again this gives us output the output is now just sorted but once again we can pipe that

4:26into yet another command so if I just say sort and then also pass it into the unique function

4:31if I hit enter we've now taken the output we've sorted it so that it's alphabetical and we've

4:36removed any duplicates so no longer are we going to see the duplication of this particular password

4:41indeed instead we only have one instance of it

Validation

0:00Alright friends, so let's go and validate what we learn within this skill.

0:03Alright so the instructions are to launch the lab below so let's go down here and just choose to

0:08launch this like so. Now in the meantime what we're going to do here is wait for the VM to load

0:12but once it has loaded we're going to go and double click on the Ubuntu 1 shortcut on the desktop and

0:18that's going to go and open up an Ubuntu virtual machine. Now after that I want you to open up a

0:23terminal window and perform the following actions. So I want you to print the current working

0:28directory first and then after that I would like you to go and list its contents. Then I want you

0:34to create a folder called validation folder and then change into this directory. Now once you've

0:39done that I want you to create a text file inside this directory and call it validation.txt and then

0:45use the nano text editor to edit this file and enter the following text. You're just going to say

0:51learning Linux is fun and then we're going to go and save the content and then exit the nano editor.

0:57Now after this we're going to go and use grep to search the content of this particular file

1:01that you just created and we're going to look for the presence of the word Linux. Now we want to

1:06ensure that the grep search is indeed case insensitive okay so make sure we do that. Then

1:11after that we're going to use sed to substitute the word fun which is this part right here. We

1:17want to substitute that for the word awesome and we want to ensure that the content inside the text

1:23file itself is modified okay so very very crucial. And then finally after everything we want to go

1:29and navigate back to our home directory and then validate or rather validate delete the validation

1:34folder directory including all of its contents. So friends go build up your lab give it a go and

1:40I will see you in the very next video where we discuss the solution.

Validation

0:00Alright friends, so let's go and walk through the answer them. So what I'll do here is go to my lab environment and I'm going to go and click on the Ubuntu 1 virtual machine. So just double click this like so.

0:10And all I'm going to do is to collapse this to get a bit more space.

0:13Now just give this a moment to load.

0:15Alrighty, so it's beginning to connect to the VM.

0:18So what I'll do here is just full screen this, okay.

0:20So what I'll do here is let's just go up and click this and we'll search for terminal to find the terminal app. There we go right there, click here.

0:27Alright, so as it says here, the first thing we're going to do here is to print the working directory.

0:32So that's just going to simply be pwd, print working directory.

0:35Let's enter, as we can see here, we're in the home directory for the learner user as we can see.

0:41So now we're inside this directory, we're going to go and list its content.

0:44So we can do this by just typing the ls command and we can see here all of the folders within.

0:50And if you so wish, you could also do the long listing, either which way is going to be a-okay.

0:54So then next thing we're going to do is to create a folder called validation folder and then change into this directory.

0:59So as we know, what I can do here is say mkdir validation and then we'll say dash folder.

1:05And then we'll just cd into that particular directory.

1:08So as we can see, we're now inside of it.

1:10Now we're going to use the nano text editor and enter the following line.

1:14Okay, we're going to create this file validation.txt.

1:17So inside here, I'll say nano validation.txt and to enter.

1:21Alright, so let's just say learning Linux is fun.

1:24And to save this, I'll hold down ctrl, press o and then hit enter and then press ctrl x to escape.

1:30So now I'll use the grep utility to search the content of that text file and search for the presence of the word Linux

1:36and ensure that the search is case insensitive.

1:40So what I'll do here is I'll say grep and I'll say grep for Linux.

1:43And like I say, if I just did validation.txt and to enter, it's not going to find it because I've done lowercase right here.

1:49But if I just say, you know what, I don't care about the actual casing.

1:52And I just say dash i to ignore the case.

1:56And now I hit enter.

1:57What we're going to see here is we actually do have a match, even though it begins with a capital L.

2:01Alright, so next I'm going to do here is use sed to substitute the word fun for the word awesome.

2:05And we want to ensure that the content of the text file itself is indeed modified.

2:09So what I'm going to do here is I'll say sed and I'm going to say dash i to actually insert the changes.

2:14And then in my quotations, I'm going to say substitute the word fun for the word awesome.

2:19Now again, if you so wish, you can add on the forward slash G for the global conversion.

2:24But either which way, because there is only one instance, we don't have to care about that.

2:27If I just close off my quotations and say I want to act upon the validation.txt file.

2:33If I hit enter, now it's going to go.

2:35And if I do a listing, the file is still there.

2:37And we want to make sure that the file is modified.

2:39So let's use the cat command to open it up.

2:41And as we can see here, we have indeed modified the content to say learning Linux is awesome.

2:45Alright, so the next thing we're going to do is to navigate to the home directory and then delete the validation folder as well as all of the contents.

2:53So let's go back to our home directory.

2:55The shortcut we can do is just press sed.

2:57That takes us back to our home directory.

2:59As we can see right here, if I do a listing.

3:01In fact, you know what, let's clear the screen and do a listing again.

3:03As we can see, we have the validation folder.

3:06To remove it, what we're going to do here is say rm-r for recursive deletion.

3:11Because if we just do rmdir, just for clarity if I do this.

3:14And we do have content within this folder.

3:16And to enter, what it's going to do here, it's going to say it's failed the actual action.

3:20But if we say recursively remove and then specify the validation folder.

3:24And to enter, now we're going to see here the folder is gone as well as the text file within it.

3:30So friends, that is us for our validation challenge on the Linux CLI.

3:34I hope this has been informative for you.

3:36I'd like to thank you for viewing.

Team training path

Turn this skill into assignable team training

This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need CompTIA AutoOps+ (AT0-001)?

Learning on your own? Browse individual plans ($49/month, billed annually)

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo