Lesson Weekend

In the last lesson we learned about the terminal and its command line; including how to access it, and the notation commands are conveyed in. In this lesson we'll explore the most common commands executed through the command line. You'll use many of these daily at Epicodus and out in the industry.


To execute a command we just click within the terminal window and begin typing. Text is inserted where our cursor is located, directly after the prompt. After typing a command we press Enter to execute it.

Let's explore some of these commands now.

Retrieving Current Location with $ pwd

The $ pwd command prints your current location to the screen. pwd stands for print working directory.

Remember, similar to the Mac Finder or Windows Explore programs, the command line allows us to look around our computer. We can navigate through folders and directories and see what's inside. The only difference is that Finder and Explore have graphical user interfaces, and the terminal does not. It's text-based instead.

So by entering pwd into the command line and pressing Enter, we can get our current location. It returns a file path, like this:

epicodus-5:~ Guest$ pwd

In the example above we typed pwd after our command prompt and hit Enter. The terminal responded by printing out our current location for us. As you can see, we're located in the Users directory in a folder called Guest.

When you open the Terminal application on a Mac, you will begin in your home directory by default. For example, if your username is Jesse, your home directory is /Users/Jesse. You can verify this by typing pwd at the command prompt and pressing Enter.

If you're navigating through files and folders in the command line (we'll discuss how to do this in a moment) and are ever unsure of your location, just run the $ pwd command. It's like a "You are here". Give this command a shot in your own command line now.

Listing All Content with $ ls

The $ ls command stands for list. Executing this command prompts the terminal to list out the directories ("directories" is just a technical term for folders) and files in our current location. Running this command looks something like this:

epicodus-5:~ Guest$ ls
Documents       Library         Pictures
Applications        Downloads       Movies
Desktop             Music           Public          

But, of course, the output on your own computer will differ, depending what folders you have saved.

We can also list out the directories inside of one of these directories by including its name after the $ ls command. The command and specific directory name are separated by a space. For example, there is a special subdirectory of your home directory called Desktop. It contains all of the files and folders visible on your Mac desktop.

We could say $ ls Desktop to see all directories located in the Desktop directory:

epicodus-5:~ Guest$ ls Desktop
my-coding-project        coding-practice

Note that the output in your Desktop directory will differ. We'll cover how to create directories with the command line in a moment.

We can see our Desktop directory contains two other directories: A folder called my-coding-project and another folder called coding-practice.

We could also run the command $ ls Documents to see all directories in the Documents directory, like this:

epicodus-5:~ Guest$ ls Documents

Here, we can see this computer has a cover-letters directory in its Documents directory. Once again, your output will differ depending on what is in the directory.

You can also use this command in conjunction with any directory in your current location. That is, you can run $ ls to see all subdirectories in the directory you're located in. Then see into any of the directories displayed by running $ ls directory-name. (But directory-name should be replaced with the name of the folder you'd like to look inside.)

Keep in mind that we can only look one level "deeper" than our current location. If a directory doesn't appear when we run $ ls, that means we cannot look inside it from our current location using the $ ls directory-name command.


We just learned how to see what directories are in our current location, and how to see what subdirectories are inside those directories. But what if we want to see directories in an entirely different area of our computer?

Thankfully, similar to how we can navigate around different folders in Mac's Finder, or Windows' Explorer programs, we can also navigate around various files and folders through the command line. In this next section we'll learn commands to navigate through our computer in the terminal.

Moving Between Directories with $ cd

$ cd stands for change directory. As the name implies, this command allows us to navigate to a different directory than the one we're currently located in. Unlike commands we've learned so far, this command requires another word or symbol included with it. This additional word or symbol tells the terminal which directory we're navigating to.

Let's look at several ways to execute the $ cd command:

$ cd directory-name

We can include the name of a directory we'd like to navigate to after the cd command. Notice they are separated by a space. For instance, we can run the $ ls command to list all the directories in our area:

epicodus-5:~ Guest$ ls
Documents       Library         Pictures
Applications        Downloads       Movies
Desktop             Music           Public          

Then we can navigate into any of these directories by using the $ cd command, along with their name:

epicodus-5:~ Guest$ cd Documents        

After we run $ cd, no text is printed to the screen. This is normal. However, notice that the prompt preceding the command line has changed! It said ~ before, but now it says Documents!

epicodus-5: Documents Guest$

The ~ that was previously displayed has been replaced by the word Documents, because we're currently in the Documents directory. (~ means "home directory"). This means we have successfully navigated from the home (~) directory, into the Documents directory. We are now currently located in Documents!

After navigating to a new spot, we can run the $ pwd command again to see our new location:

epicodus-5:~ Documents Guest$  pwd

We can include different information after the $ cd command to navigate to different areas. Here are a few examples:

$ cd ..

Including two dots, like this .., navigates to the directory one level above the current directory. That is, the directory that houses the directory we're currently located in.

So if we are in the Documents directory, as we saw in the example above, we could run $ pwd to see our current location:

epicodus-5:Documents Guest$ pwd

Then we could run the command $ cd .. to move up one level. If we ran $ pwd again, we would see that our current location has changed:

epicodus-5:Documents Guest$ cd ..
epicodus-5:~ Guest$ pwd

Instead of being located in Users/Guest/Documents, we're now located one level 'up' in the Users/Guest directory.

$ cd ~

Also, no matter where we are located, we can always return to the home directory (/Users/Guest on Epicodus computers) using the $ cd ~ command.

For instance, let's say we navigate back to the Desktop directory with the cd command, like this:

epicodus-5:~ Guest$ cd Desktop      

Once in Desktop, we run $ ls to list all directories in that location, like so:

Epicodus-5:Desktop Guest$ ls
my-coding-project        coding-practice

We can see there's a directory called coding-practice here. Pretend we want to navigate into that. We run the $ cd command again:

Epicodus-5:Desktop Guest$ cd coding-practice

Now we're several directories deep. That is, we've navigated through multiple directories to get to our current location. We can return directly back to the home directory by running $ cd ~ like this:

Epicodus-5:coding-practice Guest$ cd ~
Epicodus-5: ~ Guest$

After running this command, we can see the prompt changed again. Additionally, if we run $ pwd, we can see our location has changed. We're back in the home directory:

epicodus-5:~ Guest$ pwd

Isn't this cool?

Creating Files and Folders

In addition to navigating around directories, we can also create files and folders through the command line! You'll use these commands often (almost daily) to create files and folders for your code at Epicodus.

Making New Directories with $ mkdir

The $ mkdir command stands for make directory. We can use the combination of commands discussed above to navigate to the location we'd like to create a new folder. Then we can create a new directory by saying $ mkdir name-of-directory. (Replacing name-of-directory with the name you'd like to give your s directory).

For instance, we could navigate to our Desktop directory:

epicodus-5:~ Guest$ cd Desktop
epicodus-5:Desktop Guest$

And make a new folder on our Desktop called intro-to-programming:

epicodus-5:Desktop Guest$ mkdir intro-to-programming
epicodus-5:Desktop Guest$

If we run $ ls to list all directories in our current location, we can see the directory we've just created:

epicodus-5:Desktop Guest$ ls
intro-to-programming         my-coding-project        coding-practice

When we work at Epicodus, we'll want each new project we create to reside in its own dedicated folder. So we'll use the $ mkdir command to make new directories every time we create a new project.

Note that multiple-word directory names should use hyphens - or underscores _ to separate the words in the directory. If you use spaces, the command will make a directory for each of the multiple words instead of one directory with a multiple-word name.

Creating New Files with $ touch new-file-name

The $ touch command is very similar to $ mkdir, but instead of creating a new directory, this command creates a new file. For example, we could navigate into the new intro-to-programming directory we just created with the $ cd command:

epicodus-5:Desktop Guest$ cd intro-to-programming
epicodus-5:intro-to-programming Guest$

Then we can create a new file called my-first-project.html inside of this directory using $ touch, like this:

epicodus-5:intro-to-programming Guest$ touch my-first-project.html

Then we can run $ ls to see that our new file has been created:

epicodus-5:intro-to-programming Guest$ ls

Moving Files

In addition to creating directories and files directly through the command line, we can also move them!

Moving and Renaming Files with $ mv file-location new-file-location

The $ mv command stands for move. It can both move and rename files. For instance, we can see which files are in our current location with $ ls, like this:

epicodus-5:intro-to-programming Guest$ ls

As we can see, this directory contains a file called my-first-project.html. If we wanted to rename this file, we could do this:

epicodus-5:intro-to-programming Guest$ mv my-first-project.html my-first-webpage.html

As you can see, the $ mv command requires two pieces of information: First, the name of the file we'd like to rename or move. Secondly, the new name or location we'd like to provide the file. So, in the example above, we are renaming a file called my-first-project.html to my-first-webpage.html.

We can confirm the file has been successfully renamed by using $ ls to list all files in the current location:

epicodus-5:intro-to-programming Guest$ ls

However, let's assume that the intro-to-programming directory will contain all projects we create for our course. As you'll learn in class, each project should reside in its own directory. But my-first-webpage.html is sitting directly in the intro-to-programming folder.

We can create a new directory in the current location with the $ mkdir command, like this:

epicodus-5:intro-to-programming Guest$ mkdir first-webpage

We can run $ ls to see that the new directory has been added:

epicodus-5:intro-to-programming Guest$ ls
first-webpage        my-first-webpage.html

We can see that first-webpage is a directory, and my-first-webpage.html is a file, because first-webpage does not have a file extension, and my-first-webpage.html does. The .html is a file extension.

Then, we can actually move my-first-webpage.html into the new first-webpage directory like this:

epicodus-5:intro-to-programming Guest$ mv my-first-webpage.html first-webpage/my-first-webpage.html

Here, we use the $ mv command. We provide the name of the file we'd like to move, my-first-webpage.html in our case. Then, separated by a space, we provide the new name and location of the file: first-webpage/my-first-webpage.html. By renaming the file from my-first-webpage.html to first-webpage/my-first-webpage.html, we are actually moving it into the first-webpage directory.

If we run $ ls again, we can see that my-first-webpage.html is no longer listed:

epicodus-5:intro-to-programming Guest$ ls

But if we navigate into the first-webpage directory like this:

epicodus-5:intro-to-programming Guest$ cd first-webpage

We can run $ ls again to list all content of this directory:

epicodus-5:first-webpage Guest$ ls

And see that the my-first-webpage.html file is now located here. We've successfully moved the file into another directory using the $ mv command.

Moving and Deleting Content

We also have the option to delete files entirely through the command line.

Removing Files with $ rm file-name

The $ rm command stands for remove and it allows us to delete a specified file entirely.

For instance, we could use $ ls again, to see all content in our current directory:

epicodus-5:first-webpage Guest$ ls

Then, if we wanted to delete our my-first-webpage.html file, we could do so like this:

epicodus-5:first-webpage Guest$ rm my-first-webpage.html

Here, we use the $ rm command, and include the name of the file we'd like to delete. If we run $ ls again, we can see it returns nothing. That is because this directory no longer contains any files. We've deleted the only file it had:

epicodus-5:first-webpage Guest$ ls

Deleting Directories with $ rm -r directory-name

We can also use the $ rm command to delete entire directories. Now that our first-webpage directory is empty, let's delete it.

Firstly, note that you cannot be located within a directory you are attempting to delete. So we'll navigate out of our first-webpage directory using the $ cd command, like this:

epicodus-5:first-webpage Guest$ cd ..

If we run $ pwd we can see we're now located back in the intro-to-programming directory that the first-webpage directory is located in:

epicodus-5:intro-to-programming Guest$ pwd

Now we can delete the first-webpage directory like this:

epicodus-5:intro-to-programming Guest$ rm -r first-webpage

Notice that to delete an entire directory, we must include -r after the rm. This allows us to delete an entire directory instead of just a file. We can run $ ls again to view the current contents of the directory, and see that first-webpage is no longer listed. It's been deleted.

Note that $ rm -r directory-name actually deletes the directory and all of the files within it. Be very careful with this command.

Make sure to check out the cheat sheet tab of this lesson for terminology from this lesson and command line reference of most frequently used commands!