Let’s get into terminal basics. I’m still in my first week at the UC Davis Extension coding bootcamp and I’m excited to share some of the fundamentals that I am currently learning. I’ll jump into them below, but note that I have a Mac so some commands and the interface will look different for Windows (and other) users.
If you’re a beginner, you are probably wondering what Terminal is used for. You will use terminal to easily navigate and access directories and files on your computer. On top of that, you will use terminal to pull and push code to git, but we won’t get into that in this article. I will run through some basic Terminal commands in the following examples that will show you: what directory you are in, how to list files in a given directory, how to move between directories, how to make a new directory, and how to make new files.
A directory is a folder that stores a list of files and other directories. An example of a directory is Desktop, which houses all of the directories (folders) and files that you have on your Desktop.
Terminal Basics: Finding Where You Are
Open up Terminal on your Mac. If you cannot find it on your computer, press command and space at the same time. This will open spotlight. Type in terminal and hit enter. The following is how Terminal will look when you first open it.
To locate where you currently are on your computer, type in pwd and hit enter. pwd stands for print working directory. You will see in the example below that I am in the directory /Users/falondarville.
Next, I type the command ls to list all of the files and other directories that I have in my /Users/falondarville directory.
From there, I can change directory but typing the command cd followed by the name of the directory that I want to move to. For example, cd Development
Here, I can use the command ls again to see what’s in my Development directory. But let’s say I want to go back to the previous directory, or the one before that. I will use the command cd .. to do so.
Use cd .. to go back one directory.
Use cd ../.. to go back two directories. In the following example, I have used the command cd ../.. to go back two directories. You will see that now my location is Users. If I had gone back one step instead of two, I would be at /Users/falondarville
Exercise: Print a list of the items inside your current directory. What do you see and why? Change your directory so that you are back to your own user profile. The directory that I want to go to is /Users/falondarville from Users. After you’ve done this, let’s move on to create our own directories and files through Terminal.
By the way, use clear to empty your Terminal. Don’t be afraid to play around with moving between directories. It’s how you will get comfortable with this tool.
Terminal Basics: Making Directories and Files
To make a new directory, go to the location where you want to create it. For example, I want to make a new directory on my Desktop. I have cleared my Terminal and changed directory to Desktop in the following screenshot.
To make a new directory in Terminal, I use the command mkdir and add the name of my new directory. In the following, I have made a new directory called example using the command mkdir example
Now the folder called example has appeared on my desktop! Locate the new directory you created on your own computer.
I now want to make some new files inside of my example folder. So I change directory to example and then use the command touch to name and create my new files. In the image below, I have created two new files on the same command line. Make sure that when you create files, you include the extension (ex. .html or .css).
The exact command that I used to create the two files is: touch example.html example.css
You can create as many files as you would like in one command line. Try create another file after creating the first two. I used the command line: touch secondexample.html
And now if you want to check the content of your example directory, just use the command ls again. This should show you the three files that you created.
That wraps up my overview of Terminal basics for the time being. Nailing these commands is going to make you a lot more comfortable using this tool. From there, pushing to GitHub and adding an SSH key is not going to seem so intimidating!
If you’re feeling frustrated as a beginner learning code, I wrote a little on the subject in a previous blog post.