Since I’m a Mac user, this tutorial references Terminal (command line app) and Spotlight (a Mac built-in search utility) frequently. All the commands in this tutorial will work for you if you are on a Linux based OS. These commands will also work for you on a Windows computer (in the Command Prompt app) once you’ve installed Bash on your Windows machine. Here’s a quick tutorial that shows you how to do that.
As folks who tinker on the web, we often come across packages and libraries we need to install using the command line. If you don’t know your way around the command line, these instructions can seem scary and written in a foreign language. If you mess something up or something doesn’t work exactly the way the tutorial describes, you’ll find yourself frantically googling for a solution in a state of panic, because you don’t really know what’s going on.
No more. After today, you’ll have a solid handle on how the command line works, what the most common commands are, and how to use them. So let’s get started.
How do you access the command line?
On a Mac, you have the option of using the built-in app, Terminal, or downloading another app. I use iTerm2. It has a lot of configuration options to make the Terminal look how you want, tabs, and a host of other useful features.
On Windows, you can use the built-in app, Command Prompt or download the new Windows Terminal.
Another popular Terminal app available for Mac, Windows, and Linux is Hyper.
Take your pick, any and all the options are just fine to get started with! I’ll be calling the app we use to access the command line, Terminal, in the rest of the tutorial.
Getting Familiar with the Terminal
I’ll be sharing graphical representations of the command line in this tutorial. My screenshots won’t look exactly like your Terminal, but you’ll still be able to follow along.
Before we deep dive into commands, we need to get comfortable with the Terminal. Open up the Terminal app on your computer (you can use Spotlight to search for it). Let’s take a look at the what we see when we first open up the app:
The very first thing you’ll see in the header of your Terminal is likely your username, then the term bash, and then the pixel size of the Terminal window (go ahead and resize the window and watch the numbers change!). The username and size are obvious, but what is bash?
Bash is the name of the language you’ll be using to input your commands into Terminal. There are other languages as well, however, this one is the default. Bash is the most common language used in the Command Line Interface.
If you’ve opened the Terminal app before, it’ll show you the exact time you used it last time.
The next line will begin with the name of your machine (if you’ve never named your machine, it’ll just be something default like MacBook-Pro), then a colon (:) and a tilde (~) symbol.
After your machine’s name, it indicates where you are inside your machine. In this case, it shows the tilde (~) symbol. The tilde (~) symbol means home directory. On your Mac/Linux/Windows, this is the folder labelled your username. For example, my username is aurooba, so I have a home directory called aurooba where my Documents, Pictures, Desktop, and other folders live.
Then it shows your username, this indicates that you are signed in as this user and will be inputting commands as this person. And then the cursor, which is where you’ll start typing your commands.
Within the Terminal, you don’t use your mouse or trackpad, everything is done with your keyboard. At first, this can seem cumbersome and annoying, but as you get quicker, you’ll find this is much faster than using a mouse or trackpad for certain things.
Things to Remember
Changes you make with the Command Line are permanent, there is no undo button. So always double check that you want to do something before you do it with the Command Line. Yes, you can delete things you’ve created, but once you’ve deleted something, there’s no way to get it back.
Spaces are special in the Command Line. Have you ever wondered why the pros always use dashes and underscores in their file and folder names? It’s because spaces are used to separate commands and parameters. Each set of characters separated by a space is treated as a different command. So if you have a file or folder name that has a space in it, you have to use a backwards slash (\) to indicate that space is part of the name and not a separate command. This can get very annoying very fast, which is why it’s better to not have spaces in your names in the first place.
If the text in the Terminal starts to get overwhelming and you want to start with a clean slate (without erasing your history), type in the command
clear and hit
Enter, this will clear up your Terminal window, but you’ll still be able to scroll up to see what else you did in this session.
The Bash Help Manual
If you ever want to know what a command does and what options (or parameters) it has, you use the command
man and then the command you want to look up. For example, the first command you’re going to learn is
pwd, if you wanted to know what this command is, what other options are available with it, you would type in
man pwd and then hit
The manual page for the command will open up, you can go up and down using your arrow keys.
- The first section is the name of the command with a short description of what it does.
- The Synopsis section shows you how this command should be used; any options in square brackets (in this case, -L or -P) are optional. The separator (
|) means or.
- The Description section gets more specific and shows you exactly what each option means and does.
At the very bottom, you’ll see a colon and then the cursor. if you type
q, you’ll quit the manual and be back to your normal Terminal window.
Moving from Place to Place in the Command Line
Before you can use more complicated commands, you need to know how to move around your computer using the command line.
What’s the path / URL of the current directory?
Although you start in the home directory and you probably know where that is already, you can always check the exact path (the URL) of your current directory by using the command
pwd (print working directory).
pwd and hit
Enter. The exact path of your current directory will show up underneath and then a place for you to type in more commands:
In this case, it shows that on your hard drive, there is a main folder, Users, and your home directory is located within it.
What’s inside the current directory?
You need to know what’s in your directory before you can decide where to go. To see all the contents of the current directory (other folders and files), you use the command
ls (list). Type in
ls and hit
These are all the folders (and maybe some files!) inside your home directory.
What’s inside a specific directory?
You can use
ls to list the contents of any directory by including the name (or path) to it after the
ls command. For example you can list the contents of the Documents folder without being in it by using the command
Or you can list the contents of the Users folder, which is the parent directory of your home folder by using the command
.. means parent directory in bash.
How do you change directories?
Let’s say you want to move to the Documents folder. You would do that with the command
cd (change directory) and the name of the folder after it. The Terminal is smart, if you start typing the (case-sensitive) name of a folder and then hit
Tab, it’ll autocomplete the name. Try it out: type in
cd Doc and then hit
Tab. The Terminal autocompleted it to say
cd Documents/. Handy, right? Now hit
Now the tilde (
~) has changed to
Documents beside the name of your machine. This indicates that you are now inside the Documents folder. You can use the
pwd command to double check. 🙂
How do you go back a directory?
You’ve gone deeper into the home directory, but what if you want to go back? The directory system uses an outline system, so when we go into a directory, we’re going down, if we want to go back into the the parent directory, we’re going up. To go up one directory you
cd .. the
.. indicates going one directory up.
As you can see, the tilde (
~) is back, we’re in the home directory again.
Pro tip: You can also go back to your home directory from wherever you are by simply typing
cd and hitting
How do you open a directory?
To open a directory in the graphical interface (Finder), you navigate to the directory and then type
open ., the period (
.) means current directory in this context.
Creating Files and Directories in the Command Line
How do you create a directory?
Creating a new directory (folder) in Command Line is super easy. The command is
mkdir (make directory) and then the name you want to give the folder. For ease of use in the Command Line, it’s recommended not to have spaces in your names, because spaces are special in Command Line; use dashes or underscores instead.
Let’s create a new directory in our Documents folder called
awesome-folder: type in
mkdir awesome-folder and hit
Now if you
ls (list) the contents of the Documents folder, you should see a new folder called awesome-folder!
How do you create a new file?
Creating a new file is just as easy as creating a new directory. The command to create a new file is
touch and then the name (with the file extension!) you’d like to give the new file.
Let’s create a new text file called newfile inside our brand new awesome-folder directory. Type in
cd awesome and hit
Tab to autocomplete the name, and then hit
Enter. Once you’re in the awesome-folder, type in
touch newfile.txt and hit
Enter. Then you can
ls (list) the folder contents, and you’ll see a new file newfile.txt in the folder!
You can create any kind of file using the touch command. You could create a blank PSD file:
touch newfile.psd, or a new MS Word file:
touch newfile.doc. As long as you know the file extension of the type of file you’d like to create, you can create it.
Where am I and how do I get out?! OMG OMG OMG
This may or may not happen a lot to you, but sometimes when I used to follow some directions from a website (or from Stack Overflow), I would sometimes end up somewhere in Terminal that I couldn’t get out of and I would freak out.
Normally, if you’re in a weird place or want to quit a process you can either type in
q or hit
CTRL + C to exit.
But there’s a situation in the Terminal where that doesn’t work: when you’ve somehow ended up in the Command Line editor called VIM (nope that doesn’t stand for cleaning powder, it stands for Visual Editor iMproved).
VIM is a strange beast and should only be used by advanced developers who know exactly what to do. If you’ve ended up here, have no fear! I’ll show you how to get out:
- Hit the
ESCbutton, this puts the editor in command mode, ready to receive commands.
- Then you have a few different options of how you want to quit:
- Type in
:qto quit (yes, type that colon in) then hit
:q!is used to quit without saving (yes, type that colon in) then hit
:xto write (save) and quit (yes, type that colon in) then hit
:qato quit all (yes, type that colon in) then hit
If you don’t know what you’re doing and may have accidentally started typing wildly in a vain effort to get out, I suggest using the
:q! option to be safe.
And voila! You’re out. You can take a deep breath in (and out).
Special thanks to Bobbi Reyda for help making the graphics pretty.