# CLI 101

# Learning Objectives

After completing this lesson, you will be able to do the following with a command line interface (CLI):

  1. Run commands with arguments and modifiers
  2. Navigate the file system
  3. Create folders and files

# Lesson

# Overview

The command line interface (CLI) is a way to communicate directly with a computer. The user types out commands in plain text and it executes on the computer without the needs for buttons or menus. The CLI used to be the only way you could communicate with the computer, but after the invention of graphical user interfaces (GUI) the average computer user in today's world doesn't need the CLI anymore.

So why use a CLI? As a developer, you can perform actions more efficiently, access areas of the computer that aren't accessible in a GUI, and configure your machine for programming.

# Running Commands

To get started, most operating systems have a built in application called "terminal". Launch this and you should see a blank screen the a cursor that let's you input a single line of text, this is called the prompt.

Run your first command by typing pwd and hitting enter/return. It will display a path of the "present working directory".

TIP

# Who came up with pwd?

Back when computers were invented, the programmers had to decide on what to name all basic commands for communicating with the data on a computer. They mostly went for names that were intuitive while not being too vague that commands would overlap each other.

# Using Arguments

The next command you're going to learn is ls, which is a list command. Run that command and hit enter/return. You will see a list of files in your directory

$ ls
Applications Desktop Documents Downloads Library Movies Music Pictures Public
1
2

To add an argument, you add a -- then the argument's name after the command name. Type ls --long and hit enter/return. You will see a vertical list with

~ ls --long
drwx------@ - joshmedeski 23 Jul 12:28 Applications
drwx------  - joshmedeski 23 Jul 13:35 Desktop
drwx------  - joshmedeski 23 Jul 12:22 Documents
drwx------  - joshmedeski 12 Aug 12:51 Downloads
drwx------@ - joshmedeski 30 Jul 16:44 Library
drwx------  - joshmedeski 22 Jul 15:13 Movies
drwx------  - joshmedeski 25 Jul 10:35 Music
drwx------  - joshmedeski 22 Jul 15:13 Pictures
drwxr-xr-x  - joshmedeski 22 Jul 15:13 Public
1
2
3
4
5
6
7
8
9
10

This is where the CLI is more powerful than a GUI. You can see the user that created the file, the files rights (can it be executed, read by the user, etc...). What's even better, is arguments can be typed out in shorthand and combined. Try typing ls -l and you'll see the same results. If you add the --all flag (-a) you can see all files, including hidden files and directories (folders), which start with a .. Try combining both flags by typing ls -la to list all of the files in long form in the terminal.

$ ls -la
drwx------@ - joshmedeski 23 Jul 12:28 Applications
.rw-r--r--  - joshmedeski 31 Jul 13:09 .bashrc
drwx------  - joshmedeski 23 Jul 13:35 Desktop
drwx------  - joshmedeski 23 Jul 12:22 Documents
drwx------  - joshmedeski 12 Aug 12:51 Downloads
.rw-r--r--  - joshmedeski  5 Aug 14:22 .gitconfig
drwx------@ - joshmedeski 30 Jul 16:44 Library
drwx------  - joshmedeski 22 Jul 15:13 Movies
drwx------  - joshmedeski 25 Jul 10:35 Music
drwx------  - joshmedeski 22 Jul 15:13 Pictures
drwxr-xr-x  - joshmedeski 22 Jul 15:13 Public
1
2
3
4
5
6
7
8
9
10
11
12

Notice how .bashrc and .gitconfig files are now show on the list thanks to the --all flag.

TIP

Long arguments are two dashes (--) and any number of letters (ex: --all). The shorthand arguments will always be one dash (-) followed by one letter (ex: -a). However, when in shorthand, you can add multiple shorthand arguments together following one dash (ex: -la).

Arguments will always be specific to the command, you can usually type --help or -h to learn more about the available arguments for the command you are using (ex: ls --help).

# Using Modifiers

Next, you can add modifiers to a command to change it's behavior. One of the most useful skills when using the CLI is navigating the filesystem. The most common command is cd which stands for "change directory". In order to change directories, you have to tell the system which directory you want to change to. Run the ls -l command to see the files in long form.

$ ls -l
drwx------+ 15 joshmedeski  staff   480 Aug 12 12:51 Downloads
1
2

The d at the very beginning of the line indicates that it is a directory. To change to the "Downloads" directory you can type cd Downloads and hit enter/return.

TIP

The 'shell' is the system used to interpret the commands you run. There are different shells available with different features. You'll notice that bash (the default shell) requires you are case sensitive ("Downloads" not "downloads"). Other shells, like zsh, do not need you to be specific with capitalized letters.

Each shell has different pros and cons. It's recommend you get used to bash and learn more about a CLI before switching to a different shell.

Since folders and be put inside folders, it's important to know how to move backwards and forward (upwards and downwards) in a file system. Moving forward you just type the name of the directory you want to change into (ex: cd Downloads) and ../ modifier is used for going backwards (ex: cd ../).

Here is a quick cheat sheet for how

Meaning Meaning Example
/ Root of drive cd /
./ Current Directory open ./Downloads
../ Previous Directory cd ../

You can begin to combine these concepts to do some powerful things with the CLI. If you wanted to go back two directories, you can type cd ../../.

# Creating Files & Directories

Creating new files and directories are two different commands in the CLI. You create directories with mkdir and create new files with the touch commands. Keep in mind that new files are just plain text files, so when you want to create a new file for a graphical user interface program (ex: Microsoft Word) you have to create the new file from within the application. But as a computer programmer, code files are all stored in plain text, which makes it easy to setup a project's file and directory from inside the CLI.

# Summary

The command line interface (CLI) is a powerful tool for the user to interact with the computer directly without needed in a graphical user interface (GUI). Typically computer programmers use the CLI to be more efficient, access areas of the computer that aren't accessible from withing a GUI, and configure your computer to do things for programming needs (start servers, modify access rights, ect...).

Here are the basics you learned:

  • How to execute a command with pwd
  • How to add arguments to a command with ls -la
  • How to add modifiers to a command with cd Downloads
  • How to navigate the file system with cd
  • How to create files and folders with mkdir and touch

# Training Exercises

To solidify your knowledge, here are a set of exercises that will require you to use the techniques you've just learned in the lesson above.

They are organized into small, medium, and large sized problems. The small exercises will be very similar to the examples in the lesson. If you get stuck, refer to the relevant section above. The medium exercises will require you to combine concepts. The lesson may not have a single, specific example for you to reference. The large exercises are more open-ended and may require you to search the web for additional material.

# Small

# Decomposing Commands

Decompose the following commands by identifying the command, arguments and modifiers for each command.

git status
1
rm -rf ./Sample
1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
1

# Medium

# Create a Project

Create a the following directories and files in the following items:

project/
├── index.html
├── css/
│ ├── styles.css
├── js/
│ ├── scripts.js
├── assets/
| ├── logo.svg
| └── photo.jpg
1
2
3
4
5
6
7
8
9

# Large

# Interview Questions

# Fundamentals

  • What is UNIX?

# Bugfix

None available.

# Conceptual

None available.

# Architect

None available.

# Additional Resources