Linux cheatsheet

Linux is the operating system on all pool computers and compute servers at the institute. Many tasks can be done through the graphical desktop; but the real power of Linux comes from the command line (terminal). Here is a reference of the most important commands.

Linux filenames are case-sensitive: File.txt and file.txt are two different files.
01 · First steps

Helpful from the start

Tab completion, command history, and the built-in manual.

Tab completion and command history

Press Tab to auto-complete commands and file names – saves typing and avoids mistakes.
Use / to navigate through previous commands.

man – Manual pages

Displays the full documentation for a command:

$ man ls          # manual for "ls"
$ man man         # manual for "man"

Navigate: Space (forward), b (back), /term (search), q (quit).

Directories and files

ls – List directory contents

Displays files and subdirectories.

$ ls              # contents of the current directory
$ ls -l           # detailed list (permissions, size, date)
$ ls -la          # like -l, also shows hidden files (starting with .)
$ ls Dir/         # contents of another directory

pwd – Print working directory

Shows the full path of the current directory.

$ pwd

cd – Change directory

$ cd directory    # change into "directory"
$ cd              # go to your home directory (~)
$ cd ..           # go up one level
$ cd -            # return to the previous directory

mkdir – Create a directory

$ mkdir newdir            # creates "newdir"
$ mkdir -p path/to/dir    # creates all intermediate directories

Reading and editing files

cat – Print file contents

$ cat file.txt                # prints content to the terminal
$ cat file1.txt file2.txt     # prints both files in sequence

less – Scroll through a file

Better suited than cat for large files.

$ less file.txt

Navigate: Space (forward), b (back), /term (search), q (quit).

head, tail, wc – beginning, end, statistics

$ head file.txt               # first 10 lines
$ head -n 50 file.txt         # first 50 lines
$ tail -n 20 file.txt         # last 20 lines
$ tail -f log.txt             # follow a file live (e.g. logs)

$ wc file.txt                 # lines, words, bytes
$ wc -l file.txt              # count lines only

nano – a beginner-friendly text editor

The most important keyboard shortcuts are always shown in the status bar at the bottom.

$ nano file.txt               # open or create a file

Key shortcuts: Ctrl+O save, Ctrl+X exit, Ctrl+K cut line, Ctrl+W search.

Stuck inside vim? Press Esc, type :q!, then Enter to exit without saving.

Copying, moving, deleting

cp and mv – Copy and move

The target will be overwritten without confirmation!
Command Effect
cp source target Copies file source to target
cp -r srcdir/ dstdir/ Recursively copies a directory
mv source target Moves or renames a file

rm – Remove files

Deleted files cannot be easily recovered. Check your input carefully before pressing Enter.
$ rm file.txt         # deletes a file
$ rm -i file.txt      # asks before deleting (safer)
$ rm -r dir/          # recursively deletes a directory

Searching

grep – Search file contents

Searches files for patterns (regular expressions).

$ grep "term" file.txt        # search in a file
$ grep -r "term" dir/         # search recursively in a directory
$ grep -i "term" file*        # case-insensitive search
$ grep -n "term" file.txt     # show line numbers

find – Find files

$ find . -name "*.pdf"        # all PDF files from the current directory down
$ find ~ -name "*.tex"        # all .tex files in your home directory

Wildcards (globbing)

The shell expands these placeholders before the command runs:

$ ls *.tex                    # all files ending in .tex
$ rm image_?.png              # image_1.png, image_a.png … (exactly one char)
$ ls data_[0-9].csv           # data_0.csv … data_9.csv
$ cp project/* backup/        # all files inside project/

Redirection and pipes

Redirection operators let you control where output goes and chain programs together.

$ ls -l > list.txt            # write output to a file (overwrites)
$ ls -l >> list.txt           # append output to a file
$ sort < list.txt             # use a file as input

$ ls -l | grep ".pdf"         # pipe output of ls into grep
$ cat file.txt | sort | uniq  # sort and remove duplicate lines

Permissions

chmod – Change file permissions

Linux manages permissions for the owner, group, and everyone else (read r, write w, execute x).

$ chmod +x script.sh      # add execute permission
$ chmod 644 file.txt      # rw-r--r-- (standard for files)
$ chmod 755 dir/          # rwxr-xr-x (standard for directories)

Archives

Command Effect
tar -czvf archive.tar.gz dir/ Pack a directory as .tar.gz
tar -xzvf archive.tar.gz Unpack a .tar.gz
tar -tvf archive.tar.gz List contents without extracting
zip -r archive.zip dir/ · unzip archive.zip Pack / unpack ZIP archives

Background processes

Especially relevant on the compute servers: start jobs so they keep running even after the SSH connection drops.

Command Effect
command & start in the background
Ctrl+Z suspend a running process
bg resume a suspended process in the background
fg bring a background process back to the foreground
jobs list your background jobs
nohup command & detach from the terminal (survives SSH logout)

For long-running sessions tmux or screen are usually more convenient – see the SSH page.

Other useful commands

Command Effect
whoami Print current username
df -h Show disk usage (human-readable)
du -sh dir/ Show size of a directory
ps aux List running processes
kill PID Terminate process with given PID
history Show command history
du -sh ~ Show your home-directory usage
hostname Show the current server name (handy when hopping between servers)
which command Show the path of an executable
command --help Quick option summary (shorter than man)

mc – Midnight Commander

A visual file manager for those who prefer a point-and-click style interface:

$ mc

Shell keyboard shortcuts

These work in almost any Bash or Zsh session – a major productivity boost:

Shortcut Effect
Ctrl+R Reverse search through command history
Ctrl+L Clear the screen (like clear)
Ctrl+C Cancel the running command
Ctrl+D End input / exit shell
Ctrl+A · Ctrl+E Cursor to start / end of line
Ctrl+U · Ctrl+K Delete to start / end of line
Alt+. Insert the last argument of the previous command