Linux Basic

Jean Monlong

29 March 2016

1 Introduction

1.1 What is Linux ?

  • Free operating system for computers, similar to Unix (proprietary).
  • Many bioinformatics programs run on Linux.
  • Leading operating system on supercomputers and servers.

1.2 Why is it good ?

  • Command-line instructions.
  • Work with big files easily.
  • Easier to develop and run specific tools.
  • Write custom analysis (scripting).

2 What is a command ?

2.1 The terminal

  • This is where you write and execute commands.
  • Open a Terminal.
  • A command looks like this: commandName arg1 arg2
  • Press Enter to run a command.

2.2 Example: Login through SSH

  • ssh stands for SecureSHell.
  • To connect securely to a server.
  • The address to connect to looks like : <username>@<server>
> ssh -p 22 jmonlong@www.genome.med.kyoto-u.ac.jp

Exercise: Connect to the server (using your username).

2.3 Try some commands

> ls
> echo Hello
> man echo
  • ls LiSt the files in the current directory.
  • man open the MANual of a command. Useful to find details on arguments. Quit by pressing q.

2.4 Tips

  • Press Tab to auto-complete commands or paths.
  • Up/Down arrows to navigate through previous commands.
  • clear or Ctrl-L clear the screen.

3 Files and folders

3.1 Folder structure

At any moment you are in a directory, the working directory.

3.2 Moving through folders

  • pwd to Print the Wordking Directory.
  • cd to Change Directory.
    • .. represents the previous directory.
    • / is the directory separator.
    • ~ represents your root folder.
> pwd
/home/jmonlong
> ls
workshop
> cd workshop
> pwd
/home/jmonlong/workshop
> cd ..
> cd workshop/linux

3.3 Exercise

  1. Print the working directory
  2. Create a directory workshop.
  3. Go to the workshop folder.
  4. Create a directory linux.
  5. Go back to your root folder.


Note: mkdir to MaKe a DIRectory.

3.4 Solution

> pwd
> mkdir workshop
> cd workshop
> mkdir linux
> cd ..

3.5 Manipulate files

  • rm ReMove a file.
  • cp CoPy a file.
  • mv MoVe a file.
> cp file1 file2
> rm file1
> mv file2 file3

3.6 Exercise

  1. Download the annotation file located at https://goo.gl/FLGAZH.
  2. Copy the file to the linux folder.
  3. Remove the file in the root folder.
  4. Go to the linux folder.
  5. Rename the file as gencode.gtf (using mv command)


Note: To download the file you can use:

> wget https://goo.gl/FLGAZH

3.7 Solution

> wget https://goo.gl/FLGAZH
> cp FLGAZH workshop/linux
> rm FLGAZH
> cd workshop/linux
> mv FLGAZH gencode.gtf

4 Text files

4.1 Commands

  • head/tail print the first/last lines of a file.
  • cat print the entire file.
  • less to explore the text file.
  • nano is a text editor. To open, change and save a file.
  • grep retrieves lines containing a word or expression.
  • wc retrieves the Word Count as well as the number of lines in a file.

4.2 Exercise

  1. Print the first lines of the annotation file.
  2. Explore the file using less. Try using -S argument.
  3. How many lines are in the file ?
  4. Find the lines containing ENSG00000278267.

4.3 Solution

> cd ~/workshop/linux
> head gencode.gtf
> less gencode.gtf
> less -S gencode.gtf
> wc gencode.gtf
> grep ENSG00000278267 gencode.gtf

5 Compression

5.1 Commands

  • gzip to compress a file.
  • gunzip to decompress a .gz file.
  • zless/zcat/zgrep are less/cat/grep directly on gzip files.
  • tar -xzvf file.tar.gz to decompress and extract a .tar.gz file.

5.2 Example

> gzip gencode.gtf
> zgrep ENSG00000278267 gencode.gtf.gz
> gunzip gencode.gtf.gz
> tar -xzvf archive.tar.gz

6 Pipes

6.1 Pipes

  • To "pipe" the output of a command into another command.
  • Using |.
  • More than two commands can be piped.
> zcat gencode.gtf.gz | grep ENSG00000278267
> zcat gencode.gtf.gz | grep ENSG00000278267 | head 
> zcat gencode.gtf.gz | grep ENSG00000278267 | less

6.2 Output redirection

  • Instead of printing the results of a command.
  • Use > to redirect it to a file.
> gunzip -c gencode.gtf.gz > gencode.gtf
> ls -lh
> zcat gencode.gtf.gz | grep ENSG00000278267 | head > gene1.gtf

7 Extra

7.1 scp command

  • Use SSH.
  • Copy a file from a server to your computer.
  • Run the command from your computer's terminal, NOT once connected to SSH.
  • The address looks like this:

<username>@<server>:<pathToFile>

scp jmonlong@www.genome.med.kyoto-u.ac.jp:workshop/linux/gene1.gtf .

7.2 * wildcard

  • * represent any possible character sequence.
  • Useful to manipulate several files with one command.
> ls
analysis.sh  graphs.R  results.csv  samp1.bam  samp1.pdf  temp1.log  temp1.txt  temp.txt.gz
> ls temp*
temp1.log  temp1.txt  temp.txt.gz
> rm temp*
> ls
analysis.sh  graphs.R  results.csv  samp1.bam  samp1.pdf

Caution: rm * removes every files.

7.3 Scripts

  • Write commands in a .sh file.
  • Run with sh.
> sh myScript.sh