You're used to navigating around your computer by clicking on folder icons and using menus to create new files and folders. We can do all of this from the terminal as well, by typing out commands instead of using the mouse to click on things.
If you're on one of the lab computers or on your own Mac, search for the application Terminal and open it. If you're using Windows, search for cmd.exe instead. After you open the program, you should have a window that has something like this in it:
Alonzos-MacBook:~ alonzo$
The little bit of text that's already in the window is called the prompt. It gives you a bit of information about the computer that you're on. When you type commands, they appear to the right of the prompt.
Let's learn how to navigate around. Right now, you're in the home directory, also called ~. A directory is just another name for a folder (like the Documents
folder).
ls
: listing contents of a directory
To see what's in the current directory, we can use the command ls
If you're on Windows, use dir
instead. The ls
command lists the contents of our current directory. To use a command, type it in the terminal, then press 'enter'. Try it out. You should see something like this:
Alonzos-MacBook:~ alonzo$ ls
Applications Downloads Pictures
Library Public Music
Desktop Movies Documents
cd
: navigating to another directory
We can see that the home directory has some other directories in it, such as Documents
and Desktop
. If we want to go into one of those directories, we can use the command cd
, followed by that directory's name. Try it out by using the command cd Documents
.
Alonzos-MacBook:~ alonzo$ cd Documents
Alonzos-MacBook:Documents alonzo$
Now you're in the Documents
directory. We can use ls
or dir
again to see the contents of Documents
.
How can we get back to the home directory from here? cd ..
moves us upward one directory, which puts us back in home. The "dot dot" always means "parent directory". This means we can use cd ..
to go to upward one directory. Try it out!
Alonzos-MacBook:Documents alonzo$ cd ..
Alonzos-MacBook:~ alonzo$
mkdir
: making a new directory
Okay, now that we've tried that, let's go back to Documents
. (If you're in home
, use the command cd Documents
). You're probably used to making new folders on your computer by using a menu action like "File > New Folder", or a keyboard shortcut like "shift-cmd-N". To make a new directory (folder) using the terminal, we can use the command mkdir
followed by the name we want for the new directory. Try the command mkdir PythonLab1
.
Alonzos-MacBook:~ alonzo$ cd Documents
Alonzos-MacBook:Documents alonzo$ mkdir PythonLab1
Nice! now use ls
or dir
to confirm that your directory has been created! We can use cd PythonLab1
to go into our new directory.
Alonzos-MacBook:Documents alonzo$ cd PythonLab1
Alonzos-MacBook:PythonLab1 alonzo$
You can also use the terminal to do almost anything, such as moving, copying, and removing files and directories. If you would like an explanation of some other useful terminal commands check out this article.