Link Search Menu Expand Document

Terminal

Lesson 2

Your terminal is used to control your computer and run commands, scripts and other tools directly. You will often need to use your terminal to interact with AWS and your code editor. You run commands in the terminal that tell your computer to do different things. Just like you can right-click on the Desktop and create a folder, or open up your word processor and create a new document (file), you can do the same thing in the terminal. The difference is that your terminal can do much more powerful things as well, but we’ll get to that later.

Important Notes:

  • In the terminal, folders are called directories. Every time you see directory (or directories) just think of it like a folder.
  • In the terminal, opening directories (folders) is called changing directories. When you see the phrase “change into” just think of it as opening a new folder.

Table of contents

  1. Terminal
  2. What you’ll learn
  3. Get Started in the Terminal!
    1. Open your terminal
    2. Your first command - pwd
    3. List files - ls
    4. Move around the terminal - cd
    5. Make a new directory - mkdir
    6. Create a file - touch
    7. Oops! Delete a file - rm
    8. Double OOPS! Delete a directory - rm -rf
  4. Review and next steps
  5. Next Steps
    1. Redo this lesson (optional)
    2. Move On

What you’ll learn

How to open a terminal and run the following commands in Linux and Mac OS:

  • pwd
  • cd
  • ls
  • mkdir
  • touch
  • rm
  • rm -rf

Get Started in the Terminal!

Follow the steps below to get comfortable in the terminal, using commands to create and delete directories and files, and move around between directories.

Open your terminal

Open your terminal by going to your application search feature (linux) or launchpad (mac). Type in terminal and choose the application. A window will open with a black background that looks like this:

terminal

  • This is your terminal.

Your first command - pwd

In your terminal, paste or type the below command, then hit enter.

pwd
  • pwd prints the name of your current directory. Think of this like having this folder open. Because you just started your terminal, this will be your home directory.

pwd

  • You can use the command pwd to know where you are at any time. We’ll practice again as we change directories.

List files - ls

In your terminal, paste or type the below command, then hit enter.

ls
  • ls will list all the files and directories in the directory you currently have “open”.

ls

  • You can use ls to show you the files and folders you can interact with at any time. Files will appear in one color (mine are red) and directories (folders) will appear in another (mine are blue).
  • Make sure Desktop is in your list. If it isn’t, you’ll need to pick a different directory to use in the next step below. (Any directory you see in the list will work).

Move around the terminal - cd

In your terminal, paste or type the below command, then hit enter.

cd Desktop
  • If Desktop wasn’t listed, instead you would type cd whateverYourDirectoryYouWant.
  • cd stands for change directory, which just means that you want to change into (or “open”) a different directory.

cd-1

  • Now use ls to see all the available directories. You can use cd to change into any of them. (Don’t do this yet though!)
  • Now use pwd to print your working directory. You should see that your path ends in Desktop now!

Make a new directory - mkdir

Let’s say on your Desktop you notice (from the terminal) you have a lot of files and not a lot of directories. To organize, you want to create a directory called Random.

In your terminal, paste or type the below command, then hit enter.

mkdir Random

mkdir

  • mkdir stands for make directory, and will do just that–create a new directory in the directory currently have open.
  • Now use ls to list the files and directories. See if you can find your Random directory.
  • Next, use cd Random to change into the Random directory.
  • Lastly, use ls again to list all files and directories inside of Random. You should see that nothing shows up! This is because Random is a brand-new, and empty, directory!

Create a file - touch

Let’s create a file in your Random directory.

In your terminal, paste or type the below command, then hit enter.

touch random.md
  • After you create your file, type the command ls to list the files and folders in Random. Make sure random.md appears!

touch

  • The touch command creates a file.
  • The file extension you give tells the terminal what type of file to create. We created a markdown file (.md), which is a type of file often used to create Readmes (instructions for code and software that are often created with most projects you’ll have). You’ll learn more about markdown later.
  • You can also create other files, like .doc files. Give it a try yourself! Use the command touch to create a new file, helloworld.doc.
  • Make sure you type ls to see if helloword.doc was created correctly.

Oops! Delete a file - rm

Oops! You created a file you aren’t going to use! Now we’ll learn the rm command, which stands for remove. You use rm to remove (delete) files.

  • Type the command pwd to make sure you are still in your Random directory.

In your terminal, paste or type the below command, then hit enter.

rm random.md
  • This will delete the random.md file you created earlier.
  • Lastly, type ls to make sure random.md was deleted and is no longer listed.

rm

  • BONUS: See if you can delete the helloworld.doc file you created also! (Don’t worry if you can’t–we’re about to delete the whole directory anyway).

Double OOPS! Delete a directory - rm -rf

The last command we’ll learn for now is rm -rf, which will remove (delete) directories.

In your terminal, paste or type the below command, then hit enter.

rm -rf Random
  • Notice that Random was not deleted! This is because we were inside the Random directory still, and you can’t delete a directory you are currently in. It’s like when you have a folder open in the background and you try to delete it anyway. Your computer gives you an error and says you can’t because you have it open somewhere else.
  • In order to delete Random, we need to move up one directory. This means, we need to move to our Desktop directory.
  • Type the command cd .. , then hit enter.

rm-rf

  • Now that you are in your Desktop directory (you can check this with pwd), and Random is in your Desktop directory, you can delete the Random directory.
  • Type the command rm -rf Random again, and this time it will work!
  • Type ls to make sure Random is gone.

Review and next steps

You’ve now learned the most important basic terminal commands! These commands are enough to get you started with using the CLI (command line interface, ie terminal) with AWS and terraform. Here’s a quick summary:

  • pwd - print working directory - this will tell you what directory (folder) you are currently “in”/the directory you have “open”
  • ls - list files and directories - this will tell you all the files and directors inside the directory you are currently in
  • cd DirectoryName - change directory - cd will let you change into any directory you see with the ls command. You just type cd and then the name of the directory you want to “open”
  • cd .. - this command will change directories into the directory “above”/”before” the one you are currently in. Let’s say you you are in Home/Desktop/Random. Typing cd .. once will get you to the Desktop directory. Typing cd .. again will get you to the Home directory.
  • mkdir - make directory - this will create a new directory (folder) inside your current “open” directory. You need to type a name for your directory after the mkdir command (ie mkdir HelloWorld).
  • touch - create file - this will create a new file inside the directory you have “open”. You need to type a name for your file as well as an extension (ie .doc, .md, .json, etc) after the touch command (ie touch BestFile.doc)
  • rm - remove - this command will delete a file. You must be inside the same directory as the file you want to delete.
  • rm -rf - remove recursive force - this command will delete a directory. You must be inside the parent directory of the directory you want to delete (ie, if you want to delete Random, you need to be in the directory “above” it, Desktop).

Next Steps

That’s all for the terminal! You have learned all the basic commands you will need to be successful in the AWS cloud. As you work more and more in the terminal, you’ll learn more commands and become a real pro in no time!

Redo this lesson (optional)

If you feel like you need more practice, feel free to complete this Lesson again as many times as you need. It is intentionally built so that all you need to do is go back to the beginning and you’re starting from scratch!

Alternatively, you can experiment with creating, deleting, and changing directories on your own.

Move On

When you feel ready, head to the next Getting Started section: AWS Account Setup