Section 2: The Basics - Green-Biome-Institute/AWS GitHub Wiki

Go back to to Section 1: Introduction

Go back to tutorial overview

With the introduction done, it’s time for us to start learning how to do these things ourselves. We will now explore the syntax is of basic command prompts, different filetypes, navigation around your computer, vocabulary related to the command line (understand what certain words mean (what is a "directory”?)), and how to get more information/understand it if we have a question about a command prompt. (ls, cd, select all “*”, ...)

Learning Points for Section 2 - The Basics

From this section you should take away that:

  • The command line prompt consists of
    • Username ubuntu
    • Hostname ip-xxx-xx-xx-xxx
    • information regarding the current computer operating environment (base)
    • the location on the computer where the command line interface currently resides ~
    • the type of user $
    • And an open field to the right of it for writing and entering text.
  • The CLI has access to information about the computer like the user who is accessing it (whoami) and the time and date (date and cal) as well as access to resources and information online.
  • We can clear the CLI to clean up our workspace when desired used clear.
  • We can find the current working directory at any time using pwd.
  • We can list the files and directories within the directory the CLI currently resides using ls
  • We can change directories by going into subdirectories using cd [directory-name] or by going back to the parent directory using cd .. or back to the home directory using cd or cd ~

The syntax of the Command Line Interface

Before continuing on, we are going to log out of the EC2 instance and re-log in, in order to see some information about the instance itself that we skipped in the first example. Use the command:

$ exit

This will log you out of the ec2 instance and move you back to your own physical computer. From here, we can log back in using the command given in example 1:

ssh -i ~/Desktop/GBI-Bioinformatics-Class.pem ubuntu@[your-IP-Address]

Now, before typing anything, let’s just look at the information given to us. When logging onto an AWS instance, we get a whole bunch of information:

Flints-MacBook-Pro:flints-dir flintmitchell$ ssh -i ~/Desktop/GBI/AWS/AWS_keypairs/r5large-gbi-keypair.pem [email protected]
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.11.0-1017-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Sep 27 22:55:48 UTC 2021

  System load:  0.72               Processes:             160
  Usage of /:   73.8% of 48.41GB   Users logged in:       0
  Memory usage: 4%                 IPv4 address for ens5: 172.31.52.167
  Swap usage:   0%

 * Ubuntu Pro delivers the most comprehensive open source security and
   compliance features.

   https://ubuntu.com/aws/pro

0 updates can be applied immediately.


Last login: Mon Sep 27 22:54:18 2021 from 76.209.247.154
(base) ubuntu@ip-172-31-52-167:~$ 

At the top of this block of text, we see the command I entered to log into the AWS EC2 instance. This command was entered on my personal computer. After sending the command to log onto the AWS EC2 instance, however, you can see at the bottom, the command line is now saying ubuntu@ip-172-31-52-167.

What this means is that this CLI window is now sending commands to the EC2 instance in the cloud, and no longer to your computer. Therefore you will not be able to access files on your own computer through this window until you log out of the EC2 instance. All the files you see in this window are on a server hosted by AWS. We will get a better introduction of AWS in another module, for now all that is important is that you understand that once you have logged into an AWS EC2 instance, you are sending commands to a different computer. This other computer is in the cloud and has components like hard drive storage, memory, and computer processors. For example, you can see in this particular case that there is a hard drive on this instance with about 50 Gb of storage. Your EC2 instance for this tutorial will be slightly different.

The command line prompt is the bottom line of text, with the $. This text has a couple components:

  • (base) refers to what is called an environment. It has to do with the types of tools that are available to the CLI. For example, say you have a garage connected to your house with a bunch of tools. When you first walk into your house, you’re in an “environment” without tools, but when you walk into the garage, you’re now in a new “environment” with direct access to your tools! We will deal with this in the next section.
  • ubuntu is the username that we are logged into our AWS EC2 instance with (in this example).
  • @ is a separator between the username and the hostname
  • ip-172-31-52-167 is the hostname of our computer in this example
  • : shows us that text to the right of this colon will define where we are on our computer (meaning what directory or folder) we are in.
  • ~ refers to the home directory of our computer. The home directory is where you will go when you first log into your AWS EC2 instance. Being able to navigate back to your home directory is useful for not getting lost or overwhelmed in the CLI.
  • $ tells you that you are logged in as a regular user. Regular users have certain levels of permissions, which we will learn more about in the next section. You will be able to write commands to the right of this sign.

You will see that some of these things change. We will come back to these changes as they happen during this module.

Interacting with the Computer using the CLI

Now that we understand this information, we can start to enter some commands to see what happens. First, let’s just use some commands that show us what kind of information the CLI has access to. Entering the following command:

$ whoami

Will output ubuntu.

A couple things have happened here:

  1. You typed a command that is within the library of commands the shell (the program that the CLI is controlling) has access to and understands.
  2. You pressed enter and sent that command to the shell.
  3. The shell found the program whoami and executed it. This command simply returns the username of the user who is currently logged into the computer. Since the username is ubuntu as we saw above, that’s what is outputted.
  4. Lastly, after finishing this output, the CLI returned to the same command prompt, awaiting more instructions from you!

Next let’s use the following command:

$ date

Which will output the current date and time down to the second. Next use the following command:

$ cal

This will output a small calendar with the current month and current day highlighted. Now let’s have the CLI repeat something back to us. Use the command:

$ echo hello

When you enter this, the CLI says “hello” back to you! This is an example of giving the computer information to use (the word “hello”) and telling the computer to do something with it using the command echo (repeat “hello” back to me). Next, use the following command:

$ wget https://raw.githubusercontent.com/Green-Biome-Institute/AWS/master/hello.txt

And then

$ cat hello.txt

What you have just done is downloaded a file from the GBI Github page on the internet and read it! From these couple commands you can appreciate that through the CLI you have access to a variety of information regarding the computer and the internet, you can give the CLI information (like the text “hello”), and tell the computer to do things! Hopefully you can now see that you are able to do the same things that you can do by clicking with your mouse on the graphical interface of your computer as you can on the CLI.

Another way to read out important information from a file is to look at just the top couple lines or bottom couple lines of a file. For these purposes we can use the commands head and tail. Using head -n 10 [FILE] will read the first 10 lines of [FILE]. Similarly, tail -n 10 [FILE] will read the last 10 lines of [FILE]!

Now that we have cluttered up our CLI with all of this stuff, use the command:

$ clear

Don’t worry, you are still logged in. All that has happened is that your CLI has cleared the screen of the previous information and your command prompt is now at the top of the window. This can be nice to do occasionally to clean up your workspace and focus on the next task at hand. However note that by doing this you also lose the text that was on your screen beforehand.

Okay I know who I am... but where am I?

Let’s dive a little further. The commands we used above are pretty self explanatory whoami, date, and cal all do things that they sound like they will do. This is true for lots of commands, however most of them are abbreviated to be very short. At the beginning of computer engineering, they were concerned with using the least amount of information possible, so commands were made to be short. Try the following command:

$ pwd

The output will be /home/ubuntu. This is your current directory (which is also your home directory). The command pwd is short for “print working directory”. Directories are like folders. They are areas on your computer with a designated name used for organizing files and other directories. Your “working directory” is the directory that your CLI is accessing (the area you are currently “in” on your computer!) Therefore, pwd prints the current directory of your CLI. So if you ever find yourself “lost” within the CLI, use the pwd command! It will print out where you are on your computer.

A subdirectory is a directory within another directory. Just like a folder within a folder. Linux creates folders for each user within a directory called home. Since your username is ubuntu, your personal home directory on this EC2 instance is the subdirectory ubuntu within the parent directory home. The forward slash / signifies this relationship between directories and files. If you see something like /home/ubuntu, it is because ubuntu is within home, signified by the / between them.

Listing contents

Now how do we find out what is in this home directory? Use the command ls, which is short for “list”:

$ ls

Changing directories

You can see that there are a bunch of files and folders already on this computer within your home directory. Now how do we access them if we can’t click on them with our computer mouse or touchpad? We use the command cd, which is short for “change directory”. Try the following command:

$ cd ex1-dir/

Your CLI has now changed to the subdirectory ex1-dir. Let’s list the contents of this directory:

$ ls

You can see the two files that we used at the beginning of this training module, count.sh and multi-variable-assembly.sh. Let’s try using them again. Use the same command as we did before for the count.sh file:

$ ./ex1-dir/count.sh

But wait, why isn’t it working now? Let’s look at a change that happened to our command prompt. Instead of reading

(base) ubuntu@ip-172-31-52-167:~$

it is now showing

(base) ubuntu@ip-172-31-52-167:~/ex1-dir$

This is because by using cd, we changed our current directory to “ex1-dir”. To confirm this, let’s use the “pwd” again:

$ pwd

The output is no longer /home/ubuntu. It is now /home/ubuntu/ex1-dir. Let’s look at the command we used to execute the count.sh file beforehand: “$ ./ex1-dir/count.sh” Within this command, we are executing the file count.sh within the directory ex1-dir. But we are already in the ex1-dir after changing our directory! That means we can immediately access the count.sh from within the directory we are currently in. In order to do that, we’ll take ex1-dir/ out of the command and try again:

$ ./count.sh

Now it works again! Let’s navigate back to the home directory and try it again. To do this we will use the change directory command cd again. This time after cd, we will press the spacebar to enter a space and then type two periods ..:

$ cd ..

These two dots represent the parent directory. By using cd .. we are changing the directory to the parent directory, which in this case is our home directory. If you want to go straight back to the home directory from any directory on the CLI, you can use both of the following:

The shortcut cd with nothing after it. cd ~ because ~ represents your home directory.

Review Questions:

When you log onto an EC2 instance within your CLI window, do your commands get sent to your own computer or a virtual computer in the AWS cloud?

  • They get sent to a computer in the cloud! Your personal computer will not receive any of the commands you send it!

What is the command line prompt?

  • It is the area for you to enter text and commands that will be sent to the computer. The prompt contains text that holds information regarding the user who is logged into the computer, the name of the computer (the host), the current working directory (where you are on the computer, and the type of user you are.

Can you access the internet via the command line?

  • Yes!

What is your current working directory?

  • It is the location on your computer that your CLI session currently resides and decides what files and directories you have immediate access to.

How do you find out what your current working directory?

  • You use the command pwd.

How do you list the contents of your directory?

  • You use the command ls.

How do you change directories to a subdirectory named "example-dir"?

  • You use the command cd with the folder named after it: cd example-dir.

How do you move back to your home directory?

  • You use the command cd or cd ~

Move on to Section 3: Behind the Scenes

Go back to tutorial overview