The Command Line

It is often faster.

A Motivating Example

Quick, create six folders to hold songs for these six artists, using your “Finder” or “Explorer”.


How long did that take?

Here’s how you do it on the Command Line:

mkdir 'Charli XCX' Halsey 'The Weeknd' Khalid 'Lady Gaga' Beyoncé

Note that sometimes quotes are needed. See why?

Why is the Command Line So Cool?

People that often use the command line are sometimes called power users. Why is the command line powerful?

You can do almost anything from the command line. You don’t need to launch many separate window-based applications. With the command line, you can:

These notes do not cover native Windows commands

We are only covering the command language of Bash, which is native to the Mac and the most popular shell on Unix systems. If you are using Windows, you can install Git Bash or other Bash-enabled terminal, no problem! The basic ideas of command line usage, though not the actual commands and shortcuts themselves, will transfer over just fine to native Windows command shells such as cmd and PowerShell.

The Basics

Your command line application carries out a dialog between you and the user. At a prompt, you type in a command and hit Enter (usually). The command executes, perhaps with a response from the system, and then you get a new prompt. Here is a sample session. In this example, the prompt is $, but on your system it might be different:

$ date
Mon Jan 20 17:40:16 PST 2020
$ echo Hello     how  are you
Hello how are you
$ grep qi /usr/share/dict/words 
$ host has address has IPv6 address 2607:f8b0:4007:802::2004

Nice! We’ve just seen four commands:

The set of available commands differs between systems, but there’s a couple hundred that are common.

Commands are super flexible. They have arguments and options. Try:

ls /bin
ls -l /bin
ls -lFG /bin
ls -lG

The arguments and options for each command are listed and explained in great detail in something called the command’s manpage.

Exercise: Find out how to find man pages.

The File System

We’re going to learn how to navigate and manage files using some cool commands and a bunch of powerful keyboard shortcuts, through an in-class exercise of making a travel photo library. But first, there are things about the computer’s file system we just need to know.

About the file system

A few brief notes before we get started:

“Folder” or “Directory”?

Doesn’t matter. Same thing. You can say either.
Exercise: What is the relative pathname of Jessica’s resume, assuming the working directory is /Library/Fonts?
Exercise: If Jessica were in her home directory, what is the relative pathname of her Homework 2 file?

An in-class exercise

We will make a travel photo library, together in class. (For homework, you will clean up and build out a movie library, starting with a very messed up an incomplete set of starter files.)

In this exercise we will learn:

Can’t find the exercise content?

We’ll be doing it in class, that’s why! Take good notes.

Special Characters

Not every character is legal in a filename; some have special meaning within Bash. If you really, really must put them in a filename, you probably can, but in order to use the file, you’ll need quotes around the file name, and even then there’s no guarantee things will work (at least not without some serious worry.) Here are some good things to know:

  1. If at all possible, stick to letters, digits, dashes, dots, and underscores only! PLEASE try to avoid anything else.
  2. NEVER use /, as that separates path components.
  3. NEVER use quotes, as these are the way you save yourself from special characters.
  4. TRY TO AVOID spaces, as they can get irritating, though just quote them if you see them.
  5. NEVER use braces ({ and }) because these get expanded.
  6. NEVER use asterisks, question marks, or square brackets (* and ? and [ and ]) because these get expanded.
  7. NEVER use &, |, <, > because these are used to combine commands and do redirections (whatever those are...).
  8. This list of characters to avoid or never use is not complete. Just remember the first rule.

What does “get expanded” mean?

Braces and the asterisk are used to generate text! It might appear complicated, but really it’s pretty amazing. Braces make arbitrary text. This is called brace expansionHere are examples:

$ echo {b,r,spl,ch}at
bat rat splat chat
$ echo {2..16}
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ echo b{a,e,i,o,u}t
bat bet bit bot but
Exercise: What does the command touch card{0..99}.txt do? Glad you learned the command line now, right?

The asterisk, question mark, and square brackets participate in filename expansion: they generate filenames only (which must exist). Though the actual rules are complicated, * matches any string, ? matches a single character, and [ ]matches a single character from the enclosed pattern, and its pattern language can be quite powerful. We won’t get into the details, but will just look at examples:

$ mkdir scratch
$ cd scratch/
$ touch dog.txt rat.jpg bat.txt animals personal.html
$ ls
animals        bat.txt        dog.txt        personal.html  rat.jpg
$ ls ?at*
bat.txt  rat.jpg
$ ls *.txt
bat.txt  dog.txt
$ ls *o*
dog.txt        personal.html
$ ls *[sgv]
animals  rat.jpg

Summary of file commands

We learned these in our in-class exercise.

pwdPrint Working Directory
cd dirChange Directory: Make dir the new current working directory
cd ..Change to the parent directory
cd /Change to the root directory
cd ~Change to your home directory
cdChange to your home directory (the cool way)
cd -Change to the previous directory you were in
mkdir dir(s)Make a directory or directories
touch file(s)Create a file or files. If any of the files
ls dirList contents of a directory
lsList contents of current directory
findFind and lists files
mv source targetMove source to target. If target is a directory and different than the one the source is in, moves it to that directory. Otherwise renames.
cp source targetCopies source to target. Only copies files. To copy whole tree, use cp -R.
rmdir dir(s)Remove folders. The folders must be empty, or else this won’t work. You can use rm -r to blow away whole trees of your file system if you like. Obviously, you need to be very careful about doing this
rm file(s)Remove files
open file(s)Start the program associated with the given files.

Other Commands

You don’t use the command line only for managing your files. There are tons of other things to do, from starting and stopping programs, to networking, to controlling devices. Please browse this awesome online reference of commands. It covers Linux, but there is a Mac Version too. Most of these commands work in Git Bash too. You can also find a list at Wikipedia.

Some of the cooler commands to learn are:

If you are into details, details, details, see The Bash Reference Manual.

Keyboard Shortcuts

To use the command line like a pro, and save tons of time, you’ll be happy knowing at least of few keyboard shortcuts. Some of the more useful ones are:

Keyboard ShortcutDescription
TabFilename completion (sometimes called “autocomplete”)
Move cursor to beginning or end of line
Move cursor back or forward one word
Up Arrow
Down Arrow
Populate line with previous or next command
Ctrl+KClear the terminal window
Ctrl+RReverse history search

But there are dozens more! Here is a great list of keyboard shortcuts. You can also, if you have tons of time and really, really want to know the details, read the section on command line editing in the Bash Reference Manual.

Exercise: Why is typing Ctrl+A and Ctrl+K in succession useful?
Exercise: Scan the list of shortcuts in the first link above. Select three you think are particularly awesome. Try them out. Write them down. Commit them to memory.

Operating on the Commands

These operations on commands should be a part of any command line users’ knowledge. They can be learned over time.

command1 | command2
(The pipe) Send the output of command1 into the input of command2.
command1 && command2
(AND) Execute command1, then ONLY if command1 succeeds, proceed to command2.
command1 || command2
(OR) Execute command1, then ONLY if command1 fails, proceed to command2.
command1 ; command2
(SEQUENCE) Execute command1, then execute command2.
command1 < file
(REDIRECT STANDARD INPUT) Read input not from the keyboard, but from the given file.
command1 > file
(REDIRECT STANDARD OUTPUT) Output not to the terminal, but to the given file.
command1 >> file
(REDIRECT AND APPEND TO STANDARD OUTPUT) Output not to the terminal, but append it to the given file.
command &
(BACKGROUND) Execute command as a background task. Do this when the command would take a long time to run, or is supposed to run in the background. After launching a command this way, control of the terminal is immediately returned to you. The program you launched will show up in your system list of processes (assuming it hasn’t finished or been killed).

Examples (meant to be executed in order):


Don’t be afraid to get too “advanced.” Instead embrace what can be done. Check out what you have time for, and ignore, for now, what seems a little arcane. There will be time. When you’re ready, here are some good places to go for details, tips and tricks, and fun times:

Try this command:

$ nc 23

Bonus Material: Built-In Folders

You’ll notice a lot of existing folders and files, even on a brand-new machine. A number of these folders are “standard.” The standard folders are operating system dependent. For fun, here are the ones you’ll see in most Linux-based systems (based on this thing called the FHS):

/binEssential binaries (executables)
/sbinSystem binaries - executed by the root user
/libLibraries used by binaries
/usr/binBinaries for end user apps, not the OS itself (non-essential) installed by a package manager
/user/local/binBinaries the end user made themselves
/etc(Editable Text Configuration) config files
/homeParent folder for all the users’ home folders
/bootContains the kernel and other files need to boot up the OS
/devDevice files (interesting)
/opt“Optional” software
/var“Variable” files (heavily modified) like log files and caches
/tmpTemporary files, not persisted, can go away at any time
/procBasically a view into the OS's control of running processes


Make sure you can discuss or explain each of the following:


We’ve covered:

  • Why the command line is cool
  • Some basic commands
  • The file system and file commands
  • How commands can be combined
  • Cool keyboard shortcuts
  • Where to find more information