00 - The command line and Linux

Table of Contents

  1. Introduction
  2. Command line

Introduction

Early computers were text based, while nowadays most are graphical with a mouse or touchscreen. Previously you had to type in commands and see the results on screen as text, working with what is known as the "command line", "terminal" or "shell". Even on modern computers this functionality is still present, and it still has a number of advantages - particularly for automating work.

What is Linux? It is a free open source operating system replicating the capabilities of earlier commercial Unix systems. While most laptops today run Microsoft Windows or Apple macOS, for servers and clusters Linux is much more common.

Why use Linux? The majority of cutting edge bioinformatics tools are written for use under Linux, typically at the command line. Many of these tools will also work directly on Apple macOS, which is also a type of Unix. Relatively few bioinformatics tools will run under Microsoft Windows - although interestingly Windows 10 has the capability run Ubuntu Linux for a hybrid approach.

So that you can use your own laptops, for this workshop we have focused on tools which are available on Linux, Apple macOS and Windows.

Command Line

Fundamentals of working at the command line

If you were sitting at Linux or Apple computer, there would be a standard "Terminal" application for accessing the (Unix) command line. From this command line terminal you could access remote Unix/Linux servers using the ssh command.

The name "Terminal" was originally used for physical terminals, which were little more than a black and white screen with keyboard connected to a large shared Unix server computer out of sight, perhaps in the basement. However, today we will be running tools locally on the laptops.

It's time for a live demonstration - please start your command line "Terminal" running. For those of you on Windows, we have asked you to install Git Bash which provides a Linux like environment. This can be started from the "Start Menu" via "All Programs".

When you log onto a Linux server you'll generally get a welcome message setup by the server administrator, and a dollar sign prompt something like this:

Last login: Thu Feb 22 10:20:48 2018 from 143.234.99.94
[userXX@servername ~]$

You should be able to type and see the letters appear on the screen. Once you press the enter key, the text will be interpreted as a command and executed. e.g. Try typing d, a, t, e and enter - you should see something like this:

[userXX@servername ~]$ date
Tue Mar  6 09:41:27 GMT 2018

One of the powerful things about the command line is it can be used just as easily on your own machine as on a remote server. You may have access to Linux computer cluster where you work for example? Sending short commands and text output back and forth requires relatively little network bandwidth, so this can even works effectively for command line remote access to computers on the other side of the world.

Files and directories

One of the most important commands you will learn is ls, short for list, which by default lists all the files in the current working directory. Try this:

[userXX@servername ~]$ ls
Desktop           Pictures
Documents         Public
Downloads         2018-03-06-ibioic
examples.desktop  Templates
Music             Videos

Here 2018-03-06-ibioic represents a sub-directory underneath the current working directory. You can tell ls to list files in specific places, e.g.

[userXX@servername ~]$ ls 2018-03-06-ibioic/
00-platform                 VM_setup.md             linux_notes
01-introduction             _config.yml             requirements_students.txt
02-sequence_databases       copy_repo.sh            requirements_tutors.txt
03-lipases                  docs                    reveal.css
04-structure                environment.yml         schedule.Rmd
Dockerfile-notebooks        images                  schedule.html
Dockerfile-terminal         index.ipynb             three_minute_test.md
README.md                   install-apps.sh

Here 2018-03-06-ibioic/ is being interpreted as a relative path, starting from the current working directory. The command pwd reports the present working directory:

[userXX@servername ~]$ pwd
/home/userXX

i.e. Starting from /home/userXX, the relative path 2018-03-06-ibioic/ means the same as the absolute path /home/userXXX/2018-03-06-ibioic/.

Here /home/userXX represents the typical home directory naming used on Linux. On Apple macOS your home folder would be named something like /Users/userXX. Under Git Bash on Windows you'll probably see /c/Users/userXX or similar where /c represents the Windows C: drive.

You can change the directory with the cd command,

[userXX@servername ~]$ cd 2018-03-06-ibioic/
[userXX@servername 2018-03-06-ibioic/]$ pwd
/home/userXX/2018-03-06-ibioic/

Notice that the cd command did not print out any output (it just silently changed the directory). However, in this example the prompt did change. This example assume a common configuration showing the username, server name, and the final part of the current working directory.

Now if you run just ls, because the current directory has changed you get:

[userXX@servername 2018-03-06-ibioic]$ ls
00-platform                 VM_setup.md             linux_notes
01-introduction             _config.yml             requirements_students.txt
02-sequence_databases       copy_repo.sh            requirements_tutors.txt
03-lipases                  docs                    reveal.css
04-structure                environment.yml         schedule.Rmd
Dockerfile-notebooks        images                  schedule.html
Dockerfile-terminal         index.ipynb             three_minute_test.md
README.md                   install-apps.sh

And now, if you try ls 2018-03-06-ibioic/ you'll get an error:

[userXX@servername 2018-03-06-ibioic]$ ls 2018-03-06-ibioic/
ls: 2018-03-06-ibioic/: No such file or directory

You can go "up" one level in the directory try using the special relative path .., e.g. cd .. which is c, d, space, dot, dot (and then enter).

You can return to the home directory using just cd on its own.

For simplicity, in most examples the command prompt will be shown as just $. In the examples above it captured the username, computer name, and the final part of the current directory, or ~ which is a convention for the current directory.

Note that Microsoft Windows does include its own command line, both an older system known as DOS and a more recent one called PowerShell, but they work a little differently to the Unix conventions (for example using \ rather than / for directory names, and dir instead of ls to list the contents of a directory).