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. Let's get started!

Basics

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
/Users/Guest

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 commadn 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, like this:

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

We can see our Desktop directory contains two other directories: A folder called my-coding-project and another folder called coding-practice. (Depending on the directories on your desktop, your response will differ. Again, this is totally normal.)

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

epicodus-5:~ Guest$ ls Documents
cover-letters        

Here, we can see this computer has a cover-letters directory in its Documents directory. (Again, the response will differ based on the files and folders present in your specific computer.)

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.

Navigation

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
/Users/Guest/Documents

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
/Users/Guest/Documents

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
/Users/Guest

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
/Users/Guest

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
my-first-project.html

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
my-first-project.html

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
my-first-webpage.html

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
first-webpage        

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
my-first-webpage.html

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
my-first-webpage.html

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
/Users/Guest/Desktop/intro-to-programming

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!

Terminology


  • Path: The unique location of a file or folder in the file system hierarchy. Example: Users/Andrea/Pictures/fun-day.jpg is the path for the image called fun-day.jpg.

  • Directory: Another name for a folder in file system hierarchy. Example: Users/Andrea/Pictures/fun-day.jpg is a path that has 3 directories: Users, Andrea and Pictures.

  • Subdirectory: A directory within another directory. Example: In the path, Users/Andrea/Pictures/fun-day.jpg , Andrea is a subdirectory of Users and Pictures is a subdirectory of Andrea.

  • Home Directory: The topmost level of your user directory; the directory where you begin when you open the terminal. (To view your home directory in the Finder (the graphical user interface on the Mac), click somewhere on the desktop, click on the Go menu at the top of the screen, and select the Home menu option. To get to your home directory in the Terminal, type cd ~ at the command line prompt.)

Frequently Used Commands:


For reference, here are all the commands we learned about in this lesson. Feel free to reference this as much as you need! Pretty soon, these will become second-nature:

  • $ pwd: prints the path to the current working directory to the screen.
  • $ ls: lists out the directories (folders) and files in the current working directory.
  • $ ls directory-name: lists out the directories and files in the specified directory.
  • $ cd directory-name: changes the current directory to the one specified.
  • $ cd ..: change current directory up one level to the parent directory.
  • $ cd ~: change the current directory to the home directory (/Users/Guest on Epicodus computers).
  • $ mkdir new-directory-name: creates a new directory (folder) in the current working directory unless otherwise specified.
  • $ touch new-file-name: creates a new, empty file in the current working directory unless otherwise specified.
  • $ mv file-name new-file-name: moves the contents of the first file into the second file, essentially renaming the file. First file is removed.
  • $ cp file-name new-file-name: creates an exact copy of the contents of the first file into the second file. Both files exist.
  • $ rm file-name: deletes the file.
  • $ rm -r directory-name: deletes the directory and all of the files within it. Be very careful with this command.
  • $ cat file-name: prints out the contents of the specified file to the screen.
  • $ cd Desktop change directory to a directory named Desktop (located inside of the current directory)
  • $ cd /Users/Guest change to /Users/Guest directory (which happens to be the home directory on the Epicodus computers)
  • $ cp Documents/hi.txt . copy the file hi.txt from the Documents directory to whatever directory you are currently working in
  • $ mv Documents/hi.txt . move the file hi.txt from the Documents directory to whatever directory you are currently working in