Introduction to the command line

2020-30-09

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:

terminal CLI command prompt shell

Here we'll refer to it as the terminal and CLI (command line interface).


A Little Background

An image of an old punch card machine

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


CLI Basics

Alright, Let's get into it. Go ahead and open the terminal application. (It's included on your operating system)

Terminal window that reads 'mhowart1 is on tatooine in ~', and then a blinking cursor.

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!

Type ls and hit enter.

ls

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 command.

cd

cd stands for "change directory". Let's get into the "Documents" directory.

cd Documents

Notice that our current directory has changed to ~/Documents.

Screen Shot of the terminal listing our current location as "~/Documents"

Let's "list" what's inside.

ls

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 cd ..

cd ..

.. indicates going back one directory. A single . indicates right here where you are

..
.

Luke Skywalker looking at the horizon at dusk of the desert planet Tatooine. Two suns are setting in the desolate landscape

I like to remember it by the twin suns of Tatooine, a BACKwater planet.


Make sure you put a space between cd and .. 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 touch luke-skywalker.txt.

touch luke-skywalker.txt

touch is 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


Luke Skywalker laying in the snow of Hoth, nearly dead.

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 tauntaun.

mkdir tauntaun

mkdir stands for "make directory". Let's run ls again to list our new directory.

Screen Shot of terminal after running the "ls" command, showing the file "luke-skywalker.txt" and the directory "tauntaun"

Luke is looking a little chilly though, so let's help him out and put him inside the tauntan.

Han Solo cutting open a tauntaun with a lightsaber

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.

Let's run ls again to see what changed.

Screen Shot of terminal after running the "ls" command, showing the directory "tauntaun"

luke-skywalker.txt is gone, which is expected, but let's check and make sure he's inside the tauntaun directory.


cd tauntaun
ls

Now you should see luke-skywalker.txt inside there!!

Toy figure of tauntaun with luke skywalker inside

Is there anything else you can think of that Tauntauns are full of? Let's add them in!

touch guts.txt
touch bad-smells.txt

Screen Shot of running the command "touch guts.txt" inside the tauntaun directory

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!

Screen Shot of tauntaun directory with the files "bad-smells.txt", "guts.txt", and "luke-skywalker.txt"

Let's make a directory now. We'll call it "stomach".

mkdir stomach

Now enter that directory with the cd command

cd stomach

Inside the stomach, let's make another file, this time with a different file extension just to mix it up.

touch hoth-hog.html

Let's run ls and make sure everything is where we want it to be.

Screen Shot of the file hoth-hog.html inside ~/hoth/tauntaun/stomach

Looks good!


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

cd ../../

This will take us out of the stomach directory, and also out of the tauntaun directory. (A single ../ would have just taken us out of the stomach directory.)

Han Solo saying "Ah! I thought they smelled bad on the outside!" After stuffing Luke Skywalker inside the guts of his dead Tauntaun


Useful CLI commands

pwd

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

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.

Screenshot of output from cat command, a small html file printed to the terminal after running the "cat" command


grep

The 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

Screen Shot of a string saying grep "created" test.txt, and then an output from the terminal with the word "created" highlighted.

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:

cat test.txt | grep "created"

Screen Shot of the command cat test.txt | grep "created" with the output showing "created" highlighted

To fully understand how this is working, see pipe (|) in the next example and give it a go yourself.


Pipe output

|

By using pipe (|), 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.txt is a simple list of names. We can output them using the cat command

Screen Shot of a list of names after running the "cat name-list.txt" command

Now if we take that output and pipe it to grep, we can search its output.

cat name-list.txt | grep "Han Solo"

Screen Shot of the command cat name-list.txt | grep "Han Solo". The output shows only the line with Han Solo's name on it.

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: Han Solo.

If we wanted to see lines around our match, we could use a flag of -C:

cat name-list.txt | grep "Ahsoka Tano" -C 2

Screen Shot of the use of the -C flag at the end of our grep search to see lines above and below our results. See our highlighted result? But it also gave us 2 lines above and below when we used the -C flag.

-C gives lines before and after match -B gives lines before match -A gives lines after match

Remember that | can be used with anything that gives output. (grep is just an example)


File Preview

head

Using the 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.


Line Numbers

-n

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 names-list.txt file.

Screen Shot of file output but with a numbered list on the left-hand side of the results


Copy

cp

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).

ex: cp file1.txt file1-copy.txt - this will create a copy of file1.txt named file1-copy.txt in the same directory you are currently in.


Delete (remove)

For files:

rm

For directories:

rm -r

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 -r flag.

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".


Interactive (confirm commands)

-i

-i will make the command line interactively ask us if we want to perform destructive actions.

rm -i -r hoth-hog.html

Using the -i flag will force you to confirm certain actions, and you can simply reply y or n to continue or cancel the operation.


Manual (help pages)

man

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 man mkdir.

man mkdir

To exit the man page, hit q.


Use your skills!

Darth Vader cutting down rebels with his lightsaber in the hallway of a rebel ship.

Tips:

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.

Related:

tutorialCLI

Back to Home