Lecture

Why the command line exists at all

Most bioinformatics software has no buttons. Aligners, variant callers and pipeline engines are programs you run by typing a command. There are good reasons for that:

  • The data is big. A single sequencing run can be hundreds of gigabytes. It lives on servers, not on your laptop, and you reach those servers through a terminal.
  • Commands can be repeated. A command you typed today can be run again on fifty more samples tomorrow, or by a colleague next year, and give the same result. Clicks cannot.
  • Small tools combine. The command line lets you connect simple programs into an analysis. That idea is the subject of next week.

A shell is the program that reads what you type and runs it. On Linux and macOS the shell is usually bash or zsh. Everything in this course works in both.

Connecting to a remote machine

You will usually work on a server, not on your own computer. ssh (secure shell) opens a terminal on another machine:

ssh student@server.example.org

The first time, you will be asked to confirm the server's fingerprint and to enter your password. Typing a password every time gets old quickly, so set up a key once:

ssh-keygen -t ed25519            # creates ~/.ssh/id_ed25519 and id_ed25519.pub
ssh-copy-id student@server.example.org

From then on ssh logs you in without a password. To leave the server, type exit or press Ctrl+D.

The file system

Linux keeps everything in one tree that starts at /, the root. Your own files live in your home directory, written ~ for short.

pwd                 # print working directory: where am I?
ls                  # list what is here
ls -l               # long format: permissions, size, date
ls -la              # also show hidden files (names starting with .)
cd /data/projects   # go to an absolute path
cd results          # go to a path relative to where you are
cd ..               # one level up
cd ~                # home
cd -                # back to the previous directory

An absolute path starts with / and means the same thing from anywhere. A relative path starts from the directory you are in. . is the current directory and .. is its parent.

Creating, copying, moving and removing:

mkdir -p course/week01/data   # -p creates the missing parents too
cp genes.fasta backup.fasta
mv backup.fasta course/week01/data/
rm course/week01/data/backup.fasta

Press Tab to complete file and command names. It saves typing and, more importantly, typos.

Permissions

Every file has an owner, a group, and three sets of permissions: for the user (owner), the group, and others. ls -l shows them:

-rwxr-x---  1 maria  lab  2048 Oct 11 09:15 run_qc.sh

Read it as - (a regular file; d would mean a directory), then rwx for the user, r-x for the group and --- for others. r is read, w is write and x is execute (for a directory, x means you may enter it).

chmod +x run_qc.sh        # make a script executable
chmod g-w results.tsv     # remove write permission for the group
chmod 640 results.tsv     # numeric form: user rw-, group r--, others ---

In the numeric form r = 4, w = 2 and x = 1, added up for each of user, group and others.

How to look for help

You are not expected to remember options. You are expected to know where to look.

man ls        # the manual page; press q to quit, / to search
ls --help     # a shorter summary, for most programs

When a man page is too long, search the web for the command together with what you want to do, and read the answer critically. Then check it against man.

Practice

Download the course files into a new directory and look around:

mkdir -p ~/course/week01
cd ~/course/week01
curl -O https://bioinformatics.science/courses/linux-and-programming-for-bioinformatics/data/genes.fasta
ls -l
genes.fasta8 short gene fragments in FASTA format

Exercises

Exercise 1

Print the directory you are in, then go to your home directory and print it again.

▸Show solution
pwd
cd ~
pwd

Exercise 2

In one command, create ~/course/week01/data and ~/course/week01/results.

▸Show solution
mkdir -p ~/course/week01/data ~/course/week01/results

-p creates ~/course/week01 too if it does not exist, and does not complain if it does.

Exercise 3

Move genes.fasta into data. From inside results, list it using a relative path.

▸Show solution
mv ~/course/week01/genes.fasta ~/course/week01/data/
cd ~/course/week01/results
ls -l ../data/genes.fasta

Exercise 4

Make genes.fasta read-only for everyone, including you. Try to append to it and see what happens. Then give yourself write permission back.

▸Show solution
chmod 444 ../data/genes.fasta
echo ">test" >> ../data/genes.fasta   # Permission denied
chmod u+w ../data/genes.fasta

Exercise 5

Using man ls, find the options that sort by file size and print sizes in a human-readable form. List data with both.

▸Show solution
ls -lhS ../data

-l long format, -h human-readable sizes (K, M, G), -S sort by size, largest first.