Linux – Basics

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.

Helpful from the start

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).

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

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)

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

mc – Midnight Commander

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

$ mc