CS 70

Finding Your Way Around the Terminal

This page is for getting around on the CS 70 server: knowing where you are, seeing what is there, and moving somewhere else. It is not a Unix course, and it does not try to make you fast. It covers the handful of commands you need in order to do the assignments, plus the two tricks that make everything else less annoying.

Nothing here is dangerous except one command, and it is clearly marked.

  • Hedgehog speaking

    I have never used a terminal before and everyone else seems to know what they are doing.

  • LHS Cow speaking

    Most of them are also pretending. There are about six commands on this page and the rest of the semester is built out of them.

  • Goat speaking

    Meh. Why is there no window with folders in it?

  • LHS Cow speaking

    There is, on your own computer. The server is a machine in a rack that fifty people share, and a text connection to it works over any network, from anywhere, without needing to install anything.

The prompt is telling you something

When you log in, you get a line like this and then it waits:

~ SERVER >

That is the prompt. It is not an error and it is not a question. It means the computer has nothing to do and is waiting for you to type a command and press Return.

The ~ at the start is the useful part: it is where you currently are. ~ is shorthand for your own home directory, which is also called /home/username. As you move around, that part of the prompt changes to show you where you have got to.

Where am I, and what is here?

The shell is always somewhere. It keeps track of one directory — your current directory — and remembers it between commands. Nothing on screen represents it the way a window or a highlighted folder would, which is why it is easy to forget it exists, but it quietly changes what your commands mean.

ls with nothing after it means "list what is here". cat notes.txt means "the notes.txt that is here". cd is how you go somewhere else, pwd says where you ended up, and the prompt has been showing you the same thing all along.

So when a command says "no such file" about a file you are certain exists, the answer is almost always that you are standing somewhere else.

Two commands, and you will use them constantly.

pwd
Print Working Directory. Says where you are, in full.
ls
List. Shows what is in the directory you are in.

If ls prints nothing at all, that is not a failure: it means the directory is empty, and a Unix command that has nothing to say says nothing.

  • Duck speaking

    Why is it called a directory and not a folder?

  • LHS Cow speaking

    They are the same thing. “Folder” is what the picture on your desktop calls it; “directory” is what it has been called since before there were pictures.

Moving around

cd means change directory.

cd cs70
Move into the cs70 directory that is inside the one you are in now.
cd ..
Move up one level, to the directory that contains this one.
cd ~
Go home, from anywhere. So does plain cd with nothing after it.
cd -
Go back to wherever you just were. Useful more often than you would think.

A path with a / in it takes several steps at once: cd ~/cs70/homework-00 goes home, then into cs70, then into homework-00, in one go.

A path that starts with / or ~ is absolute — it means the same thing no matter where you are standing. Anything else is relative — it is read from where you are now. Most confusion in a terminal is one of those two being mistaken for the other, and pwd settles it every time.

The two tricks worth learning today

These are not conveniences. They are most of the difference between a terminal being pleasant and being a chore.

Tab completes what you are typing. Type cd ho and press Tab, and the shell fills in homework-00-yourname/ for you. If it does nothing, there is more than one possibility — press it twice to see them. Use it for every filename you type. It is faster, and it cannot misspell anything.

brings back what you typed before. Press it repeatedly to walk back through your history, edit the line, and run it again. You will type git commit a great many times this semester and you should type it in full about four of them.

  • Cat speaking

    So if Tab does nothing, I have either typed something that does not exist, or something ambiguous.

  • LHS Cow speaking

    Exactly right, and that makes Tab a spell-checker as well as a shortcut. A name that will not complete is usually a name that is not there.

Making, copying, moving, and the one to be careful with

mkdir notes
Make a directory called notes.
cp old.cpp new.cpp
Copy a file.
mv old.cpp new.cpp
Move a file — which is also how you rename one.
rm scratch.txt
Remove a file.

rm is the one to be careful with. There is no Trash on the server and no undo. A file you rm is gone, and the only copy you will get back is one you had already committed and pushed to GitHub.

This is a good reason to commit often, and a very good reason to read the line before you press Return.

  • Hedgehog speaking

    What if I delete my homework?

  • LHS Cow speaking

    If you have committed and pushed it, you have not lost it, and this is exactly what that habit is for.

  • Rabbit speaking

    There are also tools that recover deleted files from the disk itself, by finding blocks that were never overwritten—

  • LHS Cow speaking

    We are not doing that. Commit and push.

Looking at a file without opening an editor

cat notes.txt
Dump the whole file to the screen. Good for short files.
less notes.txt
Page through a long file. Arrows scroll; q quits.

If you find yourself stuck in something that has taken over the screen and will not go away, q is the first thing to try, and it works more often than it has any right to.

Anatomy of a command

Almost everything you type has the same three parts:

ls -l cs70
  • ls is the command — the program to run.
  • -l is an option — it changes how the command behaves. Options start with a -, and you can usually give several.
  • cs70 is an argument — what the command should act on.

Once you can see that shape, an unfamiliar command is much less alarming, because you can tell which part is the verb.

  • Horse speaking

    Hay, ls -a showed a load of files starting with dots that were not there before!

  • LHS Cow speaking

    Those are always there. A filename beginning with . is hidden from ls unless you ask for it with -a.

  • RHS Cow speaking

    It is a convention for configuration files, so that your own work is not buried in machinery. Git keeps its entire history in one of them, .git.

Two ways things get away from you

A command is taking too long, or you did not mean it. Press Control C. That interrupts whatever is running and gives you the prompt back.

You want to leave. Type exit, or press Control D. Either ends your session on the server.

One difference that will bite you eventually

The server is case-sensitive and your laptop probably is not. On the server, Vector.hpp and vector.hpp are two different files. On a Mac they are usually treated as the same one.

This means code that compiles on your own machine can fail on the server with a “no such file” error about a file you can plainly see. Nine times in ten it is a capital letter.

Getting more out of a command

Most commands will explain themselves:

ls --help
A short summary of the options.
man ls
The full manual page. Page through it, and press q to leave.
  • Pig speaking

    I want to know EVERYTHING about ls!

  • LHS Cow speaking

    man ls will give you roughly fifty options, and you will use three of them.

  • Duck speaking

    Which three?

  • LHS Cow speaking

    Plain ls, ls -l for details, and ls -a to see the hidden files. That is genuinely most of it.

Where to go next

(When logged in, completion status appears here.)