The Shell

What is a shell?

  • login is a program that logs users in to a computer.
  • When it logs you in, login checks /etc/passwd for your shell.
  • After it authenticates you, it runs whatever your shell happens to be.
  • Shells give you a way to run programs and view their output.

More about shells

  • Shells also usually include some built-in commands.
  • Shells use variables to track information about commands and the system environment.
  • The standard interactive shell is bash.
  • There are others, though! zsh and fish are both popular.

Working with Files

Listing Files

  • ls List files. You can give it a directory to list.
    • -l Display the output in a detailed list, one line per file.
    • -h Display file sizes in a human-readable format.
    • -a Display all files, including hidden ones.
  • pwd Print working directory.
  • cd Change directory.
    • cd (without a directory) takes you to $HOME
    • cd - takes you to the previous directory you were in.

Rearranging Things

  • mv Move (or rename) files.
    • -i Interactively ask you before overwriting files.
    • -n Never overwrite files.
  • cp Copy files.
    • -r Recursively copy directories, which is what you want to do.
  • rm Remove one or more files.
    • -f Forcibly remove nonexistent files.

Making/Deleting Directories

  • mkdir Makes a directory.
    • -p Makes every missing directory in the given path
  • rmdir Removes an directory.


File and Directory Shortcuts

  • . always refers to the directory you are currently in.
  • .. always refers to the parent of the current directory.
  • ~ refers to your home directory.
  • / refers to the root directory. Everything lives under root.


  • * matches 0 or more characters in a file or directory name
  • ? matches exactly one character in a file or directory name
  • For example, ls *.cpp lists all the cpp files in the current directory.

Looking at files

Looking at files

  • cat Print out file contents.
  • less Paginate files or STDIN.
  • head Print lines from the top of a file or STDIN.
  • tail Print lines from the end of a file or STDIN.
    • -n Print LINES lines instead of 10.
  • sort Sorts files or STDIN.
    • Often paired with uniq to show unique lines.
  • diff Shows differences between files.
    • a/d/c Added/Deleted/Changed.

The Manual


  • man COMMAND opens a manual listing for that command.
    • q quits the manual.
    • Space bar scrolls down one page.
    • / searches for things.
    • n/N go to next/previous search result.
  • For example, man man gives you the manual for the manual!

IO Redirection

When a program runs, it has access to three different "pipes".

  • C++ programs can read input over STDIN using cin
  • C++ programs can write output to STDOUT and STDERR using cout and cerr.

We aren't concerned about STDERR (for now), so let's ignore that.

Not all programs need input; not all programs produce output.

For example...

echo "hello"

... only produces output

By default, the output from STDOUT is sent to your shell...

We can also tell the shell to redirect the output other places.

  • > and >> redirect output to files
    • You'll see how these work in the lab 02 assignment.
  • | redirects output to another program's input
    • You'll try this out in the lab 02 assignment, too.

Consider wc

wc reads input over STDIN, counts the number of lines/words/bytes in the input, and prints the counts on STDOUT.

We can use | to chain programs together. For example...

echo "I love to program" | wc

... tells the shell to redirect the output from echo "I love to program" into the input of wc.

This effectively counts the number of lines, words, and bytes in the phrase I love to program!

You can even chain a bunch of programs together using |

cat myFile.txt | sort | uniq | wc

cat myFile.txt | sort | uniq | wc
  • cat myFile.txt prints the contents of myFile.txt on STDOUT, which is redirected to the input of sort.
  • sort then sorts that input (alphabetically) and its (sorted) output is redirected to the input of uniq.
  • uniq removes any repeated lines and its output (the unique lines) is sent to the input of wc.
  • Finally, wc counts the unique lines and prints the counts on the console.

SO! By gluing simple programs together, you now have a program that can count the number of unique lines in a file!

Intro to Git Version Control

What is version control?

  • Keeps track of changes to your code.
  • You don’t have to worry about accidentally losing or deleting code.
  • You can experiment and reset to a known good state.
  • Makes collaborating with others easier.

What is git?

  • ‘the stupid content tracker’
  • Distributed - everything is kept on your local machine.
  • ‘Repository’ - a collection of code and history.
  • ‘Commit’ - a chunk of saved changes.

Getting Started

  • git init Makes a new empty git repository.
  • git add FILE Adds changes in FILE to the next commit.
  • git status Shows the status of the repository.
  • git rm FILE Removes FILE from working directory and index.

Configuring Git

Tell Git who you are!

$ git config --global NAME
$ git config --global EMAIL
$ git config --global core.editor vim



Looking at stuff

  • git log Show a log of commits
    • --graph Neat ASCII graph
    • -p Show what changed in each commit
  • git diff Show uncommitted changes
  • gitk Graphical log
    • --all Show all branches

Working with remotes

  • git clone makes a copy of a repository.
  • git push Pushes changes from your current branch to the remote branch it tracks. (You may need to run git config --global push.default simple.)
  • git pull Pulls changes from the remote branch and merges them into your current branch.

Git Tips

  • Make your commit messages descriptive!
    • You will lose points for commit logs like asdf or qwert!
  • Don’t add generated files (like a.out) to your repo.
  • You can ignore certain files by putting their names in a .gitignore file in your repo.
  • git help COMMAND will show you documentation.