CLI Basics (Part 1 of 2)

banner image

In this week’s newsletter, I’ll be presenting the first post in a short mini series on CLI fundamentals.

Most engineers stumble when they end up needing to use a command line.

The Problem

IDEs (“integrated development environments”) such as VSCode are an important developer tool but sometimes it’s easier and much more powerful to use a command line in conjunction with them.

For example, what happens when you need to:

Even if you’re not a very experienced developer, understanding the basics of CLI commands will be very helpful.

Most engineers will learn as the go, which is fine, but this approach can be inefficient and time consuming. When engineers are “in the moment”, they want to get something accomplished quickly so learning the “right way” goes by the wayside.

What results are bad habits and a half-baked understanding of many key CLI commands.

But what if you sat down, say for a half hour and learned key commands with examples and real world use cases?

My Recommendation on Learning CLI Basics

Here’s what I recommend to learn key CLI commands:

  1. “Dip your toe in the pool”: Take 4 minutes to read over the tutorial below. No need to memorize anything, just get the gist of things.

  2. Gamify your learning: set a daily alarm on your phone to remind yourself to take a few minutes to practice using one command, using the examples provided below. Consistency in learning is key.

  3. Take my quiz when you’re done. I’ll be crafting a quiz on the CLI commands I will be teaching. The quiz will be ready in another week or two so stay tuned.

With that, let’s dive in!

Critical CLI Concepts and Terminology

First off, let’s cover some concepts.

What is a command line interface (or “CLI”), also known as a “terminal”?

A command line terminal is a text-based interface that allows users to interact with a computer's operating system by typing commands. It serves as a window or program where users can enter text commands to perform various tasks, manipulate files, execute programs, and control the computer's functions. A terminal emulates the historical physical terminals used to control computers, way back when Windows or GUIs (“graphical user interfaces”) didn’t exist yet.

All operating systems have an application that allows you to access a terminal. On Windows, we have the Command Prompt application. On macOS, we have the Terminal application and Linux we have terminals like GNOME, Konsole, iTerm2, etc.

When a terminal starts up, something called a “shell” is started. A shell interprets and processes commands. Popular shells include Bash, Zsh, Fish. In this mini-course, we will be only covering the most popular shell: bash. This shell is available on MacOS, Linux and WSL on Windows. Especially if you’re using Windows to follow along, you’ll need to download WSL, which is free, which will emulate a Linux like terminal.

Whenever we use a terminal, we are always executing commands from a location on a computer. A location in macOS can be /Users/<username> and Linux or WSL for Windows /home/<username>. Files are contained in a hierarchy of containers called directories. Each directory is effectively a layer of an onion, so to speak, and each layer is separated by a forward slash (“/”), i.e. /some/directory/on/your/computer

In short, terminals provide a powerful, direct way to interact with computer systems, offering more flexibility and control compared to graphical interfaces.

Basic, “Must Know” CLI Commands

pwd

pwd: Prints the current working directory.

Remember when it was mentioned above that we always execute a command from within the context of a specific directory on our computer? pwd will show you exactly where you currently are at the moment.

The most realistic use of this common command is to check where you currently reside before executing a command.

cd

cd: Changes the current directory.

The cd command in Linux is used to change the current working directory.

The syntax for the cd command is straightforward:

cd [directory]

Say I reside in the /Users/myuser directory and I have a child directory in myuser called code. I can simply run cd code to navigate to that directory.

If I have a sub-sub directory, say /Users/myuser/code/example-project I can also directly navigate to example-project via cd code/example-project. Alternatively, I can provide the full path to the directory to do the same cd /Users/myuser/code/example-project.

Lastly, I can navigate “up” a directory. The “..” (two dots) in CLI terminology represent the directory above our current directory. So say I’m currently in /Users/myuser/code and I want to navigate to /Users/myuser, I can simply run cd ...

mkdir

mkdir: Creates a new directory (i.e. “make directory”).

Here’s the basic syntax:

mkdir [options] directory_name

When you see “[options]” here, if you’re new to CLIs, this means that you can provide a set of options (or “flags”) to change the behavior of a command.

For example, if we only had the directory /Users/myuser but wanted to create /Users/myuser/code/example-project, normally we’d need to first create the code directory, then navigate to that directory, then create the example-project directory. This is tedious, so instead, we can use the -p option like so:

mkdir -p /Users/myuser/code/example-project which will create all nested directories, “code” and “example-project” for me.

Lastly, we can create multiple directories by providing the names of them in a list after the command, like so: mkdir directory-name-one directory-name-two directory-name-three

Note: it is not recommend to create a directory with a space in the name, i.e. “My Project”. It’s also recommended to always use lowercase directory names. If you need to use a space in the name, use a hyphen (“-”) or, if you must, surround the directory name in quotes, i.e. mkdir “My Project”.

ls

ls: Lists directory contents.

The ls command is a fundamental utility used to list directory contents.

The simplest way to use ls is to list files and directories in the current directory:

ls

Common options include listing items with detailed information, showing other information about the directory’s contents such as the date and time each files where created or updated, their permissions (“permissions” will be an advanced concept I will cover later), etc:

Give it a try yourself: ls -l

You can run ls -a to see hidden files, which are fairly common in software projects.

Lastly, you can combine options, like so: ls -la

Here are some other common ls options:

man

man: Is a built-in manual system that provides detailed documentation for commands, utilities, and functions.

To see the “manual” or instructions, especially the list of options that can be used with a given command, you simply run man <command>.

For example, if we wanted to see all the options available to use for the ls command, we’d run man ls.

When a man file is opened, you can navigate down with the ‘j’ key and up with the ‘k’ key, search by typing ‘/’, ‘f’ to move forward by one screen, ‘b’ to move backward one screen and quit by typing ‘q’.

Conclusion

We’ve reviewed key CLI concepts, terminology and 5 useful starter commands. In the next post, we will be covering more key commands, as I wanted to keep this post short and sweet.

Until then, happy coding.

Nick,