Terminal

Back

Learning Goals

  • Notate the file path of any file when provided a diagram
  • Build, modify, and navigate a directory structure from the command line

Vocabulary

  • child
  • command
  • directory
  • file
  • file path
  • “run a command”
  • parent
  • Terminal

Files, Directories, and Paths

On your computer, you probably have many files and directories. Files are things like text documents, images, videos, PDFs, etc. Directories (or folders) are the structures we use to organize these files.

In the diagram below, we would say that there’s a directory called essays that contains three files: life_lessons.docx, book_report.docx, and literary_analysis.docx:


Example #1

files and directories 1

A file path is a way to notate where a file "lives" on your computer. This is the structure:

directory_name/file_name.extension

The file path for the first file in the diagram would be:

essays/life_lessons.docx

What is the path for book_report.docx?

What is the path for literary_analysis.docx?


Things to Note

  1. A file cannot be inside of another file. This means that every part of the path before the actual file is a directory.
  2. For now, we will follow two naming conventions:
    • Use lower case letters when naming directories and files.
    • Use underscores (_) or hyphens (-) instead of spaces when naming directories and files. However, keep in mind that different languages and frameworks have different conventions. Rather than arguing over which approach is "correct" (you'll find a lot of this on the internet), it is more important to pick an approach and be consistent. For today's lesson, we'll use the underscore (_) convention.
  3. Folders do not have extensions (like .docx or .md or .csv, etc.). File names do have extensions.
  4. File extensions matter. A .md file will behave differently than a .docx file, which will also behave differently than a .rb file because the extensions help the operating system figure out which application can open the file and how to read it.


Example #2

files and directories 2

The path for the grading.txt file is

to_do/work/grading.txt

What is the path for cleaning.txt?

What is the path for recurring.txt?

What is the path for data_entry.csv?

Things to Note

We commonly refer to directories with an analogy of parent and child. In the previous example, to_do is the parent directory of the personal and work directories. projects_to_delegate is a child directory of the work directory.


Terminal

The Terminal is a program we use to give commands, or directions, to the computer. We use the Terminal because it is a faster and more precise way to navigate our file systems, and certain tools can only be downloaded and accessed via the Terminal. In this lesson, you will learn 9 commonly used commands, but there are many more you’ll learn throughout your time at Turing and in your career.

Your Terminal will look something like this:

Screenshot of Terminal window

To open your terminal, we will use a Mac built-in keyboard shortcut. Press cmd + spacebar and a bar in the middle of your screen should open. This is your Spotlight and it can help you quickly find applications on your laptop. In the spotlight, start typing Term for terminal. It should automatically find the Terminal application. Hit enter and it should open!

Independent Research

Be prepared: You are about to be asked to go reseach a technical topic you have received no instruction on. This might feel scary! You might be wondering if it's a mistake! It's not - this is a big part of Turing's teaching and learning, and something we are sending you all the way into on your very first day. It might feel challenging or uncomfortable - sit with that feeling, embrace it, and push yourself to engage!

Take 3 minutes (max) to research each of the following Terminal commands: pwd, touch, and cd ... Take note of what you learn and any questions that arise from this research.


Reflection

  • How did it feel to research a technical topic?
  • Did you struggle? If so, was the struggle productive or unproductive?
  • Did you stick to the time limits?


Terminal Commands

Each command has a different utility. Some find it helpful to categorize the type of utility as “Action”, “Safe/Informative”, or “Destructive”. Read and follow the activities provided to learn about each of the 9 commands.

Action Commands

  • mkdir
  • touch
  • clear
  • cd
  • cd ..

Safe/Informative Commands

  • pwd
  • ls

Destructive Commands

  • rm
  • rm -rf


1. Where am I? (pwd)

When you open the Terminal, you will be in your home directory. Just like you can do different things at home vs. on vacation vs. at work, the location you are in within your file structure determines what actions you can take.

To figure out where you are in your computer’s directory structure, run pwd. This command stands for “print working directory”.

timo@Tims-MacBook-Pro:~$ pwd
/Users/timo

You’ll see the path from the root of your computer to your current directory.

This is classified as a “Safe” command because there is nothing you can mess up by running this. Ever.

2. Make a Directory (mkdir)

To make a folder using the visual interface of Finder, this is what you might do:

making a folder using finder

In the Terminal, we can make new directories with the mkdir command. Unlike pwd where we didn’t need to run anything else, we’ll need to add a name for the directory. Directory naming should follow a convention - we’ll use all lowercase letters with no spaces; if you want to create a multi-word directory, use the underscore (_) instead of a space.

The following commands will create two directories called work_spreadsheets and latest_projects:

timo@Tims-MacBook-Pro:~$ mkdir work_spreadsheets
timo@Tims-MacBook-Pro:~$ mkdir latest_projects

You won’t get any confirmation that your directory was created; you’ll just see a new command prompt ready for your next command.



3. Listing Contents of a Directory (ls)

With a visual interface (as shown in the gif above), you can easily see the contents of a directory. In the Terminal, it’s a little different.

To see what is inside of a directory, we use the ls command, which is short for list. As an example, let’s assume that we have the directories and files from this diagram on a computer:

files and directories 2

If I was in the essays directory and I ran ls, this is what I'd see:

timo@Tims-MacBook-Pro:~/essays$ ls
  book_projects    life_lessons.docx    notes.docx

You will only see the directories and files that are directly inside of the current working directory. You will not see any directories or files that are nested down the path. This is why we do not see the contents of book_projects listed.

Now assume we're in the book_projects directory. If I run ls, I'll see this:

timo@Tims-MacBook-Pro:~/essays/book_projects$ ls
  literary_analysis.docx    book_report.docx

ls is a great command to run after you’ve made a new directory as it will allow you to confirm you made the directory.

This is classified as a “Safe” command because there is nothing you can mess up by running this. Ever.


4. Go Into a Directory (cd)

You can move into a directory using the cd command, which stands for “change directory”. After cd, type the name of the directory you want to go into.

files and directories 2

For example, if I was in the essays directory and wanted to move into the book_projects directory to see my documents, I would run the following:

timo@Tims-MacBook-Pro:~/essays$ cd book_projects
timo@Tims-MacBook-Pro:~/essays/book_projects$

We see that the second command prompt now lists the path of new directory that we're in.

From there, if I used the ls command, I would be able to see the contents of my folder:

timo@Tims-MacBook-Pro:~/essays/book_projects$ ls
literary_analysis.docx    book_report.docx

Things to Note

You can't pick any random directory from your computer to give to the cd command. It has to be a directory that is directly inside the current working directory (or you need to use the full path to get to that directory, which we won't talk about in this lesson).


5. Get Out of a Directory (cd ..)

To get out of a directory you’re in, we use cd .. (note the space between the d and the first dot). This means “go back up one level.”

If I’m in the book_projects directory and I want to get back to essays, this is what I’d run:

timo@Tims-MacBook-Pro:~/essays/book_projects$ cd ..
timo@Tims-MacBook-Pro:~/essays$

Notice that my path no longer includes book_projects because I’m outside of that folder now.

Note: You never want to cd into a directory above your home directory. This area requires admin permissions, and there is no practical use case for being there.


Try It: cd and cd ..

Using what you've learned, type a response to each prompt in the associated input field. If you aren't sure or want to check yourself, this is a great topic to bring up in your small group Slack channel.

files and directories 2

If I'm in the work directory, what command do I need to run to get to to_do?

If I'm in the projects_to_delegate directory, what commands do I need to run to get to to_do?

(We'll learn how to combine these momentarily)

If I'm in the personal directory, what commands do I need to run to get to projects_to_delegate?

I'm in the projects_to_delegate directory. What commands do I need to run to get to personal?


Things to Note

  • Each level in the path is separated by a /
  • .
  • One can combine commands to navigate multiple levels through your directory structure.
  • For the third example in the previous Try It section, one could navigate to the projects_to_delegate in a single command: cd ../work/projects_to_delegate.
  • For the final example above: cd ../../personal.

If you don't feel comfortable combining commands yet - that is ok! We shared the last two bullet points on how to combine commands for those who are curious about a more efficient way, but it's not an essential part of your workflow in Mod 0.


6. Make a File (touch)

We know how to make directories (or folders) using the mkdir command. In order to make files inside of those directories, we use touch. touch needs to be provided with the name of the file being created. The following commands will create new files called chapter_1.md and chapter_2.md:

timo@Tims-MacBook-Pro:~/latest_projects$ touch chapter_1.md
timo@Tims-MacBook-Pro:~/latest_projects$ touch chapter_2.md

We don’t receive any confirmation that the file was created, but we can use ls to see what’s inside the directory:

timo@Tims-MacBook-Pro:~/latest_projects $ ls
chapter_1.md    chapter_2.md


7. Clear your Terminal (clear)

Sometimes when you’ve entered a lot of Terminal commands, your Terminal can get pretty cluttered. You can always use clear to “clean up” your Terminal workspace! The keyboard shortcut cmd + k does the same thing.

8. Remove a File (rm)

In the past, you’ve probably gotten rid of files by using the Move to trash button or dragging them into the trash, like this:

file to trash

We can remove files from the command line by running the rm command. Your current working directory must contain the file you want to remove.

timo@Tims-MacBook-Pro:~/latest_projects $ ls
chapter_1.md  chapter_2.md
timo@Tims-MacBook-Pro:~/latest_projects $ rm chapter_1.md

Again, we don’t get a confirmation, but if I were to run ls right now, the filename I provided to rm no longer appears because it has been removed.

timo@Tims-MacBook-Pro:~/latest_projects $ ls
chapter_2.md

Things to Note

  • A file removed using the rm command does not go into your trash where you could restore it later.
  • Although it may be possible to recover files deleted with rm, it is a difficult process requiring special tools and time. For now, assume that any file you remove using the rm command is permanently destroyed.


9. Remove a Directory and Its Contents (rm -rf)

We can use rm to remove a file, but we use a different command when we’re removing a directory. Since a directory could potentially contain other files and directories inside of it, we use rm -rf which stands for remove recursively, or go inside this directory and remove everything inside of it as well.

In order to remove a directory, you must be OUTSIDE of that directory. For example, if I’m inside a books directory and I want to remove it, I first need to get out of it using cd .., then use the rm -rf books:

timo@Tims-MacBook-Pro:~/latest_projects/books$ cd ..
timo@Tims-MacBook-Pro:~/latest_projects$ rm -rf books

Now when I run ls, I will no longer see books listed.

Try It: Removing files (rm) and directories (rm -rf)

Using what you've learned, type a response to each prompt in the associated input field. If you aren't sure or want to check yourself, this is a great topic to bring up in your small group Slack channel.

files and directories 2

For this scenario, assume that each question is independent of the rest, and that the starting point is always the diagram to the left.

I'm in to_do. What do I run to remove random.txt?

I'm in to_do. What do I run to remove the personal directory?

I'm in the work directory. What command(s) do I run to remove the personal directory?

I'm in the projects_to_delegate directory. What command(s) do I run to remove the directory I'm currently in?

I'm in projects_to_delegate. What command(s) do I need to run to remove the cleaning.txt file?


Practice

In your Terminal, recreate the file structures shown in the diagrams below. Assume that the extension for any files notated is .md.

Challenge #1




Challenge #2




Check For Understanding

If you haven’t already, create a terminal_lesson.md file in your Mod 0 directory. Answer these questions in that file.

  1. How do you quickly open up your Terminal on your computer?
  2. From your home directory, you have a Turing directory, then a Mod 0 directory. How would you create another directory inside the Mod 0 directory called Projects? (Note: there may be multiple correct answers.)
  3. Working off the above question, how would you add a new file to the Projects directory called first_project.md?
  4. If you were in the Projects directory, what would you type to get back to the Turing directory?
  5. From your Turing directory, how would you view all the directories and files within that Turing directoy?







Back