Linux command line quick reference

The handful of commands you need to get around, compile, and run.

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.

Here is a config for Neovim (nvim), a more advanced text editor, that the instructor uses in practice, trimmed to what works without plugins. Download it, then put it where Neovim looks:

mkdir -p ~/.config/nvim
mv ~/Downloads/init.vim ~/.config/nvim/init.vim

Every setting in it has a comment saying what it does.

Recompile on every save

Keep the editor open in one terminal, and in a second one run this, with prog changed to your program’s name:

ls *.[ch] | entr -c -s 'gcc -Wall -o prog prog.c && ./prog'

Every time you save, the screen clears and shows either the compiler’s complaints or what the program printed. Ctrl-C stops it.

  • ls *.[ch] names the files to watch: every .c and .h file here.
  • -c clears the screen before each run, and -s lets the command be a whole line, && and all.
  • With a Makefile, the line is make && ./prog.
  • A program that reads from the keyboard can be typed to as usual. When it ends, entr waits for the next save.

watch comes with Linux. It recompiles and reruns every second, saved or not:

watch -n 1 'gcc -Wall -o prog prog.c && ./prog'

It shows one screenful, without colour, and a program that reads from the keyboard needs its input from a file instead: ./prog < input.txt.

One Makefile for every one-file program

Save this Makefile in the folder with your programs. Then, for a program in prog.c:

make prog        # builds prog, if prog.c changed since the last build
make run-prog    # builds it the same way, then runs it
make entr-prog   # runs it again every time you save prog.c; Ctrl-C stops it

It builds with -Wall -g -fsanitize=address, from its CFLAGS line. A program split into several files gets one line at the bottom naming its other .c files: draw pass: image.c makes make build draw from draw.c and image.c, and pass from pass.c and image.c, with the same flags. No gcc line is needed; make -n draw prints the command make would run.

# One Makefile for every one-file program in this folder.
#   make prog        builds prog from prog.c, if prog.c changed
#   make run-prog    builds it the same way, then runs it
#   make entr-prog   runs it again every time you save prog.c; Ctrl-C stops it

CC = gcc
CFLAGS = -Wall -g -fsanitize=address

run-%: %.c
	@$(MAKE) --no-print-directory $*
	./$*

entr-%: %.c
	ls $< | entr -c $(MAKE) --no-print-directory run-$*

Two halves of one terminal

tmux runs several terminals inside one. With nano open in one half and make in the other, a save and a build are a glance apart. Type tmux once, then:

keys what happens
Ctrl-b then % the screen splits into two halves, side by side
Ctrl-b then o the cursor goes to the other half
Ctrl-b then x the half the cursor is in closes; it asks first

Ctrl-b is pressed and let go, then the letter. exit in the last half ends tmux.

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.