Linux command line quick reference
This is the same card that goes on the board in class, so you can read it from your own screen. Nothing here needs memorising — you will know all of it by about week three from using it.
Four keys that save you the most time
Learn these before you learn any commands.
| Key | What it does |
|---|---|
Tab |
completes a name you have started typing — use it constantly |
Up |
brings back the last command, so you can edit and rerun it |
Ctrl-C |
stops a program that is running away |
clear |
tidies the screen |
If your terminal ever fills with garbage and stops making sense, type reset and press Enter.
Getting around
| Command | What it does |
|---|---|
pwd |
print the directory you are in |
ls |
list what is here |
ls -a |
…including hidden files |
ls -l |
…with sizes, dates and permissions |
cd dir |
change into dir |
cd .. |
go up one |
cd |
go back to your home directory |
mkdir dir |
make a directory |
rm file |
delete a file — there is no undo and no trash |
Writing, compiling and running a program
The whole cycle, in the order you do it:
mkdir cmsc240 make somewhere to work (once)
cd cmsc240 go into it
nano hello.c write the program
gcc hello.c -o hello compile it into a program called hello
./hello run it
In nano, ^ means the Control key: ^O then Enter saves, and ^X quits. Both are listed along the bottom of the screen.
The ./ in ./hello is not decoration. The shell only looks for programs in a fixed list of standard places, and the directory you are standing in is deliberately not one of them. ./ means “no, this one, the one right here.”
Once you are past the first week, compile with warnings turned on:
| Command | What it does |
|---|---|
gcc -Wall -g -o prog prog.c |
compile prog.c into prog |
./prog |
run it |
gcc -fopenmp … |
compile with OpenMP enabled |
Always compile with -Wall. A warning you ignore is usually the bug you spend the evening looking for.
Better nano
Some editing conveniences have to be explicitly turned on. Download this config file for nano into your home directory to do that, renaming it to .nanorc on the way — the leading dot is what makes nano look at it (the dot is missing here for technical reasons).
mv ~/Downloads/nanorc ~/.nanorc
Check the location is right by opening it in the terminal: nano ~/.nanorc.
When the compiler shouts at you
Read the message. It usually names a word that is not the broken one — expected ';' before 'return' is about the line above return, because a statement runs until its semicolon and the compiler has no reason to complain until it reads the next word. Recent versions of gcc point the ^ at the spot where the missing character belongs and print the character underneath as a suggestion, so the position is normally right even when the word quoted at you is not. If a message makes no sense where it points, look one line up.
Reading more
Dive Into Systems has a whole Unix reference in Appendix 2, and it is the first place to look. It is a reference, not a chapter — look things up in it rather than reading it through. The sections you will want this term, roughly in the order they become useful:
| Section | What it covers |
|---|---|
| 17.1 | the command line and the filesystem |
| 17.2 | man, and how to look a command up |
| 17.11 | command history — stop retyping things |
| 17.7 | file permissions |
| 17.6 | finding files, and searching inside them |
| 17.5 | make and Makefiles |
| 17.12, 17.13 | redirection and pipes |
man <command> answers most questions faster than any of the above, and the Bash reference manual is the authority when it doesn’t.