The Command Line Interface (CLI) provides a means of interacting with a computer via text commands.
Back in the day, this was the primary way we interacted with computers. Eventually, though, computers evolved to use graphic interfaces, which allows us to point and click at widgets on the screen to tell the computer what we want to do.
While it may seem that the CLI is archaic compared to GUIs (Graphical User Interfaces) it’s actually a very powerful method for interacting with data and programs on a computer/server, and many of the tasks we need to complete as programmers will happen via command line.
Here are 3 reasons why every web developer should understand how to work in the command line:
When talking about command line, you may also interchangeably hear the following terms: Shell, Terminal, Bash.
Let’s construct a definition for each:
Linux and Mac operating systems are very similar in their command line environments - they both typically run some variation of bash in a pre-installed program called Terminal.
For Windows users to get this same bash-like experience you have a couple options:
Option 1, Git Bash, is the most straightforward route, so that’s what I recommend.
Knowing all this, here’s how you should get your command line program up and running on your respective operating system:
With your command line program up and running, let’s jump into some basic commands...
One of the things you’ll do most in CL is work with files on your computer and navigate around directories. To get started, let’s practice navigating to our computer’s home directory using the cd (change directory) command, followed by the path ~ which is a shortcut for your home directory.
> cd ~
Next, type in the command ls (list) which will show you everything in your home directory.
One of the directories you should see listed is your Desktop.
Move to your Desktop folder using the cd (change directory) command.
> cd Desktop
Use the ls (list) command again to see the contents of your Desktop:
> ls
On the Desktop let’s create a new, empty directory using the mkdir command. We’ll call the directory practice.
> mkdir practice
Now, move into this new directory:
> cd practice
The command pwd (present working directory) can show you the full path of the current directory you’re in. This can be useful if you want to confirm your location before running a certain command. Example:
> pwd
/Users/Susan/Desktop/practice
In our new practice directory, let’s create a new file called example.txt with the touch command:
> touch example.txt
Use the ls (list) command again to see that the file was created:
> ls
example.txt
Now let’s edit this file...
To edit text files directly from the command line, you can use the simple CL text editor called Nano by running the command nano followed by a path/file you want to open. Using Nano, open the example.txt file you just created:
> nano example.txt
Enter the text This is a test... into the file.
Then, here are the steps to save your changes in Nano:
ctrl
+ x
to save your changes.y
to confirm your save.y
, hit Enter.To confirm your changes were made, you can use the cat (concatenate) command which will output the contents of any text file directly in the console:
> cat example.txt
This is a test...
Practice: Try opening example.txt in Nano again, making some edit to the text and saving again. Use cat to confirm your save worked.
Want to learn more about Nano? Check out this guide: Simple Command Line Text Editing with Nano
We’ve created a file, we’ve edited it, now let’s delete it by running this command:
> rm -i example.txt
remove example.text? (type 'y' for yes and hit Enter)
Note the addition of the -i... this is a flag which is how you send extra instructions when using commands.
In this case the i flag is short for interactive, meaning it’ll ask you before deleting files. It’s a good habit to use the i flag when working with rm so you don’t accidentally delete anything you didn't mean to.
Mac/ Linux users: To learn more about any of the commands so far, you can type man followed by the command name. This will tell you how to use the command and all the flag options you have. These “manuals” are called “man pages” and they exist for almost all command line programs.
> man rm
The output from the man command will often span multiple screens. Use the Enter key to page through the output, or hit ctrl + z to exit.
Windows users: Unfortunately man does not work in Git Bash, but you can learn more about common commands by typing them in at ExplainShell.
Let’s clean up the practice directory we created.
First, run the following command to move “up” one directory (i.e., out of the practice directory):
> cd ../
Confirm you’re back on your Desktop:
> pwd
/Users/Susan/Desktop
And now remove practice:
> rm -ir practice
Note the addition of the r flag, which is needed for recursively removing directories and their contents.
When working with directories, you can use absolute or relative paths.
For example, imagine I had three nested directories on my Desktop, a, b, and c.
If I wanted to move into the c directory from any directory I’m currently in, I could use the absolute path:
> cd ~/Desktop/a/b/c
But let’s say I’m already in the a directory (~/Desktop/a). I could navigate to the c directory using a relative path:
> cd b/c
(Note how this path does not start with a /, indicating the path is relative to my current location.)
Similarly, imagine I’m now in the c directory and I want to get back to the a directory. Using an absolute path I could do this:
> cd ~/Desktop/a
Or, I could use the “go up one directory” trick shown above to first go up to the b directory, then the a directory:
> cd ../../
You can use both absolute and relative paths and you’ll find they come in handy in different circumstances.
For example, absolute paths are useful when you’re trying to get to a directory that is far removed from your current directory and you know the full path of where you’re trying to go.
Relative paths are useful when you’re “exploring directories” - jumping around different directories from where you currently are, in search of something.
There is a lot more you can do in command line besides working with files and directories. The above exercise was just to get you familiar with working with commands.
To wrap things up, here’s a quick reference list of the commands we covered:
pwd
Find out which directory you’re in (present working directory)cd ~/
Go to your home directoryls
List the contents of the current directorycd /path/to/example
Change to a directorycd ../
Go back one directorycd ../../
Go back two directoriesmkdir example
Make a directory in current directoryrm -i example.txt
Remove a file (with confirmation)rm -ri example/
Remove a directory (recursively, with confirmation)touch example.txt
Create a new text filenano example.txt
Edit a text file via Nano
For $4, you’ll get 6 months of unlimited access to the notes for the above video
and all of my other 200+ guides and videos.
This is a one-time payment— no subscriptions or auto-renewals to worry about cancelling.
Your support helps me continue creating free, high-quality videos. Thank you!