Introduction to the command line
mikel brierly
Posted on November 30, 2020
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
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)
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
.
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
..
.
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 withtouch
, 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 tauntaun
.
mkdir tauntaun
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.
Let's run 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 tauntaun
directory.
cd tauntaun
ls
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!
touch guts.txt
touch bad-smells.txt
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".
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.
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 thetauntaun
directory. (A single../
would have just taken us out of thestomach
directory.)
Useful CLI commands
Print working directory (where you currently are)
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
Print file contents
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.
Search
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
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"
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 thecat
command
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: 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
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.
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!
-
Download this repo to your local machine:
- Click the green
code
button 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
directory
.- The name of the folder will be
command-line-master
.master
just means what branch of the code you have.
- The name of the folder will be
cd
into therebel-traitor-hunt
directoryUse the
cat
command to read the contents of the fileinstructions-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.
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
June 15, 2023