Important:¶
This material is no longer maintained and may be out of date.¶
Please go to this link below for a current Introduction to Unix¶
Introduction to Unix¶
A hands-on workshop covering the basics of the Unix/Linux command line interface.
Overview¶
Knowledge of the Unix operating system is fundamental to being productive on HPC systems. This workshop will introduce you to the fundamental Unix concepts by way of a series of hands-on exercises.
The workshop is facilitated by experienced Unix users who will be able to guide you through the exercises and offer assistance where needed.
Learning Objectives¶
At the end of the course, you will be able to:
- Log into a Unix machine remotely
- Organise your files into directories
- Change file permissions to improve security and safety
- Create and edit files with a text editor
- Copy files between directories
- Use command line programs to manipulate files
- Automate your workflow using shell scripts
Requirements¶
- The workshop is intended for beginners with no prior experience in Unix.
- Attendees are required to bring their own laptop computers.
Introduction¶
Before we commence the hands-on part of this workshop we will first give a short 30 minute talk to introduce the Unix concepts. The slides are available if you would like. Additionally the following reference material is available for later use.
Reference Material
Topic 1: Remote log in¶
In this topic we will learn how to connect to a Unix computer via a program called ssh and run a few basic commands.
Connecting to a Unix computer¶
To begin this workshop you will need to connect to an HPC. Today we will use barcoo. The computer called barcoo.vlsci.org.au is the one that coordinates all the HPC’s tasks.
Server details:
- host: barcoo.vlsci.org.au
- port: 22
- username: (provided at workshop)
- password: (provided at workshop)
Mac OS X / Linux
Both Mac OS X and Linux come with a version of ssh (called OpenSSH) that can be used from the command line. To use OpenSSH you must first start a terminal program on your computer. On OS X the standard terminal is called Terminal, and it is installed by default. On Linux there are many popular terminal programs including: xterm, gnome-terminal, konsole (if you aren't sure, then xterm is a good default). When you've started the terminal you should see a command prompt. To log into *barcoo*, for example, type this command at the prompt and press return (where the word *username* is replaced with your *barcoo* username): *$ ssh username@barcoo.vlsci.org.au* The same procedure works for any other machine where you have an account except that if your Unix computer uses a port other than 22 you will need to specify the port by adding the option *-p PORT* with PORT substituted with the port number. You may be presented with a message along the lines of: Although you should never ignore a warning, this particular one is nothing to be concerned about; type **yes** and then **press enter**. If all goes well you will be asked to enter your password. Assuming you type the correct username and password the system should then display a welcome message, and then present you with a Unix prompt. If you get this far then you are ready to start entering Unix commands and thus begin using the remote computer.Windows
On Microsoft Windows (Vista, 7, 8) we recommend that you use the PuTTY ssh client. PuTTY (putty.exe) can be downloaded from this web page: [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) Documentation for using PuTTY is here: [http://www.chiark.greenend.org.uk/~sgtatham/putty/docs.html](http://www.chiark.greenend.org.uk/~sgtatham/putty/docs.html) When you start PuTTY you should see a window which looks something like this: To connect to *barcoo* you should enter *barcoo.vlsci.org.au* into the box entitled "Host Name (or IP address)" and *22* in the port, then click on the Open button. All of the settings should remain the same as they were when PuTTY started (which should be the same as they are in the picture above). In some circumstances you will be presented with a window entitled PuTTY Security Alert. It will say something along the lines of *"The server's host key is not cached in the registry"*. This is nothing to worry about, and you should agree to continue (by clicking on Yes). You usually see this message the first time you try to connect to a particular remote computer. If all goes well, a terminal window will open, showing a prompt with the text *"login as:"*. An example terminal window is shown below. You should type your *barcoo* username and press enter. After entering your username you will be prompted for your password. Assuming you type the correct username and password the system should then display a welcome message, and then present you with a Unix prompt. If you get this far then you are ready to start entering Unix commands and thus begin using the remote computer.Note: for security reasons ssh will not display any characters when you enter your password. This can be confusing because it appears as if your typing is not recognised by the computer. Don’t be alarmed; type your password in and press return at the end.
barcoo is a high performance computer for Melbourne Bioinformatics users. Logging in connects your local computer (e.g. laptop) to barcoo, and allows you to type commands into the Unix prompt which are run on the HPC, and have the results displayed on your local screen.
You will be allocated a training account on barcoo for the duration of the workshop. Your username and password will be supplied at the start of the workshop.
Log out of barcoo, and log back in again (to make sure you can repeat the process).
All the remaining parts assume that you are logged into barcoo over ssh.
Exercises¶
1.1) When you’ve logged into the Unix server, run the following commands and see what they do:¶
- who
- whoami
- date
- cal
- hostname
- /vlsci/TRAINING/shared/Intro_to_Unix/hi
Answer
* **who**: displays a list of the users who are currently using this Unix computer. * **whoami**: displays your username (i.e. they person currently logged in). * **date**: displays the current date and time. * **cal**: displays a calendar on the terminal. It can be configured to display more than just the current month. * **hostname**: displays the name of the computer we are logged in to. * **/vlsci/TRAINING/shared/Intro_to_Unix/hi**: displays the text "Hello World"Topic 2: Exploring your home directory¶
In this topic we will learn how to “look” at the filesystem and further expand our repertoire of Unix commands.
Duration: 20 minutes.
Relevant commands: ls, pwd, echo, man
Your home directory contains your own private working space. Your current working directory is automatically set to your home directory when you log into a Unix computer.
2.1) Use the ls command to list the files in your home directory. How many files are there?¶
Hint
Literally, type *ls* and press the *ENTER* key.Answer
When running the *ls* command with no options it will list files in your current working directory. The place where you start when you first login is your *HOME* directory. **Answer**: 3 (exp01, file01 and muscle.fq)The above answer is not quite correct. There are a number of hidden files in your home directory as well.
2.2) What flag might you use to display all files with the ls command? How many files are really there?¶
Hint
Take the *all* quite literally.Additional Hint
Type *ls --all* and press the *ENTER* key.Answer
**Answer 1**: *--all* (or *-a*) flag Now you should see several files in your home directory whose names all begin with a dot. All these files are created automatically for your user account. They are mostly configuration options for various programs including the shell. It is safe to ignore them for the moment. There are two trick files here; namely *.* and *..* which are not real files but instead, shortcuts. *.* is a shortcut for the current directory and *..* a shortcut for the directory above the current one. **Answer 2**: 10 files (don't count *.* and *..*)2.3) What is the full path name of your home directory?¶
Hint
Remember your *Current Working Directory* starts in your *home* directory.Additional Hint
Try a shortened version of *print working directory*Answer
You can find out the full path name of the current working directory with the *pwd* command. Your home directory will look something like this: **Answer**: */vlsci/TRAINING/trainXX* where *XX* is replaced by some 2 digit sequence. **Alternate method**: You can also find out the name of your home directory by printing the value of the *$HOME* shell variable:2.4) Run ls using the long flag (-l), how did the output change?¶
Hint
Run *ls -l*Answer
**Answer**: it changed the output to place 1 file/directory per line. It also added some extra information about each. **Details**: Where: * **permissions**: 4 parts, file type, user perms, group perms and other perms * *filetype*: 1 character, *d* = directory and *-* regular file * *user* permissions: 3 characters, *r* = read, *w* = write, *x* = execute and *-* no permission * *group* permissions: same as user except for users within the owner group * *other* permissions: same as user except for users that are not in either user *or* *group* * **username**: the user who *owns* this file/directory * **group**: the group name who *owns* this file/directory * **size**: the number of bytes this file/directory takes to store on disk * **date**: the date and time when this file/directory was *last edited* * **name**: name of the file * **linkcount**: technical detail which represents the number of links this file has in the file system (safe to ignore)2.5) What type of file is exp01 and muscle.fq?¶
Hint
Check the output from the *ls -l*.Answer
**Answer**: * *exp01*: Directory (given the 'd' as the first letter of its permissions) * *muscle.fq*: Regular File (given the '-')2.6) Who has permission to read, write and execute your home directory?¶
Hint
You can also give *ls* a filename as the first option.Additional Hint
*ls -l* will show you the contents of the *CWD*; how might you see the contents of the *parent* directory? (remember the slides)Answer
If you pass the *-l* flag to ls it will display a "long" listing of file information including file permissions. There are various ways you could find out the permissions on your home directory. **Method 1**: given we know the *CWD* is our home directory. The *..* refers to the parent directory. **Method 2**: using $HOME. This works no matter what our *CWD* is set to. You could list the permissions of all files and directories in the parent directory of your home: In this case we use the shell variable to refer to our home directory. **Method 3**: using *~* (tilde) shortcut You may also refer to your home directory using the *~* (tilde) character: All 3 of the methods above mean the same thing. You will see a list of files and directories in the parent directory of your home directory. One of them will be the name of your home directory, something like *trainXX*. Where *XX* is replaced by a two digit string. **Altername**: using the *-a* flag and looking at the *.* (dot) special file. **Answer**: *drwxr-x---* * **You**: read (see filenames), write (add, delete files), execute (change your CWD to this directory). * **Training users**: read, execute * **Everyone else**: No access **Discussion on Permissions**: The permission string is *"drwxr-x---"*. The *d* means it is a directory. The *rwx* means that the owner of the directory (your user account) can *read*, *write* and *execute* the directory. Execute permissions on a directory means that you can *cd* into the directory. The *r-x* means that anyone in the same user group as *training* can read or execute the directory. The *---* means that nobody else (other users on the system) can do anything with the directory.man is for manual: and it will be your best friend!
Manual pages include a lot of detail about a command and its available flags/options. It should be your first (or second) port of call when you are trying to work out what a command or option does.
You can scroll up and down in the man page using the arrow keys.
You can search in the man page using the forward slash followed by the search text followed by the ENTER key. e.g. type /hello and press ENTER to search for the word hello. Press n key to find next occurance of hello etc.
You can quit the man page by pressing q.
2.7) Use the man command to find out what the -h flag does for ls¶
Hint
Give *ls* as an option to *man* command.Additional Hint
*man ls*Answer
Use the following command to view the *man* page for *ls*: **Answer**: You should discover that the *-h* option prints file sizes in human readable format2.8) Use the -h, how did the output change of muscle.fq?¶
Hint
Don't forget the *-l* option too.Additional Hint
Run *ls -lh*Answer
**Answer**: it changed the output so the *filesize* of *muscle.fq* is now *2.5K* instead of *2461*Topic 3: Exploring the file system¶
In this topic we will learn how to move around the filesystem and see what is there.
Duration: 30 minutes.
Relevant commands: pwd, cd, ls, file
3.1) Print the value of your current working directory.¶
3.2) List the contents of the root directory, called ‘/’ (forward slash).¶
Hint
*ls* expects one or more anonymous options which are the files/directories to list.Answer
Here we see that *ls* can take a filepath as its argument, which allows you to list the contents of directories other than your current working directory.3.3) Use the cd command to change your working directory to the root directory. Did your prompt change?¶
Hint
*cd* expects a single option which is the directory to change toAnswer
The *cd* command changes the value of your current working directory. To change to the root directory use the following command: **Answer**: Yes, it now says the CWD is */* instead of *~*. Some people imagine that changing the working directory is akin to moving your focus within the file system. So people often say "move to", "go to" or "charge directory to" when they want to change the working directory. The root directory is special in Unix. It is the topmost directory in the whole file system.3.4) List the contents of the CWD and verify it matches the list in 3.2¶
Hint
*ls*Answer
Assuming you have changed to the root directory then this can be achieved with *ls*, or *ls -a* (for all files) or *ls -la* for a long listing of all files. If you are not currently in the root directory then you can list its contents by passing it as an argument to ls: **Answer**: Yes, we got the same output as exercise 3.23.5) Change your current working directory back to your home directory. What is the simplest Unix command that will get you back to your home directory from anywhere else in the file system?¶
Hint
The answer to exercise 2.6 might give some hints on how to get back to the home directoryAdditional Hint
*$HOME*, *~*, */vlsci/TRAINING/trainXX* are all methods to name your home directory. Yet there is a simpler method; the answer is buried in *man cd* however *cd* doesn't have its own manpage so you will need to search for it.Answer
Use the *cd* command to change your working directory to your home directory. There are a number of ways to refer to your home directory: is equivalent to: The simplest way to change your current working directory to your home directory is to run the *cd* command with no arguments: **Answer**: the simplest for is cd with NO options. This is a special-case behaviour which is built into *cd* for convenience.3.6) Change your working directory to the following directory:¶
/vlsci/TRAINING/shared/Intro_to_Unix
Answer
**Answer**: *cd /vlsci/TRAINING/shared/Intro_to_Unix*3.7) List the contents of that directory. How many files does it contain?¶
Hint
*ls*Answer
You can do this with *ls* **Answer**: 7 files (expectations.txt hello.c hi jude.txt moby.txt sample_1.fastq sleepy)3.8) What kind of file is /vlsci/TRAINING/shared/Intro_to_Unix/sleepy?¶
Hint
Take the word *file* quite literally.Additional Hint
*file sleepy*Answer
Use the *file* command to get extra information about the contents of a file: Assuming your current working directory is */vlsci/TRAINING/shared/Intro_to_Unix* Otherwise specify the full path of sleepy: **Answer**: Bourne-Again shell script text executable The "Bourne-Again shell" is more commonly known as BASH. The *file* command is telling us that sleepy is (probably) a shell script written in the language of BASH. The file command uses various heuristics to guess the "type" of a file. If you want to know how it works then read the Unix manual page like so:3.9) What kind of file is /vlsci/TRAINING/shared/Intro_to_Unix/hi?
Hint
Take the word *file* quite literally.Answer
Use the file command again. If you are in the same directory as *hi* then: **Answer**: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux This rather complicated output is roughly saying that the file called *hi* contains a binary executable program (raw instructions that the computer can execute directly).3.10) What are the file permissions of the following file and what do they mean?¶
/vlsci/TRAINING/shared/Intro_to_Unix/sleepy
Hint
Remember the *ls* command, and don't forget the *-l* flagAnswer
You can find the permissions of *sleepy* using the *ls* command with the *-l* flag. If you are in the same directory as *sleepy* then: **Answer**: The Answer is dependent on the computer you are connected too however will follow something like above. We can see that this particular instance of sleepy is owned by the user arobinson, and is part of the common user group. It is 183 bytes in size, and was last modified on the 9th of February at 4:36pm. The file is readable to everyone, and write-able only to arobinson. The digit '1' between the file permission string and the owner indicates that there is one link to the file. The Unix file system allows files to be referred to by multiple "links". When you create a file it is referred to by one link, but you may add others later. For future reference: links are created with the *ln* command.3.11) Change your working directory back to your home directory ready for the next topic.¶
Hint
*cd*Topic 4: Working with files and directories¶
In this topic we will start to read, create, edit and delete files and directories.
Duration: 50 minutes.
Relevant commands: mkdir, cp, ls, diff, wc, nano, mv, rm, rmdir, head, tail, grep, gzip, gunzip
4.1) In your home directory make a sub-directory called test.¶
Hint
You are trying to *make a directory*, which of the above commands looks like a shortened version of this?Additional Hint
*mkdir*Answer
Make sure you are in your home directory first. If not *cd* to your home directory. Use the *mkdir* command to make new directories: Use the *ls* command to check that the new directory was created.4.2) Copy all the files from the following directory into the newly created test directory:¶
/vlsci/TRAINING/shared/Intro_to_Unix
Hint
You are trying to *copy*, which of the above commands looks like a shortened version of this?Additional Hint
which means *cp* expects zero or more flags, a SOURCE file followed by a DEST file or directoryAnswer
Use the *cp* command to copy files.Note: This exercise assumes that the copy command from the previous exercise was successful.
4.3) Check that the file size of expectations.txt is the same in both the directory that you copied it from and the directory that you copied it to.¶
Hint
Remember *ls* can show you the file size (with one of its flags)Additional Hint
*ls -l*Answer
Use *ls -l* to check the size of files. You could do this in many ways depending on the value of your working directory. We just show one possible way for each file: From the output of the above commands you should be able to see the size of each file and check that they are the same. **Answer**: They should each be *1033773* bytes **Alternate**: Sometimes it is useful to get file sizes reported in more "human friendly" units than bytes. If this is true then try the *-h* option for ls: In this case the size is reported in kilobytes as *1010K*. Larger files are reported in megabytes, gigabytes etcetera.Note: this exercise assumes your working directory is ~/test; if not run cd ~/test
4.4) Check that the contents of expectations.txt are the same in both the directory that you copied it from and the directory that you copied it to.¶
Hint
What is the opposite of *same*?Additional Hint
*diff*erenceAnswer
Use the *diff* command to compare the contents of two files. If the two files are identical the *diff* command will NOT produce any output) **Answer**: Yes, they are the same since no output was given.4.5) How many lines, words and characters are in expectations.txt?¶
Hint
Initialisms are keyAdditional Hint
*w*ord *c*ountAnswer
Use the *wc* (for "word count") to count the number of characters, lines and words in a file: **Answer**: There are *20415* lines, *187465* words and *1033773* characters in expectations.txt. To get just the line, word or character count:4.6) Open ~/test/expectations.txt in the nano text editor, delete the first line of text, and save your changes to the file. Exit nano.¶
Hint
*nano FILENAME* Once *nano* is open it displays some command hints along the bottom of the screen.Additional Hint
*^O* means hold the *Control* (or CTRL) key while pressing the *o*. Despite what it displays, you need to type the lower-case letter that follows the *^* character. WriteOut is another name for Save.Answer
Take some time to play around with the *nano* text editor. *Nano* is a very simple text editor which is easy to use but limited in features. More powerful editors exist such as *vim* and *emacs*, however they take a substantial amount of time to learn.4.7) Did the changes you made to ~/test/expectations.txt have any effect on /vlsci/TRAINING/shared/Intro_to_Unix?¶
How can you tell if two files are the same or different in their contents?
Hint
Remember exercise 4.4Additional Hint
Use *diff*Answer
Use *diff* to check that the two files are different after you have made the change to the copy of *expectations.txt* in your *~/test* directory. You could also use *ls* to check that the files have different sizes.4.8) In your test subdirectory, rename expectations.txt to foo.txt.¶
Hint
Another way to think of it is *moving* it from *expectations.txt* to *foo.txt*Additional Hint
*mv* Use *man mv* if you need to work out how to use it.Answer
Use the *mv* command to rename the file:4.9) Rename foo.txt back to expectations.txt.¶
Answer
Use the *mv* command to rename the file: Use *ls* to check that the file is in fact renamed.4.10) Remove the file expectations.txt from your test directory.¶
Hint
We are trying to *remove* a file, check the commands at the top of this topic.Additional Hint
*rm*Answer
Use the *rm* command to remove files (carefully):4.11) Remove the entire test directory and all the files within it.¶
Hint
We are trying to *remove a directory*.Additional Hint
You could use *rmdir* but there is an easier way using just *rm* and a flag.Answer
You could use the *rm* command to remove each file individually, and then use the *rmdir* command to remove the directory. Note that *rmdir* will only remove directories that are empty (i.e. do not contain files or subdirectories). A faster way is to pass the *-r* (for recursive) flag to *rm* to remove all the files and the directory in one go: **Logical Answer**: **Easier Answer**:4.12) Recreate the test directory in your home directory and copy all the files from /vlsci/TRAINING/shared/Intro_to_Unix back into the test directory.¶
Hint
See exercises 4.1 and 4.2Answer
Repeat exercises 4.1 and 4.2.4.13) Change directories to ~/test and use the cat command to display the entire contents of the file hello.c¶
Hint
Use *man* if you can't guess how it might work.Answer
*hello.c* contains the source code of a C program. The compiled executable version of this code is in the file called *hi*, which you can run like so:4.14) Use the head command to view the first 20 lines of the file sample_1.fastq¶
Hint
Remember your *best* friend!Additional Hint
Use *man* to find out what option you need to add to display a given number of *lines*.Answer
$ head -20 sample_1.fastq
@IRIS:7:1:17:394#0/1
GTCAGGACAAGAAAGACAANTCCAATTNACATTATG
+IRIS:7:1:17:394#0/1
aaabaa`]baaaaa_aab]D^^`b`aYDW]abaa`^
@IRIS:7:1:17:800#0/1
GGAAACACTACTTAGGCTTATAAGATCNGGTTGCGG
+IRIS:7:1:17:800#0/1
ababbaaabaaaaa`]`ba`]`aaaaYD\\_a``XT
@IRIS:7:1:17:1757#0/1
TTTTCTCGACGATTTCCACTCCTGGTCNACGAATCC
+IRIS:7:1:17:1757#0/1
aaaaaa``aaa`aaaa_^a```]][Z[DY^XYV^_Y
@IRIS:7:1:17:1479#0/1
CATATTGTAGGGTGGATCTCGAAAGATATGAAAGAT
+IRIS:7:1:17:1479#0/1
abaaaaa`a```^aaaaa`_]aaa`aaa__a_X]``
@IRIS:7:1:17:150#0/1
TGATGTACTATGCATATGAACTTGTATGCAAAGTGG
+IRIS:7:1:17:150#0/1
abaabaa`aaaaaaa^ba_]]aaa^aaaaa_^][aa
4.15) Use the tail command to view the last 8 lines of the file sample_1.fastq¶
Hint
It's very much like *head*.Answer
4.16) Use the grep command to find out all the lines in moby.txt that contain the word “Ahab”¶
Hint
One might say we are 'looking for the *pattern* "Ahab"'Answer
If you want to know how many lines are in the output of the above command you can "pipe" it into the *wc -l* command: which shows that there are *491* lines in *moby.txt* that contain the word Ahab.4.17) Use the grep command to find out all the lines in expectations.txt that contain the word “the” with a case insensitive search (it should count “the” “The” “THE” “tHe” etcetera).¶
Hint
One might say we are *ignoring case*.Additional Hint
Answer
Use the *-i* flag to *grep* to make it perform case insensitive search:$ grep -i the expectations.txt
The Project Gutenberg EBook of Great Expectations, by Charles Dickens
This eBook is for the use of anyone anywhere at no cost and with
re-use it under the terms of the Project Gutenberg License included
[Project Gutenberg Editor's Note: There is also another version of
... AND MUCH MUCH MORE ...
4.18) Use the gzip command to compress the file sample_1.fastq. Use gunzip to decompress it back to the original contents.¶
Hint
Use the above commands along with *man* and *ls* to see what happens to the file.Answer
Check the file size of sample_1.fastq before compressing it:# check filesize
$ ls -l sample_1.fastq
-rw-r--r-- 1 training01 training 90849644 Jun 14 20:03 sample_1.fastq
# compress it (takes a few seconds)
$ gzip sample_1.fastq
# check filesize (Note: its name changed)
$ ls -l sample_1.fastq.gz
-rw-r--r-- 1 training01 training 26997595 Jun 14 20:03 sample_1.fastq.gz
# decompress it
$ gunzip sample_1.fastq.gz
$ ls -l sample_1.fastq
-rw-r--r-- 1 training01 training 90849644 Jun 14 20:03 sample_1.fastq
Topic 5: Pipes, output redirection and shell scripts¶
In this section we will cover a lot of the more advanced Unix concepts; it is here where you will start to see the power of Unix. I say start because this is only the “tip of the iceberg”.
Duration: 50 minutes.
Relevant commands: wc, paste, grep, sort, uniq, nano, cut
5.1) How many reads are contained in the file sample_1.fastq?¶
Hint
Examine some of the file to work out how many lines each *read* takes up.Additional Hint
Count the number of linesAnswer
We can answer this question by counting the number of lines in the file and dividing by 4: **Answer**: There are *3000000* lines in the file representing *750000* reads. If you want to do simple arithmetic at the command line then you can use the "basic calculator" called *bc*:5.2) How many reads in sample_1.fastq contain the sequence GATTACA?¶
Hint
Check out exercise 4.16Answer
Use *grep* to find all the lines that contain *GATTACA* and "pipe" the output to *wc -l* to count them: **Answer**: *1119* If you are unsure about the possibility of upper and lower case characters then consider using the *-i* (ignore case option for grep).5.3) On what line numbers do the sequences containing GATTACA occur?¶
Hint
We are looking for the *line numbers*.Additional Hint
Check out the manpage for *grep* and/or *nl*Answer
You can use the *-n* flag to grep to make it prefix each line with a line number: **Answer 1**:$ grep -n GATTACA sample_1.fastq
5078:AGGAAGATTACAACTCCAAGACACCAAACAAATTCC
7170:AACTACAAAGGTCAGGATTACAAGCTCTTGCCCTTC
8238:ATAGTTTTTTCGATTACATGGATTATATCTGTTTGC
... AND MUCH MUCH MORE ...
5.4) Use the nl command to print each line of sample_1.fastq with its corresponding line number at the beginning.¶
Hint
Check answer to 5.3.Answer
There are a lot of lines in that file so this command might take a while to print all its output. If you get tired of looking at the output you can kill the command with *control-c* (hold the *control* key down and simultaneously press the "*c*" character).5.5) Redirect the output of the previous command to a file called sample_1.fastq.nl.¶
Check the first 20 lines of sample_1.fastq.nl with the head command. Use the less command to interactively view the contents of sample_1.fastq.nl (use the arrow keys to navigate up and down, q to quit and ‘/’ to search). Use the search facility in less to find occurrences of GATTACA.
Hint
Ok that one was tough, *> FILENAME* is how you do it if you didn't break out an internet search for "redirect the output in Unix"Answer
The greater-than sign "*>*" is the file redirection operator. It causes the standard output of the command on the left-hand-side to be written to the file on the right-hand-side. You should notice that the above command is much faster than printing the output to the screen. This is because writing to disk can be performed much more quickly than rendering the output on a terminal. To check that the first 20 lines of the file look reasonable you can use the *head* command like so: The *less* command allows you to interactively view a file. The arrow keys move the page up and down. You can search using the '*/*' followed by the search term. You can quit by pressing "*q*". Note that the *less* command is used by default to display man pages.5.6) The four-lines-per-read format of FASTQ is cumbersome to deal with. Often it would be preferable if we could convert it to tab-separated-value (TSV) format, such that each read appears on a single line with each of its fields separated by tabs. Use the following command to convert sample_1.fastq into TSV format:¶
Answer
The *'-'* (dash) character has a special meaning when used in place of a file; it means use the standard input instead of a real file. Note: while it is fairly common in most Unix programs, not all will support it. The *paste* command is useful for merging multiple files together line-by-line, such that the *Nth* line from each file is joined together into one line in the output, separated by default with a *tab* character. In the above example we give paste 4 copies of the contents of *sample_1.fastq*, which causes it to join consecutive groups of 4 lines from the file into one line of output.5.7) Do you expect the output of the following command to produce the same output as above? and why?¶
Try it, see what ends up in sample_1b.tsv (maybe use less)
Hint
Use *less* to examine it.Answer
**Answer**: No, in the second instance we get 4 copies of each line. **Why**: In the first command *paste* will use the input file (standard input) 4 times since the *cat* command will only give one copy of the file to *paste*, where as, in the second command *paste* will open the file 4 times. Note: this is quite confusing and is not necessory to remember; its just an interesting side point.5.8) Check that sample_1.tsv has the correct number of lines. Use the head command to view the first 20 lines of the file.¶
Hint
Remember the *wc* command.Answer
We can count the number of lines in *sample_1.tsv* using *wc*: The output should be *750000* as expected (1/4 of the number of lines in sample_1.fastq). To view the first *20* lines of *sample_1.tsv* use the *head* command:5.9) Use the cut command to print out the second column of sample_1.tsv. Redirect the output to a file called sample_1.dna.txt.¶
Hint
See exercise 5.3 (for cut) and 5.5 (redirection)Answer
The file sample_1.tsv is in column format. The cut command can be used to select certain columns from the file. The DNA sequences appear in column 2, we select that column using the -f 2 flag (the f stands for "field"). Check that the output file looks reasonable using *head* or *less*.5.10) Use the sort command to sort the lines of sample_1.dna.txt and redirect the output to sample_1.dna.sorted.txt. Use head to look at the first few lines of the output file. You should see a lot of repeated sequences of As.¶
Hint
Use *man* (sort) and see exercise 5.5 (redirection)Answer
Running *head* on the output file reveals that there are duplicate DNA sequences in the input FASTQ file.5.11) Use the uniq command to remove duplicate consecutive lines from sample_1.dna.sorted.txt, redirect the result to sample_1.dna.uniq.txt. Compare the number of lines in sample1_dna.txt to the number of lines in sample_1.dna.uniq.txt.¶
Hint
I am pretty sure you have already used *man* (or just guessed how to use *uniq*). You're also a gun at redirection now.Answer
Compare the outputs of: View the contents of *sample_1.dna.uniq.txt* to check that the duplicate DNA sequences have been removed.5.12) Can you modify the command from above to produce only those sequences of DNA which were duplicated in sample_1.dna.sorted.txt?¶
Hint
Checkout the *uniq* manpageAdditional Hint
Look at the man page for uniq.Answer
Use the *-d* flag to *uniq* to print out only the duplicated lines from the file:5.13) Write a shell pipeline which will print the number of duplicated DNA sequences in sample_1.fastq.¶
Hint
That is, *piping* most of the commands you used above instead of redirecting to fileAdditional Hint
i.e. 6 commands (*cat*, *paste*, *cut*, *sort*, *uniq*, *wc*)Answer
Finally we can 'pipe' all the pieces together into a sophisticated pipeline which starts with a FASTQ file and ends with a list of duplicated DNA sequences: **Answer**: The output file should have *56079* lines.5.14) (Advanced) Write a shell script which will print the number of duplicated DNA sequences in sample_1.fastq.¶
Hint
Check out the *sleepy* file (with *cat* or *nano*); there is a bit of magic on the first line that you will need. You also need to tell bash that this file can be executed (check out *chmod* command).Answer
Put the answer to *5.13* into a file called *sample_1_dups.sh* (or whatever you want). Use *nano* to create the file. **Answer**: the contents of the file will look like this:5.15) (Advanced) Modify your shell script so that it accepts the name of the input FASTQ file as a command line parameter.¶
Hint
Shell scripts can refer to command line arguments by their position using special variables called *$0*, *$1*, *$2* and so on.Additional Hint
*$0* refers to the name of the script as it was called on the command line. *$1* refers to the first command line argument, and so on.Answer
Copy the shell script from *5.14* into a new file: Edit the new shell script file and change it to use the command line parameters: You can run the new script like so: In the above example the script takes *sample_1.fastq* as input and prints the number of duplicated sequences as output. **A better Answer**: Ideally we would write our shell script to be more robust. At the moment it just assumes there will be at least one command line argument. However, it would be better to check and produce an error message if insufficient arguments were given:#!/bin/bash
if [ $# -eq 1 ]; then
cat $1 | paste - - - - | cut -f 2 | sort | uniq -d | wc -l
else
echo "Usage: $0 <fastq_filename>"
exit 1
fi
5.16) (Advanced) Modify your shell script so that it accepts zero or more FASTQ files on the command line argument and outputs the number of duplicated DNA sequences in each file.¶
Answer
We can add a loop to our script to accept multiple input FASTQ files: There's a lot going on in this script. The *$@* is a sequence of all command line arguments. The '*for ...; do*' (a.k.a. for loop) iterates over that sequence one argument at a time, assigning the current argument in the sequence to the variable called *file*. The *$(...)* allow us to capture the output of another command (in-place of the *...*). In this case we capture the output of the pipeline and save it to the variable called *dups*. If you had multiple FASTQ files available you could run the script like so: And it would produce output like:Finished¶
Well done, you learnt a lot over the last 5 topics and you should be proud of your achievement; it was a lot to take in.
From here you should be comfortable around the Unix command line and ready to take on the HPC Workshop.
You will no-doubt forget a lot of what you learnt here so I encourage you to save a link to this workshop for later reference.
Thank you for your attendance, please don’t forget to complete the training survey and give it back to the workshop facilitators.