UNIX and command line tools

In this exercise, we will attemp to demystify the computer terminal and introduce some common command line tools.

Useful UNIX commands for the exercises below:

Useful keyboard shortcuts::


Part 1. Interactive Exercise (follow along with instructor)

1.1. Open a linux terminal window (Windodws - Ubuntu, Mac - Terminal).

1.2. Print the phrase Hello, world! to the terminal window using the echo command:

1.3. Create a new directory called my_directory using the mkdir command:

Notice the underscore in the name. Underscores are often preferred over spaces because spaces have special meaning in the terminal.

1.4. Change into my_directory using the cd command:

1.5. Confirm that you are in my_directory using the pwd command:

1.6. Create a new directory called birthday using the mkdir command:

1.7. Change into your home directory using cd

1.8. Change into the birthday directory with one command using cd:

You can use this approach to change into any directory on your computer. In this example, the path is relative to your working directory. If the first directory is not within your working directory this approach will not work. You can also use an absolute path that will work from anywhere on your computer. This path typically starts with a root directory, such as Users, and includes every subdirectory.

1.9. Create a new file named birthday.txt within my_directory using the touch command:

1.10. Examine the contents of my_directory using the ls command to confirm that the new file exists:

1.11. Add your name to birthday.txt using echo:

> will redirect the output to the file, overwriting any existing file with the same name. The file doesn't actually need to exist beforehand.

1.12. Add your birthday to the file using echo:

>> will redirect the output to the file, appending it to the end.

1.13. Add the day of the week (such as Monday) you were born using echo (if you don’t know what day you were born, use the cal command to find out (cal month year):

1.14. Add the day of the week your birthday falls on this year using echo:

1.15. Display the contents of the file in the terminal using less:

less is a really useful tool because it will display only a screenfull worth of a file at a time so you can use it with any size file. If you want to display the contents of a file in the terminal, default to less (or the equivalent more).

1.16. Open birthday.txt from the command line in the default program using open:

open will use the default program to open a file. You can specify a program with the -a flag (for example, open -s BBEDIT).

1.17. Delete the file using rm:

Be careful using rm because it will permanently delete a file.

1.18. Confirm that the file was deleted using ls.

1.19. Change out of the birthday and my_directory directories using cd:

The .. specifies up one directory. You can move up two directories with cd ../... Note that / is used to distinguish directories. You can move to your home directory by just typing cd and return.


Part 2. Independent Exercise (attempt it on your own)

During the exercise, practice using ctrl+a to go the beginning of a line, ctrl+e to go the end of a line, and ctrl+u to delete text. Examine the contents of the directory you make from both the command line and the Finder GUI.

2.1. Create new directory named DNA.

2.2. From the command line, create a new file named dna.txt within the DNA directory.

2.3. Insert a 6-nt DNA sequence in the 5’-3’ direction into the file.

2.4. Insert 6 pipes (i.e. '||||||' – be sure to include quotes around the pipes because pipes have a special meaning from the command line) into the dna.txt file.

2.5. Append the reverse complement of the original DNA sequence in the 3’-5’ direction to the file (for example, if your original sequence was 'ATTCCC', here you would enter 'TAAGGG').

2.6. Display the contents of the dna.txt in the terminal.

2.7. Open the file in a text editor from the command line.

2.8. Are the sequences properly basepaired?

2.9. Move up into the parent directory using cd ...

2.10. From the command line, delete the file and the DNA directory. To delete a directory, use rm -R directory_name. Warning! this can't be undone.

2.11. Confirm that the directory and file were deleted using ls.