Getting Started


In this section you will learn how to log in to a remote server and run some simple commands. You will also learn how to find out about commands, and how to use bash efficiently.

Starting a Terminal and logging in to the remote server


The process of starting a terminal depends on the operating system that you are using:

  • On Mac OS X or Ubuntu Linux, simply open the ‘Terminal’ application
  • On Windows, you can install MobaXTerm

You can log in to the server by typing the following command in the Terminal, substituting [USERNAME] for your account username:

ssh [USERNAME]@bifx-core1.bio.ed.ac.uk

If you are using MobaXTerm, an alternative way of logging into the server is shown in the MobaXTerm demo.

Once you have typed in your password, you should see some welcome text and a prompt that looks something like this:

[USERNAME]@bifx-core1:~$

This prompt shows that you are logged in to the bifx-core1 bioinformatics server, and that you are in your home directory (which is referred to using the ~ character on Linux systems).

Please see our website for more specific instructions on using the DRP-HCB bioinformatics servers.

Running basic commands in bash


Key Points

  • Commands in bash can be categorised as either:
    • built in commands (or builtins), which are part of the bash shell, or
    • installed programs, which could be either the basic GNU core utilities (or coreutils), included in most Linux distributions, or other programs, such as specialised bioinformatics tools


In order to run a command in bash you type the command and then press the return key to execute it. For example, try typing date at the prompt and pressing return. You should get something like this:

[USERNAME]@bifx-core1:~$ date
Wed 31 Jan 09:21:43 GMT 2024
[USERNAME]@bifx-core1:~$ 

As you can see, the output of the date command is printed on the command line, and then another prompt is shown.

As well as the command name, a command can include ‘options’ and ‘arguments’. For example, try typing date -d '25 Dec' +%j at the command line:

[USERNAME]@bifx-core1:~$ date -d '25 Dec' +%j
360
[USERNAME]@bifx-core1:~$ 

In this example, the command includes the following elements:

  • the command name date, which always comes first
  • one option -d '25 Dec', which has a name -d (option names always start with at least one ‘-’), and a value '25 Dec'
  • one argument +%j

Options and arguments

Options always have a name, but do not always need to have an associated value. Options that don’t have a value are called flags, and are often used as switches for different kinds of functionality. Unlike options, arguments don’t have names. Instead they are identified by their position.

In general, the values of options and arguments are text strings, which are passed directly to the command. If a value contains spaces, you can surround it with quote marks, as in the example above, so that it is recognised as a single value. You can also use the output of one command as an argument to another by enclosing it in backticks ``, as in the following example:

[USERNAME]@bifx-core1:~$ echo date
date
[USERNAME]@bifx-core1:~$ echo `date`
Sat 14 Nov 11:25:11 GMT 2020
[USERNAME]@bifx-core1:~$ 

The above example uses the echo command, which outputs the text value of its arguments. In the first instance it simply prints the word ‘date’. In the second instance, it runs the command within the backticks first, then prints the output of this command.

Finding out about bash commands


Key Points

  • The bash shell provides a number of different ways to access information about commands, in particular:
    • The man command
    • The info command
    • --help and -h flags


While the use of options and arguments makes commands more powerful and flexible, they can be difficult to remember, and can vary between different versions of the command. For this reason, it is useful to be able to find information about commands easily.

The man command

This displays a short manual page for a single command. To see the manual page for the date command simply type man date.

  • You can scroll through the man page using space, and type q to quit
    • You can learn more about different commands by typing h
  • To learn about bash builtins, type man builtins

The info command

This displays a more comprehensive hyperlinked manual, which provides detailed information about the GNU coreutils. Simply type info to see the info documentation, and type H in info to find out how to navigate the documentation. Press q to quit.

--help and -h flags

Some programs may not have any documentation that is accessible using the man or info commands. In this case, they may follow the convention of including --help or -h flags, which will show some usage information. As an example, we can try to find help information about bedtools, which is a well known bioinformatics tool that we will look at later in the course:

[USERNAME]@bifx-core1:~$ man bedtools
No manual entry for bedtools
[USERNAME]@bifx-core1:~$ info bedtools
info: No menu item 'bedtools' in node '(dir)Top'
[USERNAME]@bifx-core1:~$ bedtools --help
bedtools is a powerful toolset for genome arithmetic.

Version:   v2.30.0
...

Challenge:

We saw the command “date -d ‘25 Dec’ +%j” in the previous section. Can you use the man command to explain what it does?

Can you easily check how many days we have had this year?

Solution

Solution.

Solution:

Run man date, and look for the -d flag. The documentation explains that -d tells date to display the time described by the value of the option, rather than the current time. Now look up %j in the documentation. The relevant line says ‘%j day of year (001..366)’. So the command tells you which day of the year it will be on the 25th of December.

To check the day of the year for today just run:

date +%j

Using bash efficiently


Key Points

  • Working with long commands in bash can be laborious, but luckily there are a few tricks that you can use to make things easier. These are:
    • Keyboard shortcuts
    • Tab completion
    • Using the bash history

Keyboard shortcuts

By default, bash allows you to use many keyboard shortcuts from the GNU Emacs editor to work with your commands. These include:

  • Ctrl+a to move to the beginning of the line
  • Ctrl+e to move to the end of the line
  • Ctrl+k to cut the text from the cursor to the end of the line
  • Ctrl+y to paste text at the cursor
  • Ctrl+l to clear the terminal

You can also use Ctrl+c to terminate the command that is currently running and take you back to the command prompt.

Tab completion

Tab completion allows you to type part of a command name followed by the Tab key, which prompts bash to guess the rest of the command name based on the commands that are available on the system.

For instance, if you want to run the bedtools command on the server, you can type bedtoo followed by Tab, and the rest of the command name will be filled in for you.

Typing Tab once to complete a command name only works if there is only one possible way to complete the name. If there are multiple options and you would like to see them, you can type Tab twice, and the possible completions will be displayed.

Using the bash history

A particularly useful feature of command line shells is the ability to look back at previous commands that you have run. In bash you can access these commands using the history command. Typing history will show you a numbered list of commands you’ve run recently.

There are a few useful tricks that make it easy to find and run commands that you have run before:

  • Scrolling through the history with the up and down arrows
  • Searching the history with Ctrl+r (reverse-i-search)
    • Enter text and use Ctrl+r again to cycle through results
  • Re-running a previously run command with ! followed by the number of the command

Challenge:

What are the possible completions of ‘bed’ on the server?

Solution

Solution.

Solution:

At the command prompt, type bed then press Tab twice. You should see many possible options.


Challenge:

Use the history to re-run the date -d '25 Dec' +%j command without re-typing it

Solution

Solution.

Solution:

There are three ways to do this: One way would be to press Ctrl+r, then type date, then press Ctrl+r again repeatedly until you see the command, then press Enter to run it.

Alternatively, you could run the history command and look back through the history list to find the command. Then you could type !at the command prompt followed by the number of the command in the history.

A third way would be to either hit the up arrow to scroll back through the history until you see the command, then press Enter to run it.


Resources

Although Unix has thousands of commands, there are a few that are used most of the time. Even so, it can be daunting to try to memorise new Linux commands. The cheat sheet below is a really handy resource to get you started on the command line.