CLI

The CLI (Command Line Interface)

The majority of our interactions with computers is through a graphical user interface, or a GUI. A GUI is a great tool -- it adds a level of user experience that allowed computers to become more popular and mainstream.

The Command Line Interface (CLI) is a text-based way of interacting with your computer, that gives you more abilities than a GUI has, but with a higher learning curve.

The CLI works by typing commands (running programs) into a terminal and the computer executes those commands or gives you a fairly descriptive error regarding why it did not work.

While the GUI is perfect for the average computer user, there are drawbacks for users like us. The CLI offers many benefits:

Power/Speed. Most tasks can be completed faster with the cli. Features like tab completion, command history (hitting the up arrow in your terminal), piping contribute to this.

Precision. Each command does only one thing and we can read them and understand what they do before we run them.

Repeatability / Scriptability. These commands can be saved, reused by others.

Tools. There are a lot of open source tools that you can use on the cli, you can install them with cli package managers like Advanced Packaging Tool (apt) on Linux or Homebrew on MacOS. Because each tool does one specific thing, it is possible to chain multiple tools together to automate processes.

Debugging. The errors are better, the errors that you get from a GUI can be unhelpful while cli errors are generally more thought out and descriptive.

The Terminal and the Shell

How do we get at this text-based interface from our GUI desktop? We run what's called a terminal application (also often referred to as a terminal emulator). The default on OSX is Terminal.app. When you open a new Terminal window, the Terminal app will call a program called a shell.

A shell is a program that takes commands, passes them to the operating system and returns any output or errors. The default shell used by terminal is called Bash or ZSH on MacOS Catalina or later. There are other shells but all operate very similarly.

Basic Commands

A command is a program. Some come built into the shell and provide the basics for interacting with the operating system and some are written by programmers (like you!) to provide further functionality.

Print working directory: pwd

pwd will print the current working directory. It shows you a path. This path shows you where you are currently located in the filesystem. It's like sending up a flare or homing beacon, where you can see how far you have 'traveled' from the root directory.

You can think of a path as being similar to an address. There are two types of paths: absolute and relative.

A relative path is similar to giving someone directions to a destination from their current location. Where is General Assembly? Two blocks up 15th street from where you are now.

An example of an absolute path to General Assembly could be 1133 15th St NW, 8th Floor, Washington, DC 20005 or a longitude and latitude(38.9048728, -77.0340283).

An absolute path is comparable to giving someone the direct location from a universally known starting point. On the command line that known starting point is the root directory or your home directory (~/).

List: ls

  • ls - Lists the contents of the current directory. You can see:

    • the immediate child directories (the directories inside this directory)

    • the files in this directory

Bash commands can take flags or options denoted by a dash -

  • ls -a - list content including hidden files and directories. Hidden files and directories begin with a period, for example, .git.

  • ls -l - list content and give meta information about each item

Directories (folders) can have directories within them, and there can be directories inside those directories, ad infinitum. This creates a tree structure where directories can have many children with many different branches.

Change Directory: cd

We can navigate to other directories relative to the current working directory.

  • cd some_directory

    • navigates into a child directory called some_directory. some_directory is a child of the current directory.

  • cd ..

    • navigates into the parent of the current directory

    • .. is shorthand for parent

  • cd or cd ~ will take you back home

Make Directory: mkdir

  • Makes a directory (folder)

  • Example:

    • mkdir directory_name

      • makes a directory called 'directory_name'

Create File: touch

  • creates an empty file

  • A file typically will have a file extension like .txt whereas a directory will not.

  • Example:

    • touch file.txt

      • makes a .txt file

Remove: rm

  • Deletes a file with no confirmation.

  • There is no trash or recycle bin to recover removed files from so use with caution.

Remove Directory: rmdir

  • Deletes an empty directory. Similarly to rm, no confirmation or trash to recover from, so proceed cautiously.


DANGER ZONE


Remove recursively and forcefully: rm -rf

This command deletes files and folders recursively and by force. It deletes all the contents of folders and can be dangerous, so use cautiously!

Super user do: sudo

sudo -- or "super user do" -- runs the command that follows as the super user (i.e., 'root' or 'admin'). That means your computer will not prevent you from running the command and may not even confirm if this is what you actually want to do. This is of particular concern when the command may have destructive effects.

Generally, you shouldn't need to run commands with sudo in this course. If you're not sure, ask an instructor.


Most Used Commands

  • cat filename - concatenate, will show you the contents of the specified file

  • cd - will return to home (cd ~ also works)

  • cd directoryname - change directory

  • cd .. - will go up one directory

  • pwd - Print Working Directory, will show you where you are in your computer

  • ls - list, will show you all of the files and folders in the current directory

  • mkdir - Make Directory, creates a new folder

  • rm filename - deletes a file. This is irreversible, the file does not go into the bin, it is gone forever.

  • rmdir directoryname - will delete an empty directory (irreversible)

  • rm -rf directoryname - will delete a directory and all of its files and folders. Does not care if its empty or not. (irreversible)

  • touch filename.extension - creates a file (extension is optional)

  • mv old_file_name new_file_name - rename or move a file or folder

  • cp old_file_name new_file_name - copy a file

  • code . - open current directory in VSCode

Keyboard Shortcuts in Terminal

  • control + l - is a shortcut for clear

  • control + c - is a shortcut for cancel, good if you get stuck in something

  • tab - tab completion, will complete a word for you

  • up arrow - will being up the latest command you typed. Keep clicking to get the second latest, etc.

Last updated