Linux Commands Part 1 – CompTIA A+ 220-1202 – 1.9

The Linux operating system provides numerous utilities to manage the OS from the command line. In this video, you’ll learn about ls, pwd, mv, cp, rm chmod, grep, and more.


If you’re a Windows user, you’re probably familiar with working at the command line of a Windows computer. You would go to the Start menu and look for cmd.exe, for example. The process is very similar in Linux. You’ll be starting an application, such as Terminal, Xterm, or something that’s very similar to get you to the command prompt of the Linux device. If you’re running macOS, you’ll find that the commands that we use here in Linux and the commands available in macOS are very similar to each other. You should be able to use almost all of these commands in your macOS terminal screen.

Although macOS is derived from a Unix version known as Berkeley Software Distribution, or BSD, this video will focus on commands that are native to Linux. If you’re not currently using Linux, this would be a great time to learn about virtualization on a desktop. For example, if you’re running Windows, you might want to download the latest version of VMware Workstation or VirtualBox so that you can virtualize Linux on your current desktop.

Having a virtual machine of Linux that you can use, instead of restarting your computer into a separate Linux partition, and then restarting your computer to go back to Windows, you can instead run Linux on the desktop of your computer. And as I go through this video today, you’ll see me using Linux, but it’s really Linux running inside of a VM. As we go through these command prompts today, I’ll give you an overview of how that command operates.

But if you’d like more information about that particular command, you can go to the command prompt and use the man command. This stands for manual. So if you wanted to look at the manual for the grep command, you would use man space grep and read all about the details of the grep command. There’s usually a lot of information stored on these man pages, because this is the documentation for that utility.

So once you’ve read through the entire manual page for that command, you’ll know every possible option available to run that utility. Here’s my Ubuntu desktop. This is the front end that we’re going to use to run these Linux commands today. But you can run any Linux distribution you’d like, and these commands will functionally be the same across all of those different distributions. You can see that I have a terminal that I’ve already pinned to my left hand menu bar, but I can go all the way down to the list of applications on this computer and simply search for the word “terminal.”

And if I do that, it will list out all of the available options on this distribution. I have two different terminals to choose from, the standard terminal and a separate terminal with enhanced capabilities. Everything we’re going to use today will use the standard terminal, to be able to run these particular programs. If you’re a Windows user, you’re probably familiar with the directory command or dir command. This is a command that will list out all of the files and folders in a particular directory.

In Linux, the command is the ls command for list directory contents, and this will show you all of the files, all of the directories, and anything else that is located in that particular subdirectory. In my distribution, there’s color coding in the terminal. So anything that is a directory is listed in blue. Anything that’s archived is in red. And there are other colors you may find for different file types. You’ll often find that there may be hundreds, or even thousands, of files inside of a single directory. And using the ls command, lists all of those files, and they scroll down the screen before you’re able to see anything go by.

One way that you can paginate this list of files is using the pipe, and using the more utility. So when you use the ls command and use a pipe, we’re going to send the output of the command on the left through the utility that’s listed on the right. This will allow you to hit the spacebar to go from one page to the other, rather than having everything scroll by at once.

In my Linux terminal, the default prompt shows me the user that is currently logged in. This username is professor, has an @ sign, and then it shows me the name of this device. And in this particular case, this Linux machine is called Atlantis. To be able to run a command, we will run it where the cursor is flashing at the end of that line. I’m simply going to run the ls command, and it will list out a number of files that are in this directory.

You can see that this lists out a number of directories in the bold blue color, and I do have a single file in this directory known as first.txt. There’s also a number of system directories on this computer that we might want to look at. For example, let’s use the cd command to change directory to a particular subdirectory. And in this case, we’ll choose the /var/log directory. If I perform the same ls command again, you’ll notice there are a number of different files located in this directory.

If we wanted to get more detail about each one of these files, we could use the ls command with the -la option. This lists out all of the information associated with permissions, rights, and the ownership of these files. You can see that this lists out a lot of information, and it scrolled by a number of different pages. Instead of having it scroll by, let’s use that ls – la command again and pipe it to the more utility. And now, it will show me the first page. And at the very bottom, it provides me with an option to show more.

I’ll hit the spacebar, and it will move to the next page. It prompts me again for more because there’s additional information. We’ll just keep hitting the spacebar until finally, we’ve moved through all of the different pages of this output. There’s a lot of information that is shown in this expanded view of the ls command. You’ll notice that our file names are on the right side of the screen, and there’s a lot of other information that is shown in this expanded view of the ls command.

Later in this video, we will show you more information about all of the details that we see on this screen. When you’re working at a command prompt, you may be moving back and forth around a particular computer, and you may not have the luxury of having a lot of detail at the prompt, telling you what your current directory is. Fortunately, there’s a command pwd, which will print the working directory. It displays the current directory path so that you know exactly what directory you’re in when you run any of these commands.

In this Linux distribution, it adds the current working directory onto the prompt on the terminal. This means that we don’t necessarily need to use the pwd command. But if you’d like to confirm that the information that’s being shown on your terminal really is accurate, we can use pwd and hit Enter, and you can see that we are indeed in the /va/log directory. In the Linux operating system, you don’t directly rename a file. You instead move a file from one name to another.

To be able to do this, we use the mv command to move that file. The syntax for this command is to use the mv command. The first parameter is the source of what you’re starting with, and the second parameter is the destination of what you would like to rename this file, or rename this directory to. So, for example, if you wanted to rename the file first.txt, and rename it to second.txt, you simply use those with the mv command.

At our command prompt, I’m going to go back to my home directory by simply typing in cd and hitting Enter. You could also type in cd and use the tilde to specify that you’d like to go back to your home directory. If we do a pwd, you can see that we are indeed in /home/professor. And let’s look again at the files that we have in that directory. We have a lot of different directories inside of this folder.

But we also have a single file called first.txt. Let’s now rename that file by using the mv command. We will specify the source. In this case, that is first.txt. And we would like to change the name of this to be second.txt, and we’ll hit Enter. Notice that we don’t get any type of response at the prompt. It simply puts us back where we were. If we use the ls command again, notice that first.txt is no longer listed in the files in this directory, but we do have a second.txt, which we’ve renamed from our original first.txt file.

Sometimes, you don’t want to change the name of a single file, you’d like to create a duplicate of that file. On a Windows computer, we would use the copy command. In Linux, it’s very similar. We would use the cp command to copy a file. And the syntax for cp is very similar to the one we just used for the move command, where it is cp, the source file name, and the destination file name. So if you wanted to make another copy of the second.txt file, you would use cp, second.txt, and then use the file name you would also like to have. In this case, we’ll call it third.txt.

Back at our Linux command prompt, let’s perform an ls. And we can see there is a second.txt file that is in this directory. If I perform an ls – la, you can see the second.txt file is here, and the file size for this text file is 9358 bytes. This will be important when we look at the duplicate of this file, and we will duplicate it with the cp command. Our first file that we will use will be second.txt. I’m going to type in sec and hit the Tab key. That tab completion, very similar to the tab completion you have in Windows. It finds whatever matches in this current directory and simply completes what we were typing.

In this case, I would like to copy the second.txt file to a file named third.txt, and we’ll hit Enter. Notice, again, we don’t get any prompts back. It simply puts us back at the command line. And we’ll type, again, ls – la. Notice, now, that we still have that second.txt file that’s 9358 bytes. But now, we have another file in this directory, known as third.txt, and it is also 9358 bytes, which makes sense, since it is a duplicate of second.txt.

Just as we can copy a file to create a new document, we can also remove files with the rm command. This will delete the files from the file system. If you want to delete an entire directory, you’ll need to make sure the directory is empty. We can override that empty directory requirement by using the rm command with the -r option, and it will delete a directory, even if files still exist in that directory. Now that we’ve copied second.txt third.txt, Let’s? Now remove the original file name.

We will use the rm command, and we’ll specify second.txt, and hit Enter. Although second was there previously, if we perform that same ls, you’ll see now that second.txt not in this folder. But of course, we still have its copy that we named third.txt. As you’ve probably seen in other operating systems, there are ways to specify the rights and permissions associated with a single file. In Windows, you do this from the file properties. In the Linux command line, we do this using the chmod command, which means we are going to change the mode of a file system object.

This means that we can choose a file or directory in our system, and decide who’s able to read that file, write to the file, or execute the file. Sometimes, you’ll hear this referred to as octal notation, and we will look at details of that notation in just a moment. For each of the objects in the file system, we can set different rights and permissions if you are the file owner or user of the file. You can set different file permissions if you’re part of the group that is associated with that file. And then you can set permissions for everyone else. This would be others or all in the Linux operating system.

This list of modes, or permissions, associated with each of these objects is what’s listed in the very first column when we perform an ls – l. You can see that it has a number of different columns. There are different letters and dashes. And what we want to do is break down all of those different letters and sections to see exactly what those refer to. Let’s look at one line of the ls output to see more details about this chmod command.

In this case, we’re listing out a number of permissions associated with a file name called script.sh. And you can see there are a number of different letters associated with those rights and permissions. We were able to get those letters added by running a chmod command with 744 across the filename known as script.sh. And this is the result of running that chmod command. Let’s now break down all of these different columns and letters to determine what this really means.

Notice that there are 10 different characters that are included in that set of rights and permissions. The first column we’ll look at in a moment, but generally this is one that will have a dash if this is a file, and it will have a d if it’s a directory. There are also other letters you might see in that very first column, depending on the type of file object it happens to be. For the purposes of the chmod command, we’re really most interested with these next three groups.

And you can see that each of these remaining characters is separated. Three of those are for the user, three are associated with the group ownership, and three of these would be for everyone else on the system. You’ll also notice that the owner of the file and the group associated with the file are listed in the output of the ls command. Those are the two names that we can see in the middle of this line.

The first one is the name of the user. In this case, the username is professor. And the second word that’s next to it is the name of the group that has been assigned to this file. In this case, this file is part of group staff. So if you’re trying to figure out who the user is, what group they belong to, and who everyone else might be, you can find all of that information in the ls output associated with that file name.

If we look at the last nine characters of that first column, you can see that the first three letters are assigned to the user permissions, the middle three are assigned to the group permissions, and the last three are assigned to everyone else on that system. Notice, also, that we have numbers associated with each of those. These are associated with the chmod command we previously ran. The chmod command was 744, and this correlates to– the user is 7, the group is 4, and everyone else is also 4. You’ll also notice that those numbers are associated with a series of permissions.

So the number 7 is associated with read, write, and execute. The number 4 is associated with read only. You can see that those permissions are listed in this table, along with the type of r, w, and x that you would expect to see in that small box of three characters. So if we use the chmod command of 744, that means that the owner has permissions relating to number 7, everybody in the group has permissions for number 4, and everyone else has also permissions that are number 4. If we break this out into those permissions you can see that the user being 7 is read, write, execute. So we would expect to see rwx in that box, and we do.

The group permission is 4. That means read only, which means we would expect to see r–, and that is what we see in the middle box. And since others is also 4, then it is exactly the same as group, with the r–. This is the way that we’re able to see, at a glance, what type of rights and permissions are associated with the different files at the command line. If we look at the output from our previous command that we ran, you can see all of the different files and folders that are listed in my home directory.

And you can see, in that first column, that there are a number of different rights and permissions for the different documents in this folder. If you’re just learning about these different permissions and those characters that you see in that first column, this may be a bit overwhelming. But as you start to parse these out and become more familiar with what they might be, you’ll find that it becomes very second nature when you’re looking at one of these file listings.

Although up to this point we’ve been talking about the chmod command and using it with numbers, there are also letters that you can use to designate what you would like to change, and what you would like to modify it to. We’ll look at those options in just a moment. We’ve already seen the chmod command with the 744 option and the name of the file name. And since it’s 744, that means that the owner of this file, or the user, has read, write, and execute permission.

The group that is associated with this file has read only permission. And in this case, everyone else on the system also has read only permission. As I mentioned, you can also use letters to determine what permissions you want to set. You can use that chmod command. The first letter would be the section that you would like to change. If you use the letter a, we would be referring to all users on the system. That means not the owner of the file, and not the group associated with the file, it would be everyone else. We would then specify the type of permission we would like to set.

If you use the dash, that means it would be not that permission. If you use a plus sign, we would be adding that permission. And lastly is the permission itself. If you want to use r, it would be read, w would be write, and would x be execute. So for this chmod command, where we’re performing the chmod a-w to first.txt, we’re obviously referring to a, that would be everybody, or all users on this computer. So everyone who is not the owner of the file, or anyone who’s not in the group associated with this file, would be the a designation.

And with the -w, the dash means that we would be removing a permission, and the permission we’re removing is the write permission. So chmod a-w for first.txt will remove any write permissions that are currently set for all users on the system. Here’s another example where we’re using the letters to designate the permissions. We’ll use the chmod u+x. If it’s the u, that means it’s the user, or owner, of the file. In this case, it’s the script.sh file.

And if we’re using the plus sign, that means we’re going to add a permission. And in this case, we’re going to add the x permission, which is the execute permission. Let’s change the permissions of a single file and see what the results of changing those would be to the information that is shown in the ls command. We will perform an ls – l, and you can see that I do have a file in this directory known as third.txt.

You’ll notice that the owner of the file is professor, and the group that is assigned to this particular file is the group staff. Let’s also look at the very first column. You can see that there’s nothing in the first column, which means that it is not a directory, it’s a file. And you can see, rw- is the very first options here, which means the owner of the file can read and write to the file, but not execute what’s in the file. The second three are r–, that means that everyone who is in the staff group has read access to that file, but does not have write access to the file.

And then the last three are —, which means everyone else on the system has no access to the third.txt file. Let’s look at just the third.txt file by performing an ls – l and specifying just that file. Now, let’s make a change to that single set of permissions. And let’s say that we would like to chmod this file. And we would like to use 764 as the permissions associated with this file. And then we need to specify the file name, which is third.txt.

If I hit my up arrow twice, it will cycle through the different commands that I’ve previously run. We’re going to run the ls – l third.txt command again, and you can see that changes were made to the permissions, as compared to the ones that we previously had for this file. If we look at these, we can see that the first three characters are rwx. That specifies that the owner of the file can read, write, and execute this file.

We also have the next three of rw-. That means that anyone who is in the staff group can read or write to this file. And then the last three are r–, which means everyone else on this computer can simply read what is inside of the third.txt file. When you start working with files, directories, and applications on a Linux device, you will certainly run into a situation where you will need to modify the permissions of a single file, and using the chmod command gives you complete control over that process.

In our discussion about the chmod command, we talked a lot about who the owner of the file was, and what group was associated with the file. Obviously, we can make changes to those as well. We can change who the owner of the file might be associated with, and we can change what group is associated with a file or directory. You can do this by using the chown command. That is the change ownership command.

And that allows us to make changes based on the owner and the group. You’ll notice, for example, that you can change the name of the owner by simply specifying the owner itself. And if you would like to specify the owner and the group, you can do all of it also on the same command line. For example, let’s say that we’ve been working on this text file which contains documentation on our backup process. And now that we’ve completed this document, we would like to change ownership of this document to be the backup user.

In the previous command, we saw that the third.txt file was owned by user professor. Let’s say that we’d like to turn this file over to someone else, so that they would own the file and be able to use it on their system. Let’s say that we would like to modify the owner from professor to user jack. To do that, we would use the chown command. We would specify the user we would like to change this to. In this case, the username is jack. And the name of the file is third.txt. When we hit Enter, you’ll notice that we get an error message saying that the operation is not permitted.

That’s because to change the ownership of a file, we will need elevated rights and permissions. In Linux, the way that we do that is to use the sudo command. We will then do exactly the same command, chown, jack, and third.txt. And when we do that, it will ask us for a password, since we are asking it to perform this function with elevated rights. I’ve previously used my administrator password, so it used that password that was cached earlier.

If you’ve not used your superuser password in this command prompt yet, it will first prompt you for that password and then perform the command. Now that we’ve run the chown command, we can use the ls command again and look at what the differences are for the third.txt file. And you can see now that the permissions are the same, but the owner of the file has changed to jack. We can see that it is still part of the staff group. And obviously, the file name remains the same with third.txt.

If you’d like to change both the name and the group at the command prompt, you can specify them both in the same chown command. So we’ll run the sudo with the chown option. In this case, I’m going to change the name back to professor. And then we’re going to put a colon and specify the name of the group. Let’s say, in this case, that we would like to move this file to be part of the backup group. And then we’ll specify third.txt and hit Enter.

Now, if we perform that same ls command, you’ll notice that we’re back to professor as owning the file. But you can see that the group name has changed to the backup group for the file third.txt. Whenever we’re troubleshooting a system, or simply working on a document, we may want to look inside of the files on the system to see if we can locate a specific type of text. The command in Linux that allows us to do that is the grep command. This will search through one or multiple files, or folders, to be able to find specific text that may be located in those documents.

To use grep, we obviously use the grep option at the command line, we specify what text we would like to find in these files, and then you would specify one or more file names directly afterwards. So if you wanted to search for a particular file, we would use the grep command. We would specify the text we were looking for. In this case, the text is the word failed, and the name of the file we would like to hunt through is auth.log.

This could be a very large document, but you’ll find that the grep command works very quickly to find this information. So if we use that at the command line, it will list out every line inside of that auth.log file that contains the word failed. Let’s use the grep command for the third.txt file. You can see that there are 9358 bytes inside of this file. So instead of listing through every single line that happens to be in the file, and try to look for the text we want, we can simply use the grep command. In this case, I’m going to look for the word launch, and the name of the file name is third.txt.

And you can see that there are three separate lines inside of this very big file that contains the words launch. In this case, it is a gdm-launch- environment that is listed. But since we did specify the word launch, it finds it wherever it happens to be inside of that document. The Linux file system contains thousands and thousands of files, and if you’re looking inside of a user’s directory, you may have no idea where a particular file may be located. Fortunately, the Linux operating system includes a utility that helps you find a file, and you can find it listing the name or the extension.

This find command has a number of different options. You can use simply find by itself, if you’re in a current directory and looking for a specific file type. Or you can specify what directory you would like to use, along with a number of other different search parameters. In our home directory, we know on this system there happens to be a file named third.txt. But let’s say that we just logged into the system, we’ve never been on this computer before, and we would like to see if there’s more instances of files that are named “third dot something.”

So we’re going to use the find command. I’m going to use this with the parameter third. And since I would like to find the word third, and any extension associated with that, I’m going to use a dot and an asterisk to specify a wildcard. When we hit Enter, it will search through this directory, and all directories underneath, to find all of the matching files. And in this case, there’s only one, third.txt. Let’s make this a little bit broader in our search, so that we can really see the power of the find command.

In this case, I’m going to use the find command and search for any file that may be in my current directory, or directories underneath, that has a capital letter D, and then anything else afterwards. And if we hit Enter, you can see that any file or folder that has the first letter as a capital D is now listed inside of my local user directory. If you’ve ever started Windows after something like a power outage, or an inadvertent power down, then you notice that it goes through a process of checking the logical file system before it starts the OS.

You can also perform a similar function in Linux using the file system check utility, or fsck. This utility will check for any logical file system errors. If it finds any inconsistencies in file sizes, it will correct those. And it will look for any files that may not be included in the file system index. This check commonly runs during the startup process, because it needs to check the file system before it is mounted as part of the OS. You can see that it goes through the process of checking all of the files and clusters that are in a particular mounted directory, and you can see it goes through each one, checks all of them to make sure that they’re working properly, and then continues with the normal boot up process.

When your Linux system starts up, it begins adding different folders into the file system using the mount utility. You can also run this utility yourself at the command line to view what folders may already be mounted. And if you would like to manually mount or unmount a folder, you can also use the mount command for that. This is very similar in functionality to the Windows net use command, although the mount functionality is more integrated into the Linux OS.

If you simply run the mount command with no parameters, it will list out all of the mount points that are currently set and running on your system. This might be a large list to parse through, so if you’re looking for something specific, you may want to pipe this to the grep command to try to find exactly a piece of text that you’re looking for. One common use of the mount command is to make the information available on a USB drive that you’ve just connected to your Linux computer.

Most modern versions of Linux will recognize that you plugged in a USB device, and it will automatically mount that USB drive. But if your system has been modified, or doesn’t provide a way to automatically mount that, then we’ll need to use the mount command to be able to use that USB drive. Let’s look at all of the different mount points that are currently on my Linux system. There are quite a few here, so I’m going to scroll back up.

We can see that when I started the mount command, it showed us that the sysfs has been mounted to /sys in our file system. Proc is mounted to the /proc mount point. Udev is mounted to /dev. Devpts is mounted to /dev/pts. Tmpfs is mounted to /run, and so on. You can see there’s quite a number of mount points, and on your Linux distribution you’ll probably have a large number of mount points as well.

Earlier in this video, we ran the chown command to be able to change the ownership, or group, of a particular file object. To be able to run that command, however, we needed elevated access. And you notice that we ran the sudo command to execute that command as the superuser. There are also options in the sudo command to run that command as a different user ID who’s on that system as well.

Sudo allows us to run a single command with that elevated access, but you might want to run all commands with that elevated access. To be able to become superuser at your command prompt, you use the su command. By using just su, you’re changing the entire terminal session to one that is a superuser session. Any commands that you type in this superuser terminal session will run with elevated rights and permissions.

When you’re finished running as the superuser, you can use the exit command, and that will put you back into the normal prompt for that terminal screen. From a best practices perspective, we would much rather run sudo to be able to run these commands, rather than starting a single terminal screen that has complete access to the system. Here’s another example of someone running a command with elevated rights and permissions.

They’re using the sudo option, and running the shutdown command to be able to shut down. This particular computer shutdown is one of those commands that is not available to users that are on the system. You need to be the superuser to be able to run that shutdown prompt. There are a number of different ways to install and update applications on your Linux device. Many distributions use the apt command to do this.

This is the advanced packaging tool. And from there, you can install, remove, or update any of the packages or applications that you’ve installed on your system. For example, if you wanted to install a copy of the Wireshark protocol analyzer, you would use the sudo with apt, use the install option, and specify Wireshark. Let’s install Wireshark on my Linux distribution. We use the sudo command with apt. We’re going to install Wireshark. And we’ll hit Enter.

Because I am running the sudo option, it asked me for my password. I’m going to type that in. Hopefully, I got that right. I did, because now it begins the process of determining what it needs to install, along with Wireshark, to be able to get this working. It says that it needs to get 70.1mb of archives. And after this operation, we’ll be using 282mb of additional disk space. Do I want to continue? Yes, I do, I’ll hit Enter.

And it’s going to go through the process of downloading all of the documents that it needs, installing the application, and then putting me back at the command prompt. With most applications, it runs through that at the command line, and it’s done. In the case of Wireshark, there are additional options that I need to choose. It tells me that I need to configure some options within Wireshark. Should non-supervisors be able to capture packets?

In this particular case, since this is my computer, I’m going to say yes. And then it continues with the installation process. This usually only takes a few minutes for most applications. It goes through and updates the applications running on your system, and then it tells you that everything is up to date. No services need to be restarted, no containers need to be restarted, no user sessions are running outdated binaries, and no vm guests are running outdated hypervisor binaries on this host.

And now, if we go to the main menu of my Linux distribution, we can type in Wireshark, and it finds Wireshark as an option that we can run on our desktop, now that we’ve installed it with the apt option. In Linux, there are many different ways to perform similar functions, and some of those will differ between different distributions.

For example, your distribution may not have the apt utility. Instead, it may be using a similar utility, known as dnf. That is an abbreviation for Dandified YUM. This is an upgrade to the YUM package manager for Linux. And this is another utility that allows us to install, update, or remove packages from our computer. If you’ve been using previous Linux distributions, and you’re already familiar with YUM, YUM stands for the Yellowdog Updater, Modified, then you’re going to be very familiar with the syntax for dnf.

You will usually find dnf used on systems that use the Red Hat Package Manager, or RPM, to install applications. So if you’re trying to install an application and you find that the apt command is not available, then you’re probably using a Linux distribution that uses dnf.