Basic Linux Commands – CompTIA A+ 220-902 – 2.1

The command line of Linux provides a significant amount of control and management of the operating system. In this video, you’ll learn about some of the most popular Linux commands to run from the command line.

<< Previous: Mac OS FeaturesNext: Client-side Virtualization >>


The Linux operating system has a lot of power at the command line. You would normally access this command line by using the Terminal, XTerm, or some similar terminal program. The commands that we’re going to look at in this video are very similar in both Linux, and Mac OS. That’s because Mac OS derived from a Unix version called BSD– Berkeley Software Distribution.

This video is going to be very specific to Linux however, so if you’re planning to do any of this in Mac OS, make sure you check the documentation. If you’d like to do this yourself, you can download what’s called a live CD, where you would connect a live version of an operating system that you can run from a CD, or DVD, rather than running it from your normal system.

Another popular option is to install virtualization software like VMware player, or VirtualBox, and install a Linux distribution in that virtual machine. And of course, if you’re already at the command line, you can always use the all important man command, this stands for manual. So if you use to for instance, man grep, it would show you all of the different options for the grep command.

In Linux you’re normally logging in, and running as a normal user. You don’t have any special supervisory rights, so sometimes you may need to run things in elevated mode. If you have a number of tasks that you need to execute as the superuser, you can start this mode by simply typing in su. And then everything else in the session will run as the superuser.

To get back to your original set of rights and permissions, you can type the exit command, and it will exit out of the super user mode. If you’d like to execute just one command in the super user mode, you can use the sudo command, and run this particular command as the super user. This will only run the single command as a super user, and once that command is completed , you’re back to your normal rights and permissions.

One popular Linux command is the ls command. This gives us a listing of directory contents. If you are in Windows, you would be using the dir command to provide a similar function. This will list files and directories, and your terminal may even support color coding. For example, in mine, the blue color is a directory, and the red color is an archive file.

If you want to see the details of a directory, you can use the ls command with a -l, this will show you a lot more details about what’s in the directory, and often as many pages. So you may want to pipe this through the more command by using the vertical bar, and then more, so that you can page through one at a time. The grep command is a search function that allows you to look for some text that may be located in one or more files.

To use grep, we would use the grep command, we would specify a pattern, and then specify what files we wanted to look inside. For example, if you had a file off.log, and you were looking for the word failed, you could grep the word failed from off.log, and any lines that showed up with the word failed would be listed as the results of that grep. The cd command in Linux is very similar to the cd command in Windows that allows you to change directory.

One difference in Linux is that we are using forward slashes to separate directories, rather than the backward slashes you might use in Windows. So if you wanted to change directory to the var/log directory, you would use the cd command space /var /log. Another similar command between Windows and Linux, is the shutdown command. This allows you to shut down, or restart, a Linux device.

To use the shutdown command, we need to specify how many minutes the countdown will be so. It’s common to do something like shut down two, and it will wait two minutes before the shutdown. You usually are going to run this as a super user. So to run shutdown, you’ll probably use this as sudo shutdown and then the number of minutes.

If you wanted to restart the computer, rather than shutting it down completely, you can use exactly the same command, but use the -r. So, sudo shutdown -r two means that we’re going to shut down in two minutes, but then the system will restart after that. If any time in the middle of this countdown, you feel that you don’t want to shut down the system, or you don’t want to restart it, you can always stop this entire process by using the Control-C command, and it will cancel the shutdown process.

When you’re using the Linux command line, or moving back and forth from one system to another, you may not realize exactly what directory you happen to be in at any particular time. If that’s the case, you can use the pwd command. This means Print Working Directory , and by typing pwd, and hitting Enter, it will tell you exactly what directory you’re in. This is very different than the passwd command. Passwd is when you would like to change the password on an existing account.

So, if you wanted to change your password, you would type in passwd. It would say that it’s changing the password for the current user, in my case professor. I would put in my current password, I would enter a new password, retype the new password, and then it tells me the password is updated successfully. If you’re the administrator, you could also use the passwd command with a separate username, and change another user’s password.

The mv command stands for moving a file. And if you’re not moving a file from one directory to another, you can move a file from one name to another, or rename the file. One way to do this is to type the mv command, the source of the file that you’d like to move, and then the destination. So if you wanted to change the name of the file called first, to a file name called second, you would use mv, first.txt second.txt.

Here’s an example of this– I listed out the directory that shows that first.txt file. I ran the command mv first.tx second.txt, and you can see as I list the files again, first.txt is no longer in that directory. Instead it has been moved, or renamed, to second.txt. The mv command is also used for moving a file to different places in the directory structure. So if you’re moving a file, you can simply move it from one folder to another with the mv command.

Instead of moving a file, or renaming it, why don’t we copy a file from one file name to another. To do that, we use the cp command. The syntax for this is cp, the source, and then the destination name. So cp first.txt space second.txt will copy the contents of the first file into a second file.

So we’ll take the example I used earlier, where I had a first.txt file. I use the copy command– cp first.txt second.txt– and if we list the directory now, we can see there is a first.txt and there is also a second.txt file which is identical to the contents of first.txt.

To delete a file from the file system, we would use the rm command. This stands for removing a file. We can remove individual files by specifying rm him and the name of the file. We can also remove an entire directory, and everything inside of it, by using rm -r.

You want to be very careful when using that, because you’re removing a lot of information at one time, and you can accidentally remove files you weren’t expecting. If we wanted to remove an empty directory by itself, we can use the rmdir command and that will remove just a directory that’s already empty.

Here’s an example of using the rm command. If I list out a directory, you can see temp-1 and temp-2. If I use rm then temp-1.doc, and perform another ls, you’ll see that the temp-1.doc is no longer part of the file system. To make a directory in Linux, we use the mkdir command.

This will make a folder that we can then store files into. And you would use the syntax mkdir and the name of the directory. So if you wanted to create a directory called notes, you simply mkdir space notes.

If you’ve ever need to change the read or write permissions of a file in Linux, then you’ve probably used the chmod command. Chmod changes the mode. That’s where the name comes from of that particular system object, and you’re usually changing the mode to be either read, write, or execute. You can also use an octal notation, or even specify other parameters, in the change mode command.

It’s very common to use numbers to specify the permissions for a file. If you were to look at all of the different permissions, you’ll notice there are a bunch of Rs and Ws and Xs and dashes. These are separated out into a user, a group, and an other section. And you would set the user group, and other number, based on the permissions that you see here.

So if we set a chmod of 744 for the file script.sh, we are setting the first three characters here to RWX. That’s the 7, so that’s read, write, and execute, and that will be the permissions for the person who is the owner of the file. Everyone who is a member of the group associated with this file, will be assigned the number four. Four is read only, and you can see the r is listed under those characters.

And then everyone else has the access also of four, which means everyone else can read this file as well. There are many different forms of syntax for the chmod command. We just looked at the one that use numbers to specify the user, the group, and the other permissions. You can also use letters.

So a chmod of a-w for the file means that all users would not be able to write to first.txt. We could try even chmod u + x, which means that the user, or the owner of the file, can execute the file. Whenever you’re working at the command line with these files, and you need to change these rights and permissions, the chmod will be the utility that you use to do that.

In Linux, every file is owned by a particular user, and there are also rights and permissions associated with a group that may be associated with that file. For example, in my system, I have a script.sh, and you can see there is a backup, and staff, listed as part of the file name. That means that this file is owned by the user backup, and anybody who is in the staff group has also rights and permissions associated with this file.

To be able to change the file owner, or the group owner, we would use the chown command, and usually you have to use elevated rights. So we would use a sudo chown, and then specify the owner, and the group, and then what the file name Is.

So if we want to change the ownership of a file, we may want to look at who owns the file currently. If we use the ls -l command, we can see that script.sh is owned by backup, and is part of the group staff. But if we use the sudo chown professor script.sh, it’s now going to change the user from backup, to now owned by professor.

If you want to view, or change, any of the wireless configuration settings of a wireless adapter inside of your Linux device, you want to use the iwconfig command. For example, if you want to connect to an SSID, you can specify iwconfig, the name of the network adapter, in this case my wireless network adapter is eth0, specify that you want to connect to an ESSID, and then name the SSID that you’d like to connect to.

If you want to see the IP address configurations of any of your network adapters, you can use ifconfig. For example, ifconfig eth0 will give you all of the information for that particular adapter card. So you can see IP address, broadcast information, subnet mask, and much more.

The ps command lists out all of the current processes, and process IDs. This is very similar to looking at processes that might be in the Windows Task Manager. To be able to view these processes, we simply use the ps command by itself.

If you’d like to view every process for every user, we can use ps -e. And if you want to pipe it to more, you’ll be able to page through, because you’ll probably see a large number of processes go by.

One common command to install, update, or remove applications from the command line is apt-get. Apt stands for Advanced Packaging Tool. This allows you to easily find packages, and install them into your Linux distribution. For example, if you wanted to install Wireshark you would run sudo, apt-get, install, and then the name of the application you’d like to install– such as wireshark.

Vi is the visual mode editor. And it’s a popular editor that’s found in almost every Linux distribution. To start the visual editor, use the vi command, and then the filename that you’d like to edit. For example, vi script.sh.

Once you’re inside the editor, you can insert text by pressing the I key, and then specifying what text you’d like to have input. When you’re finished with the input process, you usually will press Escape to get out of input mode. If you like to then save the file, you can use a colon command with wq, which stands for write and quit, and it will exit the editor.

The dd command is used for converting, and copying, files. But it can also be used to backup, and restore, entire partitions. The syntax of the dd command uses an input file name for and if, then you specify the file name, and an output file name, or of.

And if you want to add additional options, you can do that as well. If you’d like to create a disk image for example, you would use the dd command, an input file, and the name of that particular partition, for instance SGA, and then the output file.

In this case, we can put it in the temp directory, and specify the image file name here. To be able to restore from that image, we simply reverse the process with dd. The input file would then be the image, and the output file would be that partition name.