User Tools

Site Tools


mkatari-bioinformatics-august-2013-introlinuxnotes

Back to Manny's Bioinformatics Workshop Home

What is Linux ?

It is a free and open source operating system released in 1991 under the GNU GPL license.GPL allows anyone to use, modify and redistribute with the requirement that they pass it on with the same license.

It is the leading operating system of choice for servers such as supercomputers. More than 90% of the top 500 fastest computers are based on Linux.

MAC computers are related to Linux because they are also based on UNIX

Depending on the purpose of the Linux machine, it may or may not have a Desktop environment that we are familiar with on our personal computers. Linux uses X Window System to provide the Desktop environment.

A popular distribution of Linux operating system is called Ubuntu.

REF: http://en.wikipedia.org/wiki/Linux

Why do bioinformaticians use Linux?

  • Many bioinformatics core tools are written in Linux.
    • BLAST, CLUSTALW, PHRAP, etc
    • Many web applications are also supported on web servers hosted on linux machines
  • Linux supports development of software for many different programming languages.
    • Developers are lazy so creating a software that does not require a window is much faster and easier
  • Multiple users can log in at the same time.
    • A user logging in over the network can do just about anything a user sitting in front of the computer can do. Which also means linux handles multitasking very well.

Remote vs. Local

Logging in with X Windows

The standard user interface for personal computers is a GUI (Graphical User Interface). However for linux it is a command-line interpreter called shell. It is simply a prompt the awaits your command. There are several different shells, but the one used often is called “bash”, which is a mixture of a bunch of other shells.

In cases where a program requires a GUI, you should log in using the –X option. This opens a tunnel to your computer allowing all windows to open in your computer. For this to work you need X11 installed on your computer (MobaXterm already has one) MAC – Xquartz (http://xquartz.macosforge.org/landing/) Windows – Xming (http://sourceforge.net/projects/xming/)

Last login: Wed Jun  3 15:49:01 on ttys000
Manpreets-MacBook-Pro:~ manpreetkatari$ ssh -X mkatari@hpc.ilri.cgiar.org
Unauthorized access is prohibited.
mkatari@hpc.ilri.cgiar.org's password:
Last login: Wed Jun  3 16:33:26 2015 from 197.136.62.11
[mkatari@hpc ~]$ emacs

You should have a window popup on your computer that looks something like this.

Simply close the window to exit.

Home Sweet Home

When you first log in, you will be in a directory called “home directory

/home/<your username>

Generally in this directory you have complete control over creating, modifying, and executing files in this or any sub directory you create. In order to return to your home directory simply type the command: cd ~ at the prompt. Unless appropriate changes have been made you can can not enter anyone’s directory or even see what is in it.

Command Line Editing

The command is only executed once you press enter. Till then you can edit the line by using the following key strokes:

Action Result
Backspace (delete on MACs) delete previous character
Left Arrow, Right Arrow move left and right on lines
Up Arrow, down Arrow previous and following command
Ctrl-A go to front of line
Ctrl-E go to end of line
Ctrl-D delete next character
Ctrl-K delete everything to the right of the character
Ctrl-Y paste
Ctrl-C stop a running job

Once you press enter the program will be executed. When your prompt returns, you know that the program has finished. If there is an output to the program it usually prints it on the screen (often referred to as the standard output)

In the example below, date is a command that is being executed with no arguments. Many commands/programs have options that are provided immediately following the command. In the ls -l example, ls is the command and everything else are options that are provided.

[mkatari@hpc ~]$ date
Wed Jun  3 21:10:57 EAT 2015
[mkatari@hpc ~]$ ls -l
total 19443152
-rw-rw-r--. 1 mkatari mkatari      16263 Jun  3 16:29 03-06-2015.pdf
-rw-rw-r--. 1 mkatari mkatari     990646 Jun 12  2014 _1.fastq
-rw-rw-r--. 1 mkatari mkatari     381856 Jun 12  2014 _2.fastq

Directing standard output

Instead letting the output print to the screen we can save it to a file by using the > sign and then giving the file name. This will replace a file if it already exists without a warning. To append use an existing file use ». It is important to mention here that once you overwrite a file, it is deleted. It is gone. There is no recycling bin to restore from trash.

The following command gets details about all users' home directories and saves them into a file called allusers.txt

[mkatari@hpc ~]$ ls -l /home/ > allusers.txt
[mkatari@hpc ~]$ ls -l allusers.txt
-rw-rw-r--. 1 mkatari mkatari 18897 Jun  3 21:26 allusers.txt

Command-line completion

In some cases the commands or the file names that you need as arguments can be very long which increases the chance of spelling mistakes.

To prevent such mistakes simply type the enough letters to unambiguously identify the command or file and then pressing tab will complete it for you.

In the case you don’t know how many letters you need, simply press tab twice to see all your options.

In the example below, after typing the command and its options, the tab key was pressed twice to get this. The command will not be executed until the enter key is pressed.

[mkatari@hpc ~]$ ls /usr/bin/bz
bzcat         bzdiff        bzip2         bzless
bzcmp         bzgrep        bzip2recover  bzmore

Wildcards

In cases where you want to refer to multiple files you can use * to represent any characters of any length. You can also use ? To represent any character of one length.

In the example below, the first line gives all files/programs that start with bz. The second only gives which begin with bz and three letters afterwards, represented by ?

[mkatari@hpc ~]$ ls /usr/bin/bz*
/usr/bin/bzcat  /usr/bin/bzdiff  /usr/bin/bzip2         /usr/bin/bzless
/usr/bin/bzcmp  /usr/bin/bzgrep  /usr/bin/bzip2recover  /usr/bin/bzmore
[mkatari@hpc ~]$ ls /usr/bin/bz???
/usr/bin/bzcat  /usr/bin/bzcmp  /usr/bin/bzip2

Finding Your Way

Often you will get lost on the hpc and you will need to know where you are, which computer did you log into, or even which account have you logged into. Below are some simple commands that help you find your way.

[mkatari@hpc ~]$ whoami
mkatari

[mkatari@hpc ~]$ pwd
/home/mkatari

[mkatari@hpc ~]$ hostname
hpc.ilri.cgiar.org

File manipulation

Useful commands for manipulating files and directories. To get details about how to use the commands type man <command>.

Command Action
mkdir make a directory
rmdir remove a directory (only works if the directory is empty )
cd change directory
pwd present working directory
ls list of files and directories in the directory. You can use wild card to look for specific files. You can also use -l to see details such as permission for files and directories
cp copy a file and/or directories. Use -r to recursively copy.
mv move a file. It will copy and then delete the source. This can be used to rename files as well.
rm remove a file
[mkatari@hpc ~]$ mkdir temp
[mkatari@hpc ~]$ cd temp/
[mkatari@hpc temp]$ ls
[mkatari@hpc temp]$ cp ../allusers.txt ./
[mkatari@hpc temp]$ ls
allusers.txt
[mkatari@hpc temp]$ mv allusers.txt allusers.backup
[mkatari@hpc temp]$ ls
allusers.backup
[mkatari@hpc temp]$ rm allusers.backup
[mkatari@hpc temp]$ ls
[mkatari@hpc temp]$ cd ../
[mkatari@hpc ~]$ rmdir temp/

Permissions

There are three levels of permissions that can be assigned to all files, programs, and directories

  • Read: open the file and copy it
  • Write: edit the file and delete it
  • Execute: Run the commands in the file or change into the directory if it is a directory

There are also three different levels of users:

  • User – you
  • Group – A collection of users that are in a group
  • Everyone - Not just the people who have accounts on the machine but if the directory is open to the public and any one.

The commands used to change owner, group, and specific permissions are:

  • chown – changes the owner
  • chgrp – changes the group
  • chmod – change read, write, and execute permissions
    • +/- r = read
    • +/- w = write
    • +/- x = execute
    • u = user level
    • g = group level
    • o = others
    • a = all
  • chmod can also use three numbers to set permissions where the value of the number represents a specific combination of rwx and their order assigns it to the different levels (u,g,o)
    • 0 = none
    • 1 = execute only
    • 2 = write only
    • 3 = write and execute only
    • 4 = read only
    • 5 = read and execute only
    • 6 = read and write only
    • 7 = read, write and execute
[mkatari@hpc ~]$ tail allusers.txt > bottomusers.txt
[mkatari@hpc ~]$ ls -al bottomusers.txt
-rw-rw-r--. 1 mkatari mkatari 772 Jun 11 13:54 bottomusers.txt

[mkatari@hpc ~]$ chmod u=+r-wx,g=+r-wx,o=-rwx bottomusers.txt
[mkatari@hpc ~]$ ls -al bottomusers.txt
-r--r-----. 1 mkatari mkatari 772 Jun 11 13:54 bottomusers.txt

[mkatari@hpc ~]$ rm bottomusers.txt
rm: remove write-protected regular file `bottomusers.txt'? y
[mkatari@hpc ~]$ ls -al bottomusers.txt
ls: cannot access bottomusers.txt: No such file or directory

Notice that since we have taken away our own write permission to the file we can not automatically delete it. The shell is asking to change the permission so it can delete. If we have write permissions that it would have delete right away.

History

Your shell saves all your commands and you can access them using the up and down keys.

Typing the command “history” returns all the commands you have entered and a number assigned to it.

You can run a specific one again by typing ! (also called “bang”) Followed by the job number.

!! Will perform the most recent command.

In the example code below, tail is used to get the last lines of the file allusers.txt. The > sign redirects the output to a new file called bottomusers.txt.

[mkatari@hpc ~]$ history | grep bottom
 1000  tail allusers.txt > bottomusers.txt
 1001  ls -al bottomusers.txt
 1002  chmod u=+r-wx,g=+r-wx,o=-rwx bottomusers.txt
 1003  ls -al bottomusers.txt
 1004  rm bottomusers.txt
 1005  ls -al bottomusers.txt
 1006  rm bottomusers.txt
 1007  ls -al bottomusers.txt
 1008  history | grep bottom
[mkatari@hpc ~]$ !1000
tail allusers.txt > bottomusers.txt
[mkatari@hpc ~]$ ls -al bottomusers.txt
-rw-rw-r--. 1 mkatari mkatari 772 Jun 11 14:01 bottomusers.txt
[mkatari@hpc ~]$ !1002
chmod u=+r-wx,g=+r-wx,o=-rwx bottomusers.txt
[mkatari@hpc ~]$ ls -al bottomusers.txt
-r--r-----. 1 mkatari mkatari 772 Jun 11 14:01 bottomusers.txt

Transferring Files

There are several ways to transfer files to a server. The most reliable and consistent way is to use scp. scp is a combination of the cp copy command and ssh command for connecting securely. An important thing to note is that you can only use scp if the remote server accepts ssh connections. For example, if you are using a windows machine and have not setup a service to accept ssh connections, you can copy to your windows machines, but you can copy from your windows machine.

One major advantage of using mobaxterm on windows machines is that it comes with some basic linux commands, including scp. On a mac, scp comes with the operating system. So if you want to copy a file that is on your Desktop to hpc from your laptop you can use the following command:

[2015-06-11 07:22.19]  ~
[Manpreet.WIN-OAOVO3NM02E] ➤ cp allusers.txt Desktop/allusers_local.txt
                                                                           ✔
────────────────────────────────────────────────────────────────────────────
[2015-06-11 07:22.37]  ~
[Manpreet.WIN-OAOVO3NM02E] ➤ scp Desktop/allusers_local.txt mkatari@hpc.ilri.cgiar.org:
Unauthorized access is prohibited.
allusers_local.txt                        100%   18KB  18.2KB/s   00:00

the : at the end is very important because it tells the shell that is a server and not a file name. The : by itself puts the file in your home directory, but you can specify a specific path if you wanted to.

Now to copy the file back to my computer I will use the following command. Note that this time I am also renaming the file on my computer.

[Manpreet.WIN-OAOVO3NM02E] ➤ scp mkatari@hpc.ilri.cgiar.org:allusers_local.txt ./return_allusers.txt
Unauthorized access is prohibited.
allusers_local.txt                        100%   18KB  18.2KB/s   00:00
                 

Controlling Jobs

The following commands and keyboard short-cuts can come in handing when you need to cancel, suspend, or start a job.

CommandAction
ctrl-CTerminate current running job
ctrl-ZSuspend Jobs
bg Once a job has been suspended bg can be put in the background
fg In order to put a background job in the foreground type fg
& When executing a command and you want to put it in the background immediately put the & symbol at the end of the command.
jobs This gives a list of jobs (suspended, running, and terminating)
top

Commands for manipulating and querying files

Some more cool commands

CommandAction
less/more read through the file without loading the entire file. Press spacebar to continue or q to quit.
touch create an empty file
head show the first few lines of the file
tail show the last few lines of the file
cat read through the file(s)
grep search for patterns in a file or files
cut separate file based on columns
comm/diff compare and see the difference between the files. The files have to be sorted before using either of these commands.
split splits file into smaller files based on the options
sort sort the file base on the options selected.
wc wordcount

Exercise

  1. Transfer it to your LinuxJune2015 directory on HPC.
  2. Gunzip the file and rename it to : Cassavagenes.txt
  3. Determine the number of different genes are present in the file. Hint – check the third column
  4. Save the rows that contain the word “gene” in the third column as a separate file. Call it “GeneTypes.txt”
  5. Create a directory called “Share”
  6. Move GeneTypes.txt into Share and change permissions on Share so others can read the file.

Some useful information about linux

Environment variables and PATH

All variables that are set in your environment can be found by using

env

The variable that is most important to us is PATH. The PATH is where the computer is looking for the commands. To see the contents of the variable type:

echo $PATH

In the sbatch files we have been adding the full path to the commands. Another option is to add the full path of the command you want to use to the variable PATH. This is what module load essentially does.

For example the following two commands are equivalent.

export PATH=/export/apps/samtools/0.1.19/bin:$PATH

module load samtools
mkatari-bioinformatics-august-2013-introlinuxnotes.txt · Last modified: 2015/06/11 14:50 by mkatari