The course is part of this learning path
Linux Command Line Byte Session
This Linux Command Line Byte Session course from Cloud Academy will teach you a range of applied skills from operating Linux instances and devices, using only the command line, to using command-line functionality to get help using and discovering commands and understanding how to use common Linux commands for working with files and directories.
Linux is the cloud's dominant operating system. Even Microsoft, whose well-known for its Windows operating system, reports that Linux is running on the majority of Azure implementations - and that share is only growing. When you work with Linux in cloud environments, you usually don’t use a full-blown graphical user interface that you might be accustomed to when you think of your own computer’s desktop. Instead, you are most likely going to be using a text-based terminal or command line. This course, along with its accompanying laboratory will equip you with the right tools to successfully work with Linux cloud instances.
This course is designed to bring you up to speed on the command line and hone the necessary skills that will allow you to meet the prerequisites for Cloud Academy Labs involving the Linux command line. You are encouraged to perform the corresponding Lab while you complete this course to gain hands-on experience with what is shown in the course. Learning more about Linux and the command line could open up big opportunities for you and in your work environment. Linux and command line proficiency are a staple of many Cloud and DevOps engineer positions. Being able to efficiently perform tasks and automate repetitive jobs will be a springboard for success on your journey to mastering Linux and command line applications.
After completing this course you will:
- Understand what the Linux command line is
- Know how to issue commands at the command line
- Use common commands for working with files and directories
- Gain hands-on experience working at the command line (By completing the accompanying Lab)
- Anyone getting started with Linux at the command line or looking to review the basics of working at the command line in Linux.
- There are no prerequisites to this course apart from being aware that Linux is a free and open source operating system that is an alternative to Windows and Mac operating systems.
This course has a corresponding Cloud Academy Lab that provides you with a Linux command line in your web browser so you can follow along with the course. Everything that I'll be demonstrating in the course is in the lab for you to follow along with at your own pace. The lab also has some additional challenges for you to complete, if you are up to it. We'll talk more about the lab in a bit, but first I wanna say a few things about why it's important to be comfortable at the command line in Linux. Linux is the dominant operating system in the cloud, even Microsoft, who's well known for it's Window's operating system, reports that Linux is running on the majority of their Azure implementations. When you work with Linux in the cloud, you are most likely going to be using a text-based terminal or a command line, this is true for many of the labs here on Cloud Academy, and that is one of the main reasons I've created this content, to quickly get some practice at the command line and to help you meet the prerequisites for labs that require Linux command line experience. Beyond the labs investing some time in learning command line skills can help you more efficiently perform tasks and automate repetitive jobs. At first you might feel a bit homesick without a graphical interface, but it doesn't take long to see how you can accomplish many tasks faster at the command line without all the pointing, clicking, and dragging, who knows, learning more about Linux and the command line could even open up some opportunities for you. Linux and command line proficiency are a staple of many Cloud and DevOps engineer positions. The program that runs and lets you enter commands on a command line is called a shell.
There are many different shells in the next. Some examples are the C shell, or csh, Korn shell, or ksh, and the bourne again shell, or bash, and yes, different sells tend to end with sh. Bash is the most common and is also the default shell used in mac OS X, that's why we'll be using Bash. A lot of what we'll learn is directly applicable to other shells, but each shell has it's own intricacies. Alright, I mentioned that there is a lab to go with this course, I have it open now, I'd encourage you to start the lab now to. The complete instructions are in the lab to get an Azure cloud shell open, from here on out we will entirely be at the command line, the lab step instructions write out the commands I'll be entering. You can follow along in your lab shell as I'm entering the commands or you can watch each lesson and then repeat the commands for yourself by completing the lab step, the lab steps give some additional information and include optional challenges for you to try out. So first let's take a step back and analyze the different parts of the command line. The green line itself here is called the shell prompt and the shell prompt is made up of several parts, the first is the user name, in my case it's student 555-91, and then after the at symbol is the host name, which is Azure. Azure is the host of the shell. After the colon there is the tilde or the squiggly line, which represents the current directory, and the tilde is a short form for the users home directory.
The home directory in Linux is the same as with other operating systems, it's your own private space for storing personal documents, and the last part of the shell prompt is the dollar sign, which indicates where you start to enter your own commands, and the blinking white rectangle is our cursor, any letters we type will show up where the cursor is. So to work at the command line we have to type in the name of certain commands. As an example I'll type clear, clear is the name of a command in Linux that will clear the terminal or the shell, all the output from previous commands will be removed and to enter the command you just press Enter on your keyboard. And there we see that all of the output has been removed and we have only the shell prompt at the top.
There are a few things to know about Linux commands, they are case-sensitive, so if I were to enter CLEAR in all capital letters, I'll get an error because Linux is case-sensitive and you have to enter all lower case letters for the command to work. Another example of a command is echo, which will simply output whatever you pass into it, and the way that you pass things to it is by entering arguments, which are additional words after the command. So if I enter hello, echo will write hello to the screen. Commands can have multiple arguments, so instead of writing just hello, I can say hello world and echo will output both. Arguments are separated by spaces. Hello is the first argument, world is the second. Commands can also use options that modify the affect of the command. As an example, if I add dash n to the echo command, it will modify the way that echo works by not placing a new line at the end of the output. So now we have the prompt compressed onto the same line as the output from echo. Just like arguments commands can have multiple options, options are easy to identify, because they begin with a hyphen.
We'll see a lot more commands as we go on, the command line follows a pattern of reading what you type, evaluating the command, and then printing output. This read, evaluate, print loop is sometimes called the REPL. I'll clear the terminal as we move on to the next topic. I can't anticipate all the questions that you might have, but I can teach you different ways to get help, to answer your own questions about the command line. First I'll enter the man command, and the output reads, "what manual page do you want?". Man is short for manual, many commands include manuals that you can access with the man command. Whenever you're unsure about how a command works, it's a good place to start by reading the man output for the command, which is often referred to as the man page for the command. As an example of how to get the man page for the clear command, enter man clear. This is a typical man page, it starts with different sections that show the name, synopsis, which is how to use the command, and then a more elaborate description of what the command does. Man pages often include a See Also section, which gives the names of different commands that are related to the current command. If you were unsure of what the clear command does you could come to the man page and see that it clears the terminal screen.
Notice how you no longer have the shell prompt, this means that you can't enter new commands while you're in a man page. You can read the useful message at the bottom that tells you to pres q to quit the man page. And now your shell prompt is returned. Let's bring up the man page for man itself by entering man man. In the case of clear there were no options for the clear command, but man has several options. The synopsis section shows different ways to call man using different options. Recall that you can identify which are options by the hyphens, and if the command takes any arguments they will be listed near the end of the command and won't start with a hyphen. Anything that's optional is enclosed in square brackets, and any options or arguments that aren't enclosed in square brackets are required for the command. Because the man command is more complex than the clear command the man page spans over multiple pages of your terminal screen.
There are some keyboard shortcuts to help you navigate around. For example, you can press F to scroll ahead one page, and then press B to scroll backward, besides scrolling through the man pages one page at a time you can also perform searches to match keywords inside of the man page. If you press slash you'll see that the bottom line changes, you can enter text after the slash to search through the man page for that text. Say we wanted to search for the K option, or hyphen K, you can type hyphen K and then press enter and any matches will be highlighted with a white background. Here we can see the two man commands with the dash K options, one lower case and one upper case, but what we really want is to understand what the dash K means.
To advance to the next search result, press N on your keyboard, and they'll keep pressing until I get into the description section. And from the dash K description we can see that we can search through all the man pages for given key words. This is a useful way to discover commands, we'll see more of this later on. If you ever forget any keyboard shortcuts, for navigating around a man page, you can always take the advice at the bottom of the screen, that says press H for help. The help page collects all of the shortcuts and provides a short explanation of each. Now I'll press Q to close the man page help and Q again to close the man page itself, to get back to the shell prompt. I'll clear the terminal, let's try to discover commands that relate to shells. There are quite a few matches, recall that shells in Linux tend to end with sh, so some examples are bash itself on the third line of the results, as well as dash further down and sh even further down. One thing to note with arguments on the command line is that spaces separate arguments, but if you want to include a single argument with spaces inside of it you need to use double quotes. So as an example, say that we want to find commands related to a content tracker, if we were to enter man dash K content tracker, without any double quotes, man would search for hits that match content and independently man pages that match tracker, it wouldn't look for man pages that have contents space tracker.
So to make man treat content tracker as a single argument we have to wrap it in double quotes, there we can see that there is only one result, but it does include content space tracker in the description. So we've seen that the man pages are a great place to look for getting help with Linux commands, we also saw that when we didn't know what the man command was doing we could simply enter the command and it returned a useful help message.
This is another common pattern, commands often output useful messages telling you how to use the command if you don't provide any options or arguments. Another common pattern is the use of a help option, so if I enter man dash dash help then the output displays a help page which describes all of the options that man supports. In the output we can see that many options support both a short form, which is a single character, and a long form. For example, hyphen capital C is equivalent to hyphen hyphen config file. If a command doesn't support the long form hyphen hyphen help of the option, you can try either of these short forms hyphen H or hyphen question mark. As with other operating systems files play an important part in Linux. What you really need to know to be effective at the command line in Linux is which command should you use for working with files. Anytime you wanna create an empty file you can use the touch command. So this command touch with the file dot txt argument creates an empty file named file.txt, if you want to list all the files in the current directory you can use ls and the output includes the file.txt file that we just created.
If you wanna include the size of the files in the output you can use the long format or dash l option, the lab explains what each of the columns represents, but for now you can see that the zero in the fifth column for the file.txt row tells us that the size of the file is zero bytes, meaning that it is an empty file. One way to write text to a file is to use redirection, which takes the output of one command and writes it to the file. As an example, if I use the echo command to write hello, usually that would write the output hello to the screen, if I wanna redirect that to the file I can include this arrow or the greater than symbol, which indicates that I want to redirect to file.txt, then instead of writing the output to the screen the output is written to the file. So when I press Enter there's no output written to the screen, but I can use ls with the long form option to verify that the size is now six. To actually see the contents of the file you can use the cat command, here we can see that the contents of file.txt is hello, which is what we wrote to the file using the echo command and the redirection operator. Besides using redirection Linux does have commands that are text based editors, so for the sake of this course I'll stick with redirection for writing to files. The lab goes into more details about how to use Vem as an editor. The redirection operator that we've seen will overwrite any contents of the file, if there were previously contents in the file. If we only want to upend new contents to the end of the file there's another redirection operator for that.
If I want to upend the word Linux to the end of the file I can use two greater than symbols or the upend redirection operator. Now I can use cat to confirm that Linux has been upended to the end of the file. Another common operation when working with files is how to copy a file. In Linux you can use the cp command with the first argument as the source file that you want to copy, and the second argument is the name of the new file. So with this command I want to copy file.txt to a new file named copy.txt and now I can use ls to list the files and see that there is a copy.txt file. And even use the long format to see that they have the same file size of 12 bytes, and now if you want to delete a file or remove it you can use the rm command, entering rm file.txt will remove the file named file.txt. The last command that I'll cover related to working with files is move or mv. Mv is similar to copy in that the first argument is the name of the source file or the file that you want to move, and the second argument is the new name of the file. So if I wanna move the copy.txt file into a new file named file.txt, I give file.txt as the second argument and now the copy.txt file has been moved or renamed to file.txt.
The next topic that we'll discuss are directories and I've already said before that directories play the same role as folders in other operating systems, but just like working with files at the command line we need to know which commands to use to work with directories. A good one to start with is pwd, our print working directory. As the name suggests, this prints the working directory where the working directory is the current directory that your shell is located in. In this case you're in slash home slash student 55591, which is the home directory of the user I'm using. And the short form of the home directory is the tilde or the squiggly line character. Now we can see that the home directory and the tilde are the same. You can use the mkdir command to create a directory, let's say we wanna create a directory called documents, mkdir stands for make directory, now if I list the output, includes the documents directory. So now documents is a directory inside of my home directory, if I wanna change my working directory into the documents directory, I can use the change directory command or cd, and now you can see that the shell prompt has changed to indicate that we're in the documents directory.
To change back to the home directory or to go one level up in the file system hierarchy, you can change into the dot dot directory, where dot dot represents the parent of the current directory. No matter what your current directory is you can always change into dot dot to go one level up in the file system hierarchy. You can use ls with the a option to show all files, including hidden files that begin with dot. In the output you can see the two dots no matter what directory you're in. If you want to reference a file that's not in your current directory you can include the directory as part of the path to the file. Say we wanna copy file.txt into the documents directory, but using a name copy.txt, and the second argument includes the path to the new file, which begins with the directory name documents followed by a slash and then the name of the file copy.txt. The second argument here, document/copy.txt is an example of a relative path, meaning that you specify the path relative to your current directory. Besides relative paths there's absolute paths. Absolute paths always begin with a slash, that slash represents the top level or the root of the file system.
As an example, if I wanted to do the same command but using an absolute path, I could enter cp file.txt and then slash home student documents copy.txt. So this is an absolute path, because it begins with a slash. You always have the choice between absolute or relative paths. Relative paths are often more concise, but if you always want to refer to the same file, regardless of your current directory, absolute paths should be used. To delete a directory you can use the same remove command that you used with files, except with the dash d or directory option. So if you wanted to delete the document's directory you could enter this command, but this will fail, because the directory is not empty. This acts as a kind of safeguard, if you're sure you do want to delete the directory and the files inside of it, you can use another option of the rm command, which will recursively delete all of the files and then the directory in a single step, that's the r option. So now the documents directory and the file inside of it have been deleted in a single swoop.
The last topic I wanna cover is using variables at the command line. Variables give you a convenient way to reuse values. For example if you wanted to write two commands that both use the same long text string as an argument, rather than writing the string twice you could store the value in a variable. To define a variable on the command line you give the variable name, say my path, followed by an equal sign and then the value you want to store in the variable. Let's say I wanna store the path slash home. To use the variable in a command you need to proceed it with a dollar sign, so to output the value of a variable you can write dollar followed by the variable name. When you enter this command the Bash shell will first search the command for any dollar signs to find variables, Bash will replace the variable name with the value stored in the variable, and then the command is executed.
One of the benefits of using variables is that you can change the effect of commands by only changing the value stored in variables, there's no need to modify the commands. As an example, if I change the value stored in my path to slash or the root directory and I reissue the same command, the effect of the command has changed, although the command is the same, echo my path, the variables can be used with any command. For example ls my path, will list the contents of the directory stored in my path, which is the root directory.
The root directory has several directories inside of it, the one that's familiar to us is home. Besides storing values that you directly type into a variable, you can also assign the output of commands to a variable. There's a special syntax for that, as an example, if I wanna store the current date in a variable named current_date I can use the date command, but to tell the shell that I want to store the output into a variable I have to enclose the command in a dollar parenthesis date, which is the command, and then close the parenthesis, that will store any output written by date, into the current date variable. Now if I print out what's stored in current date it will show the current date. That takes us to the end of this course. To wrap up I'd like to review some of the key takeaways from this course.
When you were working at the command line, you were interacting with a program called a shell, Bash is the most common shell, and that's what we used. You interact with the command line by following its read-evaluate-print loop or REPL. Linux commands have several way to get help at the command line, including man pages and help options. The concept of files is the same in Linux as other operating systems. There are a few common commands for moving, copying, and removing files at the command line. Files are organized using directories, which are conceptually the same as folders.
You saw how to make, change, and remove directories. You also learned that the root directory is represented by a forward slash and absolute paths begin with it. Lastly, you saw how to assign and use variables to reuse values with minimal changes required. Variables are often used to adapt long series of commands in files called scripts. What we've seen in this course has given you a solid foundation for continuing to learn about the Linux command line. You shouldn't hesitate to try labs on Cloud Academy that require prior command line experience, or to use the command line more in your own work. If you are following along in the lab there is an additional lab step that teaches you another way to work with the opiod of commands that I'd highly recommend trying out. That's all for this course, until next time, I'm Logan Rakai with Cloud Academy.
About the Author
Logan has been involved in software development and research since 2007 and has been in the cloud since 2012. He is an AWS Certified DevOps Engineer - Professional, AWS Certified Solutions Architect - Professional, MCSE: Cloud Platform and Infrastructure, Google Cloud Certified Associate Cloud Engineer, Certified Kubernetes Administrator (CKA), Certified Kubernetes Application Developer (CKAD), Linux Foundation Certified System Administrator (LFCS), and Certified OpenStack Administrator (COA). He earned his Ph.D. studying design automation and enjoys all things tech.