This guide will go through what the the command line interface (
CLI) is, why we need it, and how to start using it!
NOTE: All the examples here are for Mac/Linux users. If you are on Windows 10 or newer, you can use the Windows Subsystem for Linux.
You'll often hear the
command line referred to in different ways:
Here we'll refer to it as the
CLI (command line interface).
Before the introduction of the graphical user interface (screens with a mouse pointer), the only way to interact with your computer was through the
terminal. The earliest terminals (dating back to the mid 1960's) literally punched out the user's program (hand-written and then transcribed) onto to a card. The terminal has evolved exponentially since then, but it is still text-based, and it is still the most efficient way to communicate with our machines.
Here is a really good thread talking about why punched cards were used for early computing
Alright, Let's get into it. Go ahead and open the terminal application. (It's included on your operating system)
In your terminal, you'll see some information: which user is currently logged in, the name of the computer, and where you are in your computer right now. (there might be some slight variation, and this output is configurable). You should see a little squiggly line toward the end.
(if you don't see that, type
cd and hit enter)
That little squiggle is called a
tilde, and it indicates that you are at your home directory (you'll see it on the key above
tab on your keyboard).
What's my home directory you say? Lets find out!
ls and hit enter.
This command "lists" all the items in your current
directory. (you can think of directories as folders). You should see some directories that look fairly familiar, such as Applications, Desktop, Documents, and Photos. To get into one of these directories, we can use the
cd stands for "change directory". Let's get into the "Documents" directory.
Notice that our current directory has changed to
Let's "list" what's inside.
Probably some directories (folders), and files, each will be displayed differently. Directories are usually indicated by a color of some sort, and files are usually plain.
But for now, let's go back to the Desktop directory. To navigate back, type
.. indicates going back one directory. A single
. indicates right here where you are
I like to remember it by the twin suns of Tatooine, a BACKwater planet.
Make sure you put a space between
.. or the terminal will yell at you.
Now that took us back to the home directory
~, right where we were before. (if you want to jump to the home directory from any location, just type
cd by itself).
Now let's make a file of our own! Type
touchis a command used to create files! You have a lot of power with
touch, because you can add any extension you like to files you create, such power!
When you are creating your file, make sure that your filename has no spaces in it, as the command line will interpret "luke" and "skywalker" as two separate files to create if there is a space
I think our file needs a better place to live, rather than exposed out in the hoth-like conditions of the home directory. Let's create a new directory for our file to live in. Type
mkdir stands for "make directory". Let's run
ls again to list our new directory.
Luke is looking a little chilly though, so let's help him out and put him inside the tauntan.
mv luke-skywalker.txt tauntaun
mv takes two
operands (parameters), the first one is the item to be moved, and the second is the
directory it is to be moved to.
ls again to see what changed.
luke-skywalker.txt is gone, which is expected, but let's check and make sure he's inside the
Now you should see
luke-skywalker.txt inside there!!
Is there anything else you can think of that Tauntauns are full of? Let's add them in!
When we run this command, it creates that file inside the tauntaun directory because our current location is inside of it.
now when we type
ls, we see that our "tauntaun" directory is full of guts, bad smells, and Luke Skywalker!
Let's make a directory now. We'll call it "stomach".
Now enter that directory with the
Inside the stomach, let's make another file, this time with a different file extension just to mix it up.
ls and make sure everything is where we want it to be.
At this point, we are inside the stomach of the tauntaun. but what if we wanted to go back multiple directories?
We can do that with the by chaining our
.. operand. To go back two directories, we would say
This will take us out of the
stomachdirectory, and also out of the
tauntaundirectory. (A single
../would have just taken us out of the
pwd allows you to check where you are currently located in your system.
It stands for "print working directory", and it shows you where you currently are. It will print out the
path of where you are all the way back to the root directory (
~= home directory
/= root directory
cat shows us the contents of a file right inside the terminal itself. Keep in mind this command is most useful for shorter files that are only a few lines long. (cat stands for concatenate, which means "link (things) together in a chain or series").
Here's an example of what the
cat command prints out for a small html file.
grep command searches any given input, selecting lines that match, and printing out what did match! Let's look at a simple example:
# create sample file: echo "I am a test string I created just now" > test.txt
grep "created" test.txt
See how grep highlighted our search criteria?
grep can also search the output of another command, not just a file! Let's see how it could work with
cat test.txt | grep "created"
To fully understand how this is working, see
|) in the next example and give it a go yourself.
|), you can take the output of any command, and pass it into another command.
There are many things this could be used for, but a common one you can focus on now is using it in conjunction with
grep to search for things. If you wanted to search the contents of a file, you could output it's contents using
cat, and then search with
grep using a pipe.
name-list.txtis a simple list of names. We can output them using the
Now if we take that output and pipe it to
grep, we can search its output.
cat name-list.txt | grep "Han Solo"
grep only gives output if it finds a match for what you passed it! In this case, it gave us the line with our search criteria:
If we wanted to see lines around our match, we could use a
cat name-list.txt | grep "Ahsoka Tano" -C 2
-Cgives lines before and after match
-Bgives lines before match
-Agives lines after match
| can be used with anything that gives output. (
grep is just an example)
head command on a file will give you only the first few lines of the file. This can be helpful if you need to see a header, license, or just to check the structure of a document.
If you want to see your output numbered by line, you can use the
-n flag. Any command that gives output can be used with the
-n flag. Here's an example using
cat on our
copy takes two parameters, the first is the file to be copied, and the second is what you want to call the copied file (it can't have the same name as the original).
cp file1.txt file1-copy.txt - this will create a copy of
file1-copy.txt in the same directory you are currently in.
rm stands for remove, but the
-r can be a little confusing. The
- indicates a flag, and the
r stands for "recursively". We'll get more into recursion later, but for now, you can think of
-r as a way to manipulate directories when a command without
-r won't work.
For example, running
rm tauntaun will tell you
rm: tauntaun: is a directory. So we need to use the
Quick note on deleting and modifying files: When you give CLI commands, there is usually no confirmation and no "undo's". If we want the command line to be a little more forgiving, we can use the "interactive" flag, "-i".
-i will make the command line interactively ask us if we want to perform destructive actions.
rm -i -r hoth-hog.html
-i flag will force you to confirm certain actions, and you can simply reply
n to continue or cancel the operation.
man stands for manual, and is used in conjunction with other commands. If you want to learn more about any specific command, you can simply type
man before it, and you'll get a very thorough explanation of what it can do. For example, if you wanted to learn more about the
mkdir command, you would type
To exit the man page, hit
Download this repo to your local machine:
- Click the green
codebutton at the top of the page
- In the dropdown menu that appears, click "Download ZIP", and save the file on your computer. (Somewhere you can conveniently access it.)
- Click the green
Use the terminal to navigate to the newly downloaded
- The name of the folder will be
masterjust means what branch of the code you have.
- The name of the folder will be
catcommand to read the contents of the file
instructions-from-vaderto get started!
- Keep this webpage open for referencing commands you'll need, and for brushing up on the lesson as you go along.
- For extra help, open up the walkthrough file. It has detailed hints, and the commands to run if you still need that extra push.
If you want to learn more about the command line, or find more commands, MDN has a wealth of information, and goes into a lot of detail explaining what you can accomplish with this tool. For more reading, this is a good starting point.