A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual...

21
A primer on Linux Shell

Transcript of A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual...

Page 1: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

A primer on Linux Shell

Page 2: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Shells

● textual shells (CLI)○ unix shells, dos prompt

● graphic shells (GUI)○ windows explorer, mac

finder, linux desktop environments

Page 3: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

How files are organized on disk

/

dir1 dir2 dir3 dir4

dir1

fileA fileB fileB fileC

fileA fileB

Page 4: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Browsing the filesystem

● a shell always has a working directory

● you can see it with “pwd”● you can see the files in it with “ls”● you can change it with “cd”

● at the beginning it is your home

Page 5: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Special names for special directories

● the working (current) directory: “.”

● the parent of the current directory: “..”

● the root directory: “/”

● the home directory:“~”

Page 6: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Paths

● absolute path: starts from `/`● relative path: starts from the current

directory

Page 7: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Absolute paths

Always begin from the root (/)

/dir1/fileA

/dir2

/dir4/fileB

/dir2/dir1/fileA

/

dir1 dir2 dir3 dir4

dir1

fileA fileB fileB fileC

fileA fileB

Page 8: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Relative paths

$ pwd/dir2/dir1

● Need to use fileU?

../../dir4/fileU

● And fileE?../fileE

/

dir1 dir2 dir3 dir4

dir1

fileA fileB fileA fileU

fileA fileB

fileE

Page 9: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Other commands

● Copy a file:$ cp fileA copyOfFileA

● Rename a file:$ mv fileA fileB

● Create a directory:$ mkdir dir2

If you do not know how to use a command try:$ command -h

$ command --help

$ man command

● You can Google it● If everything fails

ask us!

Page 10: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Command’s Anatomy

● Some commands are fine alone:$ pwd

$ ls

● Many require one or more arguments:$ mkdir some_dir

$ mv a_file new_name

● Most commands can have options:$ ls -l -a

Page 11: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

live demo

Page 12: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Simple text files are your friends!

Simple text files contains text and nothing else.

● MP3 are binary files● DOC, XLS and JPG are

binary too!

● In Windows you can create text files with Notepad (not Word or Wordpad!)

Page 13: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Bioinformatics and Text files

● Most bioinformatics format are simple text files:○ Fasta○ FASTQ○ SAM○ VCF○ BED

>beta_lac_FORACGATCGTTACGTACTTGGGGGG>beta_lac_REVACGTACGTATTTTTACGATCGATC>ctrl_FORCAGCTATTATCGTATCGTACGACT>ctrl_REVCAGCAGCTACGTACGGTAGCATGC

primers.fa

Page 14: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

How to see the content of a file

● The command `head` prints the first lines

$ head primers.fa

● The command `tail` prints the last lines

$ tail primers.fa

● The command `cat` prints the content of a text file

$ cat primers.fa

● The command `wc` prints the number of lines

$ wc -l primers.fa

Page 15: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Redirects: do it there, not here!

● Many commands write text to the terminal:$ head fileA

● Maybe the output is:○ too long and/or ○ you want to save it on disk

$ head fileA > headOfFileA

● The reverse also works:$ head < inputFile > headOfInputFile

Page 16: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Pipes: call Mario!

You can also “pipe” the output of a command into another command● How many are the files in the current

directory?$ ls > listOfFiles

$ wc -l listOfFiles

● Why bother with a file? Use a pipe:$ ls | wc -l

You can really do a lot of stuff with these

Page 17: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Grep and family

● Often you want to see only some lines in a file:

$ grep password secretsOfPippo

password di facebook di Pippo: 123456

password del conto in banca: qwerty

● Can be very useful!

● Other useful command for text files:○ sort, tr, diff, cut, sed, awk, … (there are really many)

Page 18: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Don’t forget…

Page 19: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

live demo

Page 20: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

Why bother?

The textual shell has some disadvantages:● you need to know the commands (no hints)● little feedback on what happens● not very intuitive (or you are not used to it)

So why bother? Because the shell:● handles big data much better● GUIs take too long to write● makes you look cool/nerd/hacker

Page 21: A primer on Linux Shellm.docente.unife.it/silvia.fuselli/dispense-corsi/Shell.pdfShells textual shells (CLI) unix shells, dos prompt graphic shells (GUI) windows explorer, mac finder,

And now, to the computer lab!

● Each of you has an account (will be disclosed in the computer room)

● Each account has a personal home directory