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:
- Command line is commonly used to remotely manage web servers.
- Command line-based programs are easier/faster to create than GUIs.
When talking about command line, you may also interchangeably hear the following terms: Shell, Terminal, Bash.
Let’s construct a definition for each:
- The first Unix CLI program was called “sh” (shell). Now-a-days, we use other programs like bash and zsh, which we collectively refer to as “shell” programs.
- terminal (lowercase t) refers to a graphical application we use to run shell programs.
- Terminal (capital T) is the name of a program on Mac and Linux operating systems. Terminal is a terminal.
CLI Programs
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:
- Mac: Type Terminal into Spotlight.
- Linux: Open Terminal from your applications list.
- Windows: Download/install Git for Windows and then open the included Git Bash program.
With your command line program up and running, let’s jump into some basic commands...
Navigating around directories
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
Creating text files
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...
Editing text files with Nano
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:
- Hit
ctrl
+x
to save your changes. - Nano will ask you to type in the letter
y
to confirm your save. - After typing in
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
Deleting text files and using command flags
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.
Using man pages
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.
Deleting directories
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.
Navigating directories
When working with directories, you can use absolute or relative paths.
- Absolute paths include the full pathname you’re working with and are not impacted on which directory you’re currently in.
- Relative paths may include abbreviated paths and are relative to the directory you’re currently in.
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.
Misc tips
- Use the up/down arrows to cycle through previously used commands.
- Using the tab key when you’re typing a path will attempt to autocomplete that path.
- When commands are run they typically execute and release you back to your prompt. Some commands, however, might not immediately exit, preventing you from running additional commands. When this happens, use the keyboard shortcut ctrl + z to exit the command back to your prompt.
In conclusion...
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 directory -
ls
List the contents of the current directory -
cd /path/to/example
Change to a directory -
cd ../
Go back one directory -
cd ../../
Go back two directories -
mkdir example
Make a directory in current directory -
rm -i example.txt
Remove a file (with confirmation) -
rm -ri example/
Remove a directory (recursively, with confirmation) -
touch example.txt
Create a new text file -
nano example.txt
Edit a text file via Nano