Introduction to the command line

mikel_brierly

mikel brierly

Posted on November 30, 2020

Introduction to the command line

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)

Screenshot of the terminal listing our current location as

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.

~
Enter fullscreen mode Exit fullscreen mode

(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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

cd Documents 
Enter fullscreen mode Exit fullscreen mode

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

Screen Shot of the terminal listing our current location as

Let's "list" what's inside.

ls
Enter fullscreen mode Exit fullscreen mode

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 ..
Enter fullscreen mode Exit fullscreen mode

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

..
Enter fullscreen mode Exit fullscreen mode
.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Screen Shot of terminal after running the

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
Enter fullscreen mode Exit fullscreen mode

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

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


cd tauntaun
Enter fullscreen mode Exit fullscreen mode
ls
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
touch bad-smells.txt
Enter fullscreen mode Exit fullscreen mode

Screen Shot of running the command

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

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

mkdir stomach
Enter fullscreen mode Exit fullscreen mode

Now enter that directory with the cd command

cd stomach
Enter fullscreen mode Exit fullscreen mode

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

touch hoth-hog.html
Enter fullscreen mode Exit fullscreen mode

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 ../../
Enter fullscreen mode Exit fullscreen mode

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


Useful CLI commands

Print working directory (where you currently are)

pwd
Enter fullscreen mode Exit fullscreen mode

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


Print file contents

cat
Enter fullscreen mode Exit fullscreen mode

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


Search

grep
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
grep "created" test.txt
Enter fullscreen mode Exit fullscreen mode

Screen Shot of a string saying grep

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"
Enter fullscreen mode Exit fullscreen mode

Screen Shot of the command cat test.txt | grep

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


Pipe output

|
Enter fullscreen mode Exit fullscreen mode

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

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

cat name-list.txt | grep "Han Solo"
Enter fullscreen mode Exit fullscreen mode

Screen Shot of the command cat name-list.txt | grep

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For directories:

rm -r
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

rm -i -r hoth-hog.html
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

  • Download this repo to your local machine:

    • Click the green code button at the top of the page

    Screen Shot of button saying

    • In the dropdown menu that appears, click "Download ZIP", and save the file on your computer. (Somewhere you can conveniently access it.)

    Screen Shot of button saying

  • Use the terminal to navigate to the newly downloaded directory.

    • The name of the folder will be command-line-master. master just means what branch of the code you have.

    Screen Shot 2020-11-30 at 8.42.15 AM

  • cd into the rebel-traitor-hunt directory

  • Use the cat command to read the contents of the file instructions-from-vader to get started!

Tips:
  • 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.

💖 💪 🙅 🚩
mikel_brierly
mikel brierly

Posted on November 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related