Transcript for:
PS 15 Linux Commands

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 could run in Mac OS. That's because Mac OS 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 dash 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 dash 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 dash 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, 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 slash home slash 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 fi 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.txt. 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.txt 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 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 0 and 7, 7 being read, write, and execute, which is effectively full permission, and a 0 would be none of those or no permission. So in this example where we're changing the mode of script.sh to 744, the 7 means that the user will have read, write, and execute permission. Anyone who belongs to the group that owns that file will have 4, which is read-only permission. And everyone else who's neither a user or a group will also have the permission of 4, 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 1 column, the W is in the 2 column, and the R is in the 4 column, you can see that having 4 plus 2 plus 1 is equal to 7. In the case of the number 4, read only, we just have a single character in the fourth column, which is why that one is designated as number 4. So if we were to use the chmod command with a 744 on file first.txt, that would mean that the user would be associated with the 7 or read, write, and execute access. The group would be associated with the 4, which is read only access. And everyone else would have access of 4 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 dash w means we're going to disable any write functionality to that particular file. Here's a similar command, chmod u plus 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. It 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 6. Our group remains at 6. And for the last three values, we want to enable the R column, which is in the 4 column. So I'll specify 4. 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 file. It's now rw-rw-r-dash. 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. And 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.text. 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. We'll 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 in Windows to run a command as administrator. In Linux, the administrator is the super user, 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 super user 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 super user, you could start the session with the SU command. And now every subsequent command you type in will always run as the super user. When you're finished running commands as the super user, 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. App 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. It will 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 yellow dog 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 We also have another adapter on this device, which is an ethernet adapter. This ethernet adapter name is ENP0S5, and you can see the IP address associated with this adapter is 10.1.10.65 with a subnet mask of slash 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 dash 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 dash H option, and you can see on that root 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, you know 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 slash log, and it's called auth dot log. You can see there are a number of lines inside of this file that have the word opened. 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 know 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 username, 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-E. 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 dash 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 the 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. 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 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 a 1, 5, 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 hotkeys 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 can 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 dash 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 dot text extension, you could use a quote with an asterisk, dot text, 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 slash home slash professor. Now I'd like to use the find command to find all of the text files that are located under slash home. slash 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 dash name. And then in quotes, we'll put star dot text. 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 dot text. 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 slash downloads slash 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. 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 file1 and a file2, 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, you 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 appget or yum to be able to install it on your current Linux distribution. Resource 2: Basic Linux A+ Command List Objective 1.9 of the CompTIA A+ Core 2 (200-1002) exam calls out 20 different Linux/Mac OS commands. Those *nix commands are: ls grep cd shutdown pwd passwd mv cp rm chmod chown iwconfig ifconfig ps su sudo apt-get vi dd kill That gives us a specific set of commands to focus our efforts on. Before we jump into examples, let's cover some basic information on switches and man pages. Understanding Linux Commands: switches/options You'll notice that some of our command examples include a dash "-" followed by some characters, like ls -l. The dash plus additional characters are commonly referred to as "switches" or "options". These switches are optional (i.e. you can use the command without them). Generally, a switch will modify what a command does in some way. For example, simply adding the -l switch to ls tells us a whole lot more about file permissions. Often, you can combine switches to apply multiple options at once. Understanding the concept of switches is an important part of mastering Linux commands. Understanding Linux: man/info & help There are tons of different options and ways to use even basic Linux commands. Here, we'll focus on common usage and avoid getting too far into the weeds. However, it's important to know you can get detailed information on command usage straight from a Linux terminal. Generally, this will involve typing man (e.g. man ls), info , or command –help. You can also find man pages online, for example this fairly long list on man7.org. Don't worry about mastering all the different usage scenarios now. Just know that there is a place to look if you ever get stuck working with a command. Linux Commands: Prompts, Home Directories, Examples When you are looking at a Linux terminal, you'll see a prompt that usually ends with a dollar sign $ (for normal users) or a pound sign # (for the superuser or root user). Often that prompt is in the form of username@computername:<current working directory> (~ means the user's home directory) You'll get used to this fast, but for beginners it's important to note that the commands are everything after the prompt. For example, in this article, we're using a user named "cooluser" on an Ubuntu install. Therefore, preceding most of our commands you'll see this: :~$ Remember: There is no need to type anything before the $ if you're practicing with the examples here. Your prompt will look a little different, usually with a username behind it. The prompts you see in other online examples may differ as well. Some may have different prompts. Some may exclude the prompt altogether. Others may just use the $ or # to indicate if commands are run as a normal user or a superuser/root user. Avoiding Typos: Tab Completion is Your Friend When you're working in a command line, it's easy to make typos. On most modern Linux systems, tab completion can help minimize typing errors. The way tab completion works is simple: hitting the TAB key will complete as much of the command as possible for you given what you have typed. This is particularly useful when dealing with different paths on a system. For example, if we were going to change directory (using cd) from /home/cooluser/ to /home/cooluser/subdirectoryone and there was no other file or directory starting with an "s" in /home/cooluser/, simply typing cd s and pressing the TAB key will autocomplete to cd subdirectoryone. 20 Linux Commands: Explanations and Examples With those prerequisites and tips out of the way, let's dive into the list of Linux commands for the A+ exam. For each command, we'll provide an explanation and examples to help you get started. We've combined a few of the entries, specifically "pwd vs password", "iwconfig/ifconfig", and "su/sudo", to be consistent with how they are listed on the A+ exam objectives. Command 1: ls ls is a very common Linux command. It is used to list files and directories within a directory. When used with different switches, ls can tell you a lot about files and permissions. ls by itself prints (displays) all the files and directories in your current working directory: :~$ ls accouting.csv coolpictures.png interestingscript.sh learningnotes.txt logessay.docx :~$ ls /path/to/another/directory/ lists all the files and directories in /path/to/another/directory/ (fill in the path to make sense for your system). You can use this same /path/to/another/directory/ approach with other switches and other commands as well. In this example, we list the files in /tmp/tempfiles/: :~$ ls /tmp/tempfiles/ draftscript.sh interestingstory.tmp knowledge.nugggets :~$ ls -l lists files and directories as well as their permissions, user that owns the file/directory, group that owns the file/directory last modified date/time, and the total file system blocks used. :~$ ls -l total 0 -rw-rw-rw- 1 cooluser cooluser 0 Jun 24 21:50 accouting.csv -rw-rw-rw- 1 cooluser cooluser 0 Jun 24 21:50 coolpictures.png -rw-rw-rw- 1 cooluser cooluser 0 Jun 24 21:50 interestingscript.sh -rw-rw-rw- 1 cooluser cooluser 168 Jul 3 13:31 learningnotes.txt -rw-rw-rw- 1 cooluser cooluser 0 Jun 24 21:50 logessay.docx :~$ ls -a includes hidden files, those that start with a ".", in the ls output. :~$ ls -a .bash_logout .config .motd_shown .sudo_as_admin_successful coolpictures.png logessay.docx .bashrc .landscape .profile .viminfo interestingscript.sh .bash_history .cache .local .ssh accouting.csv learningnotes.txt Command 2: grep grep looks for specific patterns in text files or other input such as the output of a command. Specifically, grep looks for and displays the lines that match a given regular expression a.k.a. regex. For example, to find the word "sysadmin" in the plaintext file "learningnotes.txt" we can use grep "sysadmin" learningnotes.txt :~$ grep “sysadmin” learningnotes.txt sysadmin sysadmin's work hard sysadmins study a lot sysadmins are admins of systems :~$ If we wanted to narrow that down to times we typo'd sysadmins with an apostrophe, we can use grep “sysadmin’s” learningnotes.txt :~$ grep “sysadmin’s” learningnotes.txt sysadmin’s work hard :~$ To do a case insensitive search, we can use the -i switch, like this: grep -i “sysadmin” learningnotes.txt (notice in this check, we also found "SysADmIn"). :~$ grep -i “sysadmin” learningnotes.txt sysadmin sysadmin’s work hard sysadmins study a lot sysadmins are admins of systems SysADmINn :~$ It's also very common to use grep to parse the output (stdout) of a command. To do that, we simply type the command, a pipe "|", and then the grep command we want to apply. For example, ls | grep “.txt” will print only files with .txt in their name in the current working directory. :~$ ls | grep “.txt” learningnotes.txt :~$ Pro-tip: In practice, a command like find is often better than using grep and ls together. Command 3: cd cd is used to "change directories". If you need to navigate to different directories, cd is likely the tool you'll want. When working with cd, it is important to understand absolute paths vs relative paths. Absolute paths start with a leading /. Relative paths do not start with a leading /. The leading / is the root directory on your system. With relative paths, cd starts from your current working directory. Let's use an example to get a feel for cd and paths. In our example, our home directory is /home/cooluser. In that directory, there is one subdirectory, /home/cooluser/subdirectoryone. In /home/cooluser/subdirectoryone there is one more subdirectory, /home/cooluser/subdirectoryone/subdirectorytwo. Let's start at /home/cooluser. From here, we can cd subdirectoryone to enter subdirectoryone (pwd as we'll see in a subsequent section prints the current working directory). :~$ pwd /home/cooluser :~$ cd subdirectoryone/ :~/subdirectoryone$ pwd /home/cooluser/subdirectoryone :~/subdirectoryone$ From here, we can cd subdirectorytwo to move to the next subdirectory. :~/subdirectoryone$ cd subdirectorytwo/ :~/subdirectoryone/subdirectorytwo$ pwd /home/cooluser/subdirectoryone/subdirectorytwo :~/subdirectoryone/subdirectorytwo$ Now that we're in /home/cooluser/subdirectoryone/subdirectorytwo, let's try to get back to subdirectoryone with cd subdirectoryone :~/subdirectoryone/subdirectorytwo$ cd subdirectoryone -bash: cd: subdirectoryone: No such file or directory :~/subdirectoryone/subdirectorytwo$ Interestingly, we get a "cd: subdirectoryone: No such file or directory" error. But we know subdirectoryone exists. We also know cd subdirectoryone got us to /home/cooluser/subdirectoryone just a few minutes ago. So, what's the difference now? In our first attempt, subdirectoryone was within our current working directory. That is, it was available to cd to relative to where we were at (/home/cooluser). Once we were in /home/cooluser/subdirectoryone/subdirectorytwo, there is no subdirectoryone relative to our current working directory, so the cd command fails. We can address this problem by using cd with the absolute path of /home/cooluser/subdirectoryone, like this cd /home/cooluser/subdirectoryone. Alternatively, we can use cd .. to move "up" one directory, like this: :~/subdirectoryone/subdirectorytwo$ pwd /home/cooluser/subdirectoryone/subdirectorytwo :~/subdirectoryone/subdirectorytwo$ cd .. :~/subdirectoryone$ pwd /home/cooluser/subdirectoryone :~/subdirectoryone$ To move to the "root" directory of a system, use cd / :~$ pwd /home/cooluser :~$ cd / :/$ pwd / :/$ Command 4: shutdown shutdown as you may expect, powers off, halts, or reboots the system depending on the switches you use. The general usage for shutdown is shutdown [options] [time] [wall]. "Wall" is a message to be displayed to users when the shutdown begins. shutdown by itself is used to power down the system (usually with a default one-minute delay). :~$ shutdown shutdown -H does a halt instead of a power down. :~$ shutdown -H shutdown -r kicks off a reboot :~$ shutdown -r shutdown now or shutdown +0 shuts down the system immediately. Without the one-minute delay. :~$ shutdown now :~$ shutdown +0 shutdown -r +5 begins a reboot of the system in five minutes. :~$ shutdown -r +5 shutdown -c cancels a pending shutdown command. :~$ shutdown -c Command 5 & 6: pwd vs. passwd Sometimes, password is abbreviated as pwd. Therefore, you may expect that the pwd and passwd commands do something similar, but they don't. pwd displays the present working directory. passwd is used for changing passwords. For example, if we go back to our previous directory examples, when we are in /home/cooluser, pwd will print that to the screen. :~$ pwd /home/cooluser :~$ The passwd command on the other hand will bring us the prompt to change our own password. :~$ passwd Changing password for cooluser. Current password: New password: Retype new password: passwd: password updated successfully :~$ If we wanted to change a password for another user named "seconduser" we need sudo privileges, which we have (more on sudo below), and then we just enter sudo passwd seconduser and follow the prompts. :~$ sudo passwd seconduser [sudo] password for cooluser: New password: Retype new password: passwd: password updated successfully :~$ Command 7: mv The mv command is used to move (not copy) files from one place to another. Because of how it works, mv is also useful for renaming files. mv has a variety of options related to backups and overwriting or not overwriting files, but basic usage is as simple as. :~$ mv In the example below, we have a "coolpictures.png" file in our current working directory we want to rename to "awesomepics.png". We can do that with the command mv coolpictures.png awesomepics.png. :~$ ls accouting.csv coolpictures.png interestingscript.sh learningnotes.txt logessay.docx subdirectoryone :~$ mv coolpictures.png awesomepics.png :~$ ls accouting.csv awesomepics.png interestingscript.sh learningnotes.txt logessay.docx subdirectoryone :~$ You can use mv with absolute or relative paths. To move our "accounting.csv" file to /tempdir we can use the command mv accounting.csv /tmpdir. :~$ ls accounting.csv awesomepics.png interestingscript.sh learningnotes.txt logessay.docx subdirectoryone :~$ mv accounting.csv /tmpdir :~$ ls /tmpdir/ accounting.csv :~$ ls awesomepics.png interestingscript.sh learningnotes.txt logessay.docx subdirectoryone :~$ Similarly, to move our "longessay.docx" file to /home/cooluser/subdirectoryone and rename it "greatessay.docx" while in /home/cooluser, we can use mv longessay.docx subdirectoryone/greatessay.docx. :~$ ls awesomepics.png interestingscript.sh learningnotes.txt longessay.docx subdirectoryone :~$ mv logessay.docx subdirectoryone/greatessay.docx mv: cannot stat ‘logessay.docx’: No such file or directory :~$ ls awesomepics.png interestingscript.sh learningnotes.txt longessay.docx subdirectoryone :~$ mv longessay.docx subdirectoryone/greatessay.docx :~$ ls subdirectoryone/ greatessay.docx subdirectorytwo :~$ ls awesomepics.png interestingscript.sh learningnotes.txt subdirectoryone :~$ To move all the files in a directory, you can use an asterisk * can be used as shorthand for "everything here". For example, to move the contents of our current working directory to /tmpdir we can use the command mv * /tmpdir. :~$ ls /tmpdir/ accounting.csv :~$ ls awesomepics.png interestingscript.sh learningnotes.txt subdirectoryone :~$ mv * /tmpdir/ :~$ ls /tmpdir/ accounting.csv awesomepics.png interestingscript.sh learningnotes.txt subdirectoryone :~$ ls :~$ Command 8: cp Once you understand mv, it's easy to understand cp. They work similarly, but cp copies instead of moves files. Going back to our original mv coolpictures.png awesomepics.png. example, we can use cp coolpictures.png awesomepics.png to make a copy of "coolpictures.png" named "awesomepics.png" without getting rid of the original. :~$ ls coolpictures.png interestingscript.sh learningnotes.txt longessay.docx subdirectoryone :~$ cp coolpictures.png awesomepics.png :~$ ls awesomepics.png coolpictures.png interestingscript.sh learningnotes.txt longessay.docx subdirectoryone :~$ Command 9: rm rm is used to delete or "remove" files. Use caution when working with rm, as you won't always have the benefit of something like a recycle bin on Linux systems. To remove the file "junkfile.txt" that is in our current working directory, we can use the command rm junkfile.txt :~$ ls awesomepics.png coolpictures.png interestingscript.sh junkfile.txt learningnotes.txt longessay.docx subdirectoryone :~$ rm junkfile.txt :~$ ls awesomepics.png coolpictures.png interestingscript.sh learningnotes.txt longessay.docx subdirectoryone :~$ If the file was in a different directory, we could use absolute and/or relative paths to delete it. Like many other commands, rm can be used with the asterisk wildcard character. If you need to delete a directory with files in it, rm without any switches won't cut it. You can (with caution!), use rm -r though. For example, from /home/cooluser/subdirectoryone we can use rm -r subdirectorytwo to delete /home/cooluser/subdirectoryone/subdirectorytwo and its contents. :~/subdirectoryone/subdirectorytwo$ cd .. :~/subdirectoryone$ ls subdirectorytwo :~/subdirectoryone$ rm -r subdirectorytwo/ :~/subdirectoryone$ ls :~/subdirectoryone$ Command 10: chmod Linux file permissions can get a bit complex, which is why we wrote a piece that focuses on chmod and chown[a]. For now, let's focus on the basics of these two commands. Here we'll look at chmod, which is used to modify the read, write, and execute permissions on a file. For example, suppose we wanted to add "execute" permissions for all users to our "interestingscript.sh" file. We can use the command sudo chmod +x interestingscript.sh to do that. As you can see in the example below, the "x" (for execute) permission was added for the user that owns the file (the "x" that is the fourth character in the ls -l interestingscript.sh output), group that owns the file (seventh character), and everyone else (tenth character). :~$ ls -l interestingscript.sh -rw-rw-rw- 1 cooluser cooluser 0 Jul 3 15:10 interestingscript.sh :~$ sudo chmod +x interestingscript.sh [sudo] password for cooluser: :~$ ls -l interestingscript.sh -rwxrwxrwx 1 cooluser cooluser 0 Jul 3 15:10 interestingscript.sh :~$ Command 11: chown chown is how you configure who owns a file. Sticking with our "interestingscript.sh" example, suppose we wanted to change the user that owns the file to "seconduser" and the group to seconduser's group. For that, we can use the command sudo chown seconduser: interestingscript.sh. :~$ ls -l interestingscript.sh -rwxrwxrwx 1 cooluser cooluser 0 Jul 3 15:10 interestingscript.sh :~$ sudo chown seconduser: interestingscript.sh :~$ ls -l interestingscript.sh -rwxrwxrwx 1 seconduser seconduser 0 Jul 3 15:10 interestingscript.sh :~$ If we wanted to just change the user that owns the file, but not the group, we could have omitted the colon :. i.e. use the command sudo chown seconduser interestingscript.sh. Command 12 & 13: iwconfig vs ifconfig Before we dive in here, it's worth noting that the ip command is becoming more and more common on modern Linux systems. In production environments, you may want to get used to using ip instead of ifconfig. In fact, on many modern Linux distributions, you may need to manually install iwconfig and ifconfig. That said, let's focus on the two called out by the A+ exam objectives for now, iwconfig and ifconfig. Both of these commands are used to configure and obtain information on network interfaces. The key difference is that iwconfig focuses specifically on wireless interfaces while ifconfig is a general utility. There is plenty you can do with iwconfig and ifconfig, but we'll focus on the basics here. To display network interface information such as IP address, broadcast address, and subnet mask for all active interfaces, simply enter the ifconfig command with no switches. :~$ ifconfig Similarly, to display wireless specific information, like signal level, managed frequency, and access point MAC addresses, simply enter the iwconfig command :~$ iwconfig To display information on ALL interfaces (active or inactive) use ifconfig -a :~$ ifconfig -a Command 14: ps ps command is used to get information on process status for running processes. This command can get a bit confusing because there are two approaches to options. The BSD (Berkeley Software Distribution) style options do not use a dash. On the other hand, Linux/Unix/GNU style options that do use dashes. To keep things consistent, we'll focus on the *nix style here. ps by itself displays all the running processes for the current shell. Here, we can see the PID (Process ID), associated terminal (TTY), amount of CPU time used (TIME), and specific command (CMD) associated with each process. :~$ ps PID TTY TIME CMD 4 tty1 00:00:00 bash 72 tty1 00:00:00 ps ps -e and ps –A give similar output to ps but includes all active processes. :~$ ps -e PID TTY TIME CMD 1 ? 00:00:00 init 3 tty1 00:00:00 init 4 tty1 00:00:00 bash 73 tty1 00:00:00 ps :~$ ps -A PID TTY TIME CMD 1 ? 00:00:00 init 3 tty1 00:00:00 init 4 tty1 00:00:00 bash 74 tty1 00:00:00 ps :~$ Adding the -F switch will provide more detail on running processes. Specifically, with ps -F now we see the user ID of the process owner (UID), the process ID (PID), the parent process's PID (PPID), CPU usage (C), set size of memory allocated (SZ), resident set size (RSS) which is the actual memory in use in kilobytes, processor associated with the process (PSR), time the process started (STIME), and the specific name of the command (CMD). :~$ ps -F UID PID PPID C SZ RSS PSR STIME TTY TIME CMD cooluser 4 3 0 4521 3616 0 11:51 tty1 00:00:00 -bash cooluser 76 4 0 4662 1888 0 12:13 tty1 00:00:00 ps -F :~$ Like with other commands on the list, you can combine options to further modify the output of ps. Command 15 & 16: su/sudo The command su is used to switch to a different user. The command sudo is used to run a command as a specific user, buy default the superuser or root. Generally, to use sudo a user must be granted specific permissions, often in a file at /etc/sudoers. Let's take a look at a few su and sudo examples. Note: use caution when running commands as a "root" user or superuser. If you don't have a specific reason to do so, try and avoid running commands with elevated privileges. To run a command as root, we'll use sudo and supply our password when prompted. Here, we'll use the whoami command which simply prints the current user's username to the screen. Remember that the specific command we use is arbitrary and sudo can be used with most other commands. :~$ sudo whoami [sudo] password for cooluser: root :~$ whoami cooluser :~$ To run a command as "seconduser" we can use the command sudo -u seconduser and enter our password when prompted. :~$ sudo -u seconduser whoami [sudo] password for cooluser: seconduser :~$ If we actually want to switch the account we are using, that's where su comes in. For example, to switch to our "seconduser" account, we can use the command su seconduser and input the password for the "seconduser" account. To exit running our shell as "seconduser", we simply type exit. :~$ su seconduser Password: $ whoami seconduser $ exit :~$ whoami cooluser :~$ Command 17: apt-get apt-get is a command used for package management on systems like Debian and Ubuntu that use .deb packages. If you want to do things like install, remove, or update packages, apt-get can help. It's important to remember that apt-get looks for packages in specific repositories. That means in certain situations, you may need to modify the default repositories your system uses or add additional options to your commands. Often, you can find a list of repositories for your system at "/etc/apt/sources.list". To keep things simple we'll focus on the basic apt-get commands. In many cases, you'll need to run apt-get as the superuser to have it work, so we'll begin our examples with sudo. To install a package from a default repository, you can use the command sudo apt-get install . For example, to install the Firefox web browser on Ubuntu, we can use sudo apt-get install firefox. :~$ sudo apt-get install firefox … and confirm when prompted. 0 upgraded, 55 newly installed, 0 to remove and 0 not upgraded. Need to get 64.8 MB of archives. After this operation, 264 MB of additional disk space will be used. Do you want to continue? [Y/n] y Similarly, to remove (uninstall) a package, we can use sudo apt-get remove . Therefore, to uninstall Firefox, we can use sudo apt-get remove firefox. :~$ sudo apt-get remove firefox … and confirm when prompted. The following packages will be REMOVED: firefox 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 203 MB disk space will be freed. Do you want to continue? [Y/n] y For upgrades, updates, and cleaning up old packages, these three commands come in handy: sudo apt-get update fetches available updates sudo apt-get upgrade installs select updates sudo apt-get autoremove removes old packages deemed no longer required Command 18: vi vi is a text editing program that has been around for a long time. Many will tell you they favor other text editors, like nano or vim, but you'll still find vi in the wild today. Here's a quick crash course on vi. To open a file in your current working directory for editing, type vi . For example, to edit our "learningnotes.txt" file, we can enter the command vi learningnotes.txt. :~$ vi learningnotes.txt That will open the currently blank file in the vi text editor. Before we begin adding text, we'll press the INSERT key on our keyboard to enter vi's insert mode. Now that we're in insert mode, we can use our keyboard to type some text. When we're ready to exit and save the file, first we press the ESC key to exit insert mode. Then we type :wq and press ENTER to save and exit (or write & quit). That's it, our changes have been saved to "learningnotes.txt". We can use cat learningnotes.txt to print the contents of "learningnotes.txt" to the screen and verify. :~$ cat learningnotes.txt These are the things I learned today: * Thing 1 * Thing 2 * Thing 3 This is the stuff I want to learn next: * Thing 4 * Thing 5 * How to make a pepper n’ egg sandwich :~$ Command 19: dd dd is the data duplicator command and it is used for converting and copying files. Use cases for dd include creating ISO files, creating bootable USB drives, copying master boot records (MBRs), backing up drives, and converting the case of a text file. Because dd is a powerful tool, generally only the superuser can use it. The basic usage of dd is dd if= of= [options] Let's look at a few examples. To convert the "learningnotes.txt" in our current working directory file from our vi example to a version named "LEARNINGNOTES.TXT" with all capital letters, we can use sudo dd if=learningnotes.txt of=LEARNINGNOTES.TXT conv=ucase. :~$ sudo dd if=learningnotes.txt of=LEARNINGNOTES.TXT conv=ucase 0+1 records in 0+1 records out 168 bytes copied, 0.0042432 s, 39.6 kB/s :~$ cat LEARNINGNOTES.TXT THESE ARE THE THINGS I LEARNED TODAY: * THING 1 * THING 2 * THING 3 THIS IS THE STUFF I WANT TO LEARN NEXT: * THING 4 * THING 5 * HOW TO MAKE A PEPPER N’ EGG SANDWICH :~$ To convert "LEARNINGNOTES.TXT" to an all lowercase version named "lowercasenotes.txt" we can use sudo dd if=LEARNINGNOTES.TXT of=lowercasenotes.txt conv=lcase :~$ sudo dd if=LEARNINGNOTES.TXT of=lowercasenotes.txt conv=lcase 0+1 records in 0+1 records out 168 bytes copied, 0.0032191 s, 52.2 kB/s :~$ cat lowercasenotes.txt these are the things i learned today: * thing 1 * thing 2 * thing 3 this is the stuff i want to learn next: * thing 4 * thing 5 * how to make a pepper n’ egg sandwich :~$ While these are very basic examples, dd is quite powerful and can be useful for more practical system administration tasks like creating backups or changing the encoding of a file. Command 20: kill kill is used to stop a running process. If you're familiar with stopping a process using taskkill or Task Manager in Windows, kill should be easy to understand. The most basic syntax of kill is: :~$ kill To get the PID of a process with commands we've already covered, ps will be helpful. Below, we ran ps -a and noticed a ping running that we want to kill. :~$ ps -a PID TTY TIME CMD 4 tty1 00:00:01 bash 11801 tty2 00:00:00 bash 11835 tty1 00:00:00 ping 11836 tty2 00:00:00 ps :~$ Because the associated PID is 11835, we simply use the command kill 11835 and afterwards, the ping is no longer running. :~$ kill 11835 :~$ ps -a PID TTY TIME CMD 4 tty1 00:00:01 bash 11801 tty2 00:00:00 bash 11837 tty2 00:00:00 ps :~$ Pro tip: If you already know the name of the process you need a PID for, many Linux systems already have the pidof command that can simplify things. Simply use pidof . For example, to get the PID of top, we can use pidof top. :~$ pidof top 11838 :~$ Final Thoughts: There's Plenty More to Learn It may seem like the Linux commands on the CompTIA A+ are a lot to learn, but with some practice it can be done.