Linux Commands – CompTIA A+ 220-1102 – 1.11

The Linux command line provides extensive access to operating system files and configurations. In this video, you’ll learn about file management, software installation, network configurations, troubleshooting processes, and much more.


If you’ve ever worked at the command line of Linux, then you are probably using the Terminal, the XTerm, or some similar type of terminal program. And as we go through this video, you’ll notice that the commands that we’re running in Linux are also the same commands you can run in macOS. That’s because macOS is derived from Unix, which is also where Linux is derived. So a number of these commands are identical between the two operating systems.

If you’d like to try these commands on your own version of Linux, you can run a live CD version or install Linux on a virtual machine and run it on your workstation. All of the commands I’ll be showing you are running in Ubuntu on a virtual machine on my desktop. And if you find yourself running into a problem understanding the syntax of a particular command, you can use the man command, which stands for the manual. So if you wanted to see more information on the grep command, you simply type in man grep, and it will give you all of the details for that particular command.

Here’s my Ubuntu Linux desktop. It is a default configuration with nothing on the desktop. There are a number of icons on the left hand side, and at the bottom left is a view of show applications. And from there, I can type terminal, and that’s going to bring up the terminal option. And if I click that, I’ll get a terminal screen that I can then use to input commands directly to the Linux operating system. Let’s start our tour of these Linux commands by learning how to list directory contents in the window.

If you’ve worked at the Windows command line, then you’re probably familiar with the dir command. There is a similar command in Linux called ls, which stands for list directory. This will list the files, the directories, the symbolic links, and anything else that might be stored in this particular folder. It can also color code some of this information so you might notice that directories have a different color than the files. This isn’t necessarily supported across every Linux version, but you may find that feature inside of your terminal.

You can view a longer form view of this directory by using the ls command with the -l option. If this is a very large group of files that would extend a number of pages, you may want to use the more command to limit the output to show you one page at a time. To do this we would use the ls -l command, but we would pipe it with the vertical bar to the more command. This will present the output one page at a time, which allows us to see everything on the screen before continuing.

Let’s see what files are on my system. I’m going to use the ls command, and you can see there are a number of directories. I have desktop, downloads, pictures, videos, and more. There is a file in this directory called first.txt. I can also use the long version of ls by using ls -l, which shows me exactly the same information. You can see all of the different subdirectories and the first.txt file, but notice there’s a lot more information on the screen.

On the left side is a list of permissions. We’ll go through those in just a bit. There’s a list of the owner of the file and the group that owns the file, the size of the file itself, and then a date associated with the last update. If you’re looking for a particular file, or you just like to see what files are available in a particular directory, you would use the ls command. You may have noticed when we were using the ls command that it was not obvious what the name of the directory was where all of these files were stored.

You can use the PWD command, or Print Working Directory, to show exactly what the current directory name happens to be. If you have a lot of windows on the screen and you’re connected to a lot of remote systems, the PWD command can really help you understand exactly where you’re working in a particular file system. And indeed, looking back at the ls command, it’s not very obvious what directory we happen to be in. So I’m going to use the PWD command and hit enter.

And you can see that the directory that we’re working in is /home/professor. If you need to change the name of a file or directory, you would use the mv command inside of Linux. MV stands for move. So if you need to move a file from one name to the other, you’re effectively renaming that file. The syntax for the mv command is to use the source file name and then specify the destination file name. So if you’d like to rename a file, you would use the mv command, you would specify the file that you’d like to rename, in this case first.txt, and you would rename it to the file second.txt.

Let’s try that on our Linux system. We do have a first.txt file so I’m going to use the mv command. I’m going to specify first.txt, but I’ll type in phi and hit the tab key to autocomplete that file. And then I’ll specify what I would like to move this name to, which in this case would be second.txt. And when I hit enter, we simply get a prompt back. So if we perform another ls, we can see that our first.txt file is no longer listed, and the file name is currently called second.txt.

Instead of changing the name of the file from one name to another, we can create a copy of the file so that we have two versions on our system. We would use the cp command to copy a file, and it uses the same syntax as the move command. We would use the CP with the source name first, followed by the destination name. Let’s copy that file second.txt to a file called third.txt. I’ll use the cp command. I’ll specify second text, and now, we need the output name of third.txt.

And if I hit enter and perform another ls, you can see we now have two versions of this file. We have the one originally called second.txt and a new file that contains exactly the same information called third.txt. Of course, there’s also going to be times when we would like to remove a file from the file system. We would do that with the rm command, or remove command. This will remove both files and directories. But if you’re planning to remove a directory, you have to make sure the directory is empty before you perform the rm command.

Let’s try this on our list of files. We have the second.txt and third.txt. I would like to remove the second text file. So we’ll use the rm command and specify second.txt. And if we perform another ls, you can see that the only file left in this directory is third.txt. I mentioned earlier that when we looked at the long version of the directory listing, we saw all of these letters on the left side. There’s d’s, and r’s, and w’s, and x’s and these correlate back to permissions associated with the file.

The r stands for read access, the w stands for write access, and the x stands for execute access. This is the way that we can define how a particular file may be used or viewed by different users inside of Linux. You can view this by breaking out all of the letters that are contained within that long string. The first column designates the type of file this is. If it’s a simple file, like second.txt or script.sh, it’ll simply have a dash, or a hyphen. But if this is a directory, there will be a d in that column.

If it’s a symbolic link, there will be an s in that column. And there are other options within Linux, as well. To view the permissions of this file, we would then separate the next nine characters into three different groups of three characters each. So the first three are the user permissions, the middle three are the group permissions, and the last three characters are the permissions associated with everyone else. To be able to change these permissions, or modes as we call it in Linux, you would use the chmod command.

You would specify the mode that you’d like to change it to, and then you specify the name of the file that you would like to modify. This mode, being these three characters, are associated with these values that we see here between zero and seven, seven being read, write, and execute, which is effectively full permission. and a zero would be none of those, or no permission. So in this example where we’re changing the mode of script.sh to seven, four, four, the seven means that the user will have read, write, and execute permission.

Anyone who belongs to the group that owns that file will have four, which is read-only permission. And everyone else who’s neither a user or group, will also have the permission of four, which is also read-only. If you remember your conversions between binary and decimal, you’ll notice that these values are associated with the binary representation of the r, the w, and the x. If the x is in the one column, the w is in the two column, and the f is and the four column, you can see that having four plus tw plus one is equal to seven.

In the case of the number four, read-only, we just have a single character in the fourth column, which is why that one is designated as number four. So if we were to use the chmod command with a seven, four, four on file first.txt, that would mean that the user would be associated with the seven, or read, write, and execute access. The group would be associated with the four, which is read-only access. And everyone else would have access of four, or read-only.

You can also use letters instead of numbers with the chmod command as long as you remember what those letters refer to. So in the command chmod a-w first.txt, the a stands for all users, and the -w means we’re going to disable any right functionality to that particular file. Here’s a similar command. chmod u+x on the file script.sh means that the u, which is the user or owner of the file would have plus x, or enabled, execution rights to the file script.sh.

Let’s change the mode of our file third.txt. And if we look, we can see that third.txt is a file, does not have the d in front of it. So it’s not a directory. It has an rw dash for the user or the owner, which means the owner has both read and write access to the file. We have rwx for the group, which means the group would have also read and write access. And you’ll notice the last three characters are all dashes. So everyone else on the system has no access to third.txt.

Let’s modify this so that anyone else on the system would have read-only access to third.txt. So let’s run the chmod command, but we don’t want to change the values of the first two for the user or the group. We only want to change those last values for everyone else. We can see that rw- is listed for the user, which is the same as the group. And if we look back at our chart, or we perform the binary conversion, we can see that rw- is the same as the number six.

So let’s run the chmod command. Our user remains at six. Our group remains at six. And for the last three values, we want to enable the r column, which is in the four column. So I’ll specify four, and then we’ll specify the file name of third.txt. Now when I perform an ls -l, you could see that the permissions have changed for the third.txt. It’s now rw-, rw-, r–, which means that everyone else on the system has read access now to third.txt.

Not only can you change permissions associated with the owner and group. You can change the name of the owner and the name of the group with the chown command. You would use sudo because this requires additional rights and permissions in Linux, the chown command. You would specify the name of the owner. You could optionally also put a colon and include the name of a group, and then you specify the name of the file.

In the Ubuntu Linux that I’m using, by default, it creates a username and creates a group from that same username. So you’ll notice that all of my users are named professor, and the group is also named professor. In this case, let’s change the ownership of the third.txt file so that instead of it being professor, the owner will actually be called Messer. That is the name of another user that’s defined on this Linux system. So let’s use the chown command.

I’ll specify the name that we would like to move this to, which is Messer, and then we’ll specify the name of the file, which is third.txt. You’ll notice that this command doesn’t work because we don’t have elevated permissions, and Linux isn’t going to allow just anyone to change the owner of a file. You have to have administrative, or root, permissions on Linux to be able to make this change. To be able to do this, we’ll use the sudo command, which gives us elevated rights and permissions for this single command.

Well then choose chown messer third.txt. And when I hit enter, it will ask me to authenticate with my root credentials. So I’ll put in my password, and it gives us a prompt back. We now look at our long view. We can see that the third.txt file does have a new name associated with that owner, and the new owner’s name is Messer. This ability to run a command with elevated rights and permissions is very similar to the function of Windows to run a command as administrator.

In Linux, the administrator is the superuser, and that’s why we use su, or sudo, to provide that elevated permission. As we’ve already seen, putting sudo in front of a command runs that command as the superuse on the system. You could optionally use a flag to run this command as a different user on this Linux system, as well. And when this finishes executing the command, it goes back to the regular permissions for all subsequent commands.

If you would like to have an entire session inside of the terminal run as the superuser, you could start the session with the su command. And now, every subsequent command you type in will always run as the superuser. When you’re finished running commands as the superuser, you can use the exit command, which puts you back into the terminal with your normal rights and permissions. There will certainly be times when you’re using Linux where you will need to install or uninstall a particular application on that operating system.

And depending on the Linux version you’re using, there may be different commands that provide this management. On some Linux distributions, it’s the apt-get command. Apt stands for advanced packaging tool, and it allows you to install or uninstall entire packages or applications inside of Linux. So if you’d like to install a new application, such as Wireshark, we would use the command sudo apt-get install and Wireshark. Let’s try this on my Linux distribution.

To install a file, you will need elevated rights and permissions so we already know we’re going to start this command with the sudo option. We’ll specify apt-get as our command. We want to install an application, and in this case, we want to install Wireshark. We hit Enter, and we’ll find out all of the files that we need to be able to run this particular application. And if it has enough room on the drive, it will begin unpacking, setting up, and installing that application.

Now that Wireshark is available at my system, I can run the command line options for Wireshark, or we can go to the list of apps, and I can type in Wireshark. And there’s the application. I simply click that, and now, I can begin capturing packets from the network. If you’re using a Linux distribution that’s based on one of the Red Hat versions of Linux, then you probably don’t have apt-get available in your distribution.

Instead, you would use yum, or the Yellowdog Updater, Modified. This allows you to add, modify, or remove packages following the RPM style, or Red Hat Package Manager format. This is something you’ll have to look into your specific distribution to find out which package manager is used by your version of Linux. And then from that point, you’ll know whether to use apt-get or yum. There may be times when you need more information about the network config inside of Linux, and one of the ways that you could do this is by using the ip command.

IP allows you to perform a number of different networking functions, but if you simply want to look at the current configuration, you would use the command ip address. You can also look at a routing table list by using ip route. And if you’d like to add or modify an IP address associated with a network adapter, you can use sudo with the ip address option. Choose add, and then specify the IP address, the subnet mask, and then specify the device, or the name of the adapter, by adding dev for device and then the name of that particular adapter.

Let’s look at the IP configuration on my machine. I’m going to use the ip address command. You can see that the first adapter is the lo, or loopback, adapter. And if you’re familiar with the loopback address for IPv4, you can see that it’s set to 127.0.0.1. We also have another adapter on this device, which is an ethernet adapter. This ethernet adapter name is end0s5, and you can see the IP address associated with this adapter is 10.1.10.65, with a subnet mask of /24.

Before you begin installing new applications and making changes to your operating system, you may want to know how much free space is available on your system. Linux has the command df, or disk free, to show you how much free space is available. The default for df is to show you the amount of space available in number of 1K blocks. This may not be very intuitive for people that are trying to determine exactly how much space is being used.

So fortunately, df comes with a -h command, which shows us this information in megabytes, gigabytes, and other human readable values. Let’s see how much space is available on the different file points that are in my Linux distribution. If I run the df command, indeed, it shows us 1K blocks, how many of those blocks are used, how many are available, and the percentage that’s in use. So at the mounting point, where it is the root of my drive, you can see that we’re using 19% of the drive.

But it’s very difficult to understand exactly how much room that really is. Let’s use df with the -h option, and you can see on that route Mount that it is a 63-gig partition. I’m using 11 gigabytes, which leaves us 49 gigabytes available for other applications. If you’ve ever had to look through a very large file for a very specific piece of information, then how much of a challenge that can be without some type of automated search function.

Fortunately, Linux includes this type of search in a command called grep. Grep will find a piece of text that’s located in any file that’s on the system, and you can use the syntax of grep with the pattern you’re looking for. And then the name of the file. Let’s look through a file to see how many times a particular word appears in an authentication log file. Let’s perform the grep command. I’ll specify the word opened because that’s the word I’m looking for inside of this file.

And the location of the file is in var/log, and it’s called auth.log. You can see there are a number of lines inside of this file that have the word opened. You can see where it’s highlighted where that name is. So you can see how many lines are inside of that single file that have the word that we were searching for with the grep command. If you’ve ever used the Windows Task Manager, then you can view a list of all of the different processes that are currently running in a Windows system.

There’s a similar command in Linux called ps, the process command, where you can view all of those processes that are happening behind the scenes. To view just the processes running for yourself, or your user name, you would simply use the ps command. But if you’d like to view all of the processes running on the system, you would use ps-d. You’re probably going to see a lot of processes go by so it might be a good idea to pipe the output to the more command.

There’s not many applications that are running on this Linux system right now so I’m going to simply use the ps option to see what’s running on my user. And we can see there is a bash. Process that’s the terminal process we see in front of us. You can also see that it shows the ps command is currently executing. So by running the ps command, we’re able to see that we are running the ps command. Let’s look at all the commands used by everyone on the system.

I’ll use ps with the -e option, and you can see a lot of information went by. So we’re going to hit the arrow up to use this command again, and I’m going to pipe the results of this into the more utility. Now, it’s going to show me the first page of the output, and it’s going to stop after the first page and put a more option down at the bottom. If I hit spacebar, it will move it to the next page, and it will wait with the more option at the bottom. I could also use the enter key, and it will move one line at a time.

And if I want to stop this from paginating and simply get back to the Linux prompt, I can use the q option. Well, as you can already tell, it might be a challenge to try to find exactly the process that you’re looking for. And if you want to get an update to the number of processes, you have to run the ps command again. Fortunately, there is a command very similar to the Task Manager inside of Windows called top. The top command will give you an overview of all of the processes running, and it shows you the percentage of CPU memory and other resources that are used by the individual processes.

This makes it very easy to find the processes using the most number of resources because it will put those processes at the top of the list. This also has a list of loads at the top. You can see this load average, and then there are three values. This is the load on this Linux system in one, five, and 15-minute interval. So you can get a view of historical use by simply looking over the load average numbers. The top command has extensive options and hot keys available, and you can get a list of what all of those might be by running the man top command.

Let’s run top on this Linux system and see what we get. We type top and hit enter, and you can see all of the different processes are being shown on the screen. And we’re getting updates every second that shows us the current status. If we hit the enter key, it will also give us a more responsive view of what’s going on. So if you don’t want to wait every second for this to refresh, you can refresh it manually with the enter key. In this view, we’re simply reading or viewing what the different processes happen to be doing on the system, but you can also kill, or halt, different processes from the top command.

Make sure you check the documentation if you would like to extend those capabilities and know exactly what keystrokes can provide you with that management function. If you would like to stop viewing the top information and get back to the Linux command prompt, we simply hit the q key for quit. There will certainly be times on a Linux system where you’re not able to find a particular file that you know is located somewhere in the file system.

Fortunately, Linux has the find command that can help you find a name or an extension of a file. You would simply use the find option. You would specify where you would like to begin this search. If you use the period, it starts in your current folder. Use the -name option, and then specify the name of the file you’re looking for. If you’d like to see all of the files that have a .txt extension, you could use a quote with an asterisk, .txt and end the quote.

Let’s confirm our working directory before we perform the find command. We’ll use the PWD command to print the working directory, and currently, we are working out of /home/professor. Now, I’d like to use the find command to find all of the text files that are located under /home/professor and all of the folders that are located within that, as well. Let’s try find with the dot to specify that we’re starting on our current directory. We want to find everything that has a particular name so we use -name. And then in quotes, we’ll put *.txt.

And if we hit enter, you can see all of the different files that are located in this entire set of folders that end with a .txt. One of the things you may notice when using a browser session is we rarely refer to a website with its IP address. It’s difficult to remember IP addresses for all of the different sites we might visit, but it’s very easy to remember their fully qualified domain name. For example, if you’d like to visit www.professormesser.com, simply type that into your browser.

And behind the scenes, there’s a resolution that occurs between the fully qualified domain name and the IP address that it needs to communicate with that web server. If you would like to see what this resolution looks like, you can use the dig command inside of Linux. This will provide you with a list of all of the IP addresses associated with a fully qualified domain name. It can also provide you with a fully qualified domain name if you provide it with an IP address.

Although dig is not automatically included with Windows, you can download and install a version of dig by visiting the ISC.org website under /downloads/bind. Let’s perform a name resolution and determine what the IP addresses might be for ProfessorMesser.com. I’ll run the dig command, and I’ll specify www.ProfessorMesser.com. If we hit enter, it tells us that we are running dig 9.18.1. Our question, or the query that we’re posing, is to find www.ProfessorMesser.com

And you can see that we got three answers from the name server with three different IP addresses that could all be used to connect to my web server. There may be times when you have information in one file and information in another file, and you would like all of that information to be in one single file. In Linux, there’s a command that allows you to concatenate these together, and that is the cat command. The syntax for the cat command is cat, the name of the first file, and the name of the second file.

If you don’t use any other parameters, it will take everything in both of those files and output all of that to the screen. If you would like, instead, to take the contents of those files and copy it to a new third file, you would use the same cat command with file one and file two, but you would use a greater than sign, which redirects everything that would normally go to the screen and redirects it to the name of a file. And the file that we would use would be, in this case, both.txt.

On our system, we do have two files, one that’s called file1.txt and another called file2.txt. Let’s concatenate those files together by using the cat command. We’ll specify file1.txt and then specify file2.txt. We use the greater than sign to redirect this to a file. Then we’re going to call this file both.txt. And if we now look at the directory, we can see that we have a file one and a file two, which is still there, but we have a new file that we’ve created called both.txt.

And you’ll notice the size of both.txt is exactly the same size as both the file one and file two added together. There may be times when you need to edit a file or change the configuration of a file inside of Linux, and one type of editor that you could use for this is the nano editor. The nano editor is very commonly included in most Linux distributions, and if it isn’t, can always install it as a separate application. This is a full-screen editor. Allows you to copy and paste, you can modify and see the results of the modification on the screen.

And using Control keys, you can get help, you can save files, and perform other functions within the editor. Let’s make some changes to the both.txt file that we created earlier. I’ll use the nano command and specify both.txt. Now, we’re in a full-screen mode with a cursor that we can use to move around. We can add lines and modify information on the screen. And when we want to write this out, or save this information, we would use the Control-O option to write out.

If I press that it, says what file name would you like to write it to? I’d like to write it to the same file, in this case both.txt. And it wrote 23 lines to the disk. To exit out of nano, we would use the Control-X option, and we’re back at the Linux command prompt. If nano is not available on your system, you can always use apt-get or yum to be able to install it on your current Linux distribution.