EECS 298: Social Consequences of Computing

Command Line Interface (CLI)

This is a brief tutorial of command line interface basics.

The GUI (Graphic User Interface) is a “point and click” way to interact with a computer. The Windows File Explorer and macOS Finder are examples of GUIs.

The CLI (Command Line Interface) is a text-based way to interact with a computer. The terminal is another name for the CLI. On the Windows Subsystem for Linux (WSL) it might be called “Ubuntu”. The CLI is fast, easy to automate, and easy to use remotely.

gui vs cli

Prerequisites

If you’re on Windows or macOS and haven’t installed CLI tools on your machine yet, follow one of these tutorials first. Linux users have CLI tools installed by default.

macOS Windows

Keywords

A file stores data like C++ source code (main.py) or plain text (example.txt).

A directory contains files and other directories. It’s also called a folder.

A path is the location of a file or directory. Sometimes we end a directory path with /. For example:

/Users/ohjun/Desktop/project/main.py
/Users/ohjun/Desktop/project/stuff/

Basic Commands

ls

ls prints files and directories in the present working directory.

  $ ls
  example.txt main.py stuff

Pro-tip: Colorize the output of ls to tell files and directories apart (instructions).

colorized ls example

tree

tree recursively prints files and directories. tree is useful for comparing your files against a project spec.

$ tree
.
├── example.txt
├── main.py
└── stuff
    └── hello.txt

Pitfall: You may need to install tree.

$ sudo apt install tree  # WSL, Linux
$ brew install tree      # macOS

pwd

pwd prints the path of the present working directory.

  $ pwd
  /Users/creiglas/Desktop/project

mkdir

mkdir creates a directory.

  $ mkdir newfolder

touch

touch creates an empty file.

  $ touch newfile.py

rm

rm removes (deletes) a file.

rm -rf removes a directory. The rm command deletes files without requiring confirmation and without the option to undo, so be careful!

  $ rm oldfile.py
  $ rm -rf oldfolder/

cd

cd changes directory. You can use cd ~ to return to your home directory and cd .. to move up in the file tree. Check out the Special Paths section for more.

  $ cd stuff/

mv

mv moves a file or directory into a different directory.

  $ mv main.py projects/

mv is also the preferred way to rename a file or directory.

  $ mv oldname.txt newname.txt

cp

cp copies a file.

  $ cp main.py projects/

open / wslview

On macOS, open opens a file or directory with the default application, like a double click (docs).

On WSL (Windows), wslview opens a file or directory with the default application, like a double click (docs).

WSL Pitfall: You may need to install wslu, which includes wslview.

$ sudo apt install wslu

If this does not work, try running:

$ sudo apt update
$ sudo apt install ubuntu-wsl
$ sudo add-apt-repository ppa:wslutilities/wslu
$ sudo apt update
$ sudo apt install wslu

Tips and Tricks

clear Control + l

clear the terminal. Pro-tip: Control + l. That’s a lowercase L.

Tab complete TAB

TAB autocompletes a file or directory name.

Type the first part of a filename, then press TAB. Press again to show multiple completion options.

$ cd ~/src/eecs2  # Press TAB twice to see options
eecs280/     eecs281/     eecs298/

Previous Command

(the up arrow key) cycles through previous commands. The down arrow key can then cycle in the opposite direction.

Colorize ls output

Colorize the output of ls so it’s easy to tell the difference between files and directories.

colorized ls example

Windows/WSL and Linux (Bash shell)

Verify you’re using the Bash shell, which is typical on WSL Ubuntu Linux.

$ echo $0
-bash

Edit your shell customization file.

$ touch ~/.bash_profile  # Create file if it doesn't exist
$ wslview ~/.bash_profile

WSL Pitfall: You may need to install wslu, which includes wslview.

$ sudo apt install wslu

If this does not work, try running:

$ sudo apt update
$ sudo apt install ubuntu-wsl
$ sudo add-apt-repository ppa:wslutilities/wslu
$ sudo apt update
$ sudo apt install wslu

Add this line. Whenever you type ls, you’ll actually get ls --color, which adds color.

alias ls='ls --color'

Close your terminal and reopen it. You should see colorized ls output.

colorized ls WSL example

macOS (Z shell)

Verify you’re using the Z shell, which is typical on macOS.

$ echo $0
zsh

Edit your shell customization file

$ open ~/.zshrc

Add this line. Whenever you type ls, you’ll actually get ls -G.

alias ls='ls -G'

Close your terminal and reopen it. You should see colorized ls output.

colorized ls macOS example

Customize prompt

Customize the terminal prompt to be more helpful and look prettier.

First, complete the Colorize ls output section. At this point, you should know whether you are using Bash or Z Shell, and you should have a working .bash_profile or .zshrc file.

Windows/WSL and Linux (Bash shell)

Add a line to your .bash_profile file that sets the PS1 environment variable.

export PS1='\[\e[0;32m\][\u] \[\e[0;34m\]\w/ \[\e[01;34m\]$ \[\e[0m\]'

Close your terminal and reopen it. It should look like this. For more, check out this guide.

customized bash example

macOS (Z shell)

Add a line to your .zshrc file that sets the PS1 environment variable.

PROMPT='%F{green}[%n] %F{blue}%~%f %B%F{blue}$%f%b '

Close your terminal and reopen it. It should look like this. For more, check out this guide.

customized zsh example

Special Paths

A path is the location of a file or directory.

Current directory .

. refers to the current directory.

For example, you might open the current directory in the Finder (File Explorer).

$ open .     # macOS
$ wslview .  # Windows/WSL

Parent directory ..

.. refers to the parent directory of the current directory.

$ pwd
/Users/creiglas/Desktop/project/stuff
$ cd ..
$ pwd
/Users/creiglas/Desktop/project

Home directory ~

~ refers to your home directory.

$ cd ~
$ pwd
/Users/creiglas
$ ls
Applications Pictures Desktop ...

Root directory /

/ refers to the root directory. This is the top-most directory in your file system, and has no parent.

$ ls /
Applications cores sbin ...

Absolute Path

An absolute path starts from the root directory /.

For example, sometimes it’s useful to make sure the exact file is correct.

$ /usr/local/bin/python3  # One version of Python
$ /usr/bin/python3        # Another version of Python

Relative Path

A relative path starts from the current directory.

For example, running an executable.

$ python3 ./main.py

Glob *

A glob is a wildcard path that may match multiple paths. The * symbol matches any string.

$ ls *.py

More commands

This section contains some more useful commands.

wget

wget downloads a file from the internet.

$ wget https://tacobell.com/menu.pdf
$ ls
menu.pdf

tar

tar unpacks an archive ending in .tar.gz.

$ tar -xvzf archive.tar.gz
starter-files/
...
$ tree
.
├── archive
│   ├── file1.txt
│   ├── file2.txt
└── archive.tar.gz

diff

diff compares two files. No output means the files are identical.

$ diff oldfile.py newfile.py

cat

cat prints the contents of files to the terminal.

$ cat shapes.py
import numpy as np

def get_circumference(radius):
    diameter = 2*radius
    circumference = diameter*np.pi
    return circumference

...

grep

grep searches inside a file. It’s short for “Globally search for a REgular expression and Print matching lines”.

Search for circumference in shapes.py.

$ grep circumference shapes.py
def get_circumference(radius):
    circumference = diameter*np.pi
    return circumference

Search for circumference in all .py files. This example also uses a glob (*).

$ grep circumference *.py
shapes.py: def get_circumference(radius):
shapes.py:    circumference = diameter*np.pi
shapes.py:    return circumference
main.py: from shapes import get_circumference
main.py:    circumference = get_circumference(radius)

...

Shell scripting

A shell script is a file that contains commands. Shell scripts are useful for automating things like running test cases. Learn more at the EECS 485 Shell Scripting Tutorial.

Acknowledgments

Original document written by Andrew DeOrio awdeorio@umich.edu and Oh Jun Kweon ohjun@umich.edu.

This document is licensed under a Creative Commons Attribution-NonCommercial 4.0 License. You’re free to copy and share this document, but not to sell it. You may not share source code provided with this document.