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:
To change directories use cd
.
To determine current working directory use pwd
.
To list the contents of a directory use ls
.
To create a new directory use mkdir
.
To create a new file use touch
.
To insert text into a file use echo
.
To display the contents of the file in the terminal use less
or more
.
To open a file from the command line use open
. To specify a program use the -a
option.
To delete a file use rm
. To delete a directory use the -r
option.
To create a file use touch
.
To print output from a command to a new file use >file_name
.
To append the output from a command to a file use >>file_name
.
Useful keyboard shortcuts::
ctrl+a
.ctrl+e
.ctrl+u
.up
and down
arrow keys.tab
. This is highly recommended in order to minimize typos.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:
echo Hello, world!
1.3. Create a new directory called my_directory
using the mkdir
command:
mkdir my_directory
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:
cd my_directory
1.5. Confirm that you are in my_directory
using the pwd
command:
pwd
1.6. Create a new directory called birthday
using the mkdir
command:
mkdir birthday
1.7. Change into your home directory using cd
cd
1.8. Change into the birthday
directory with one command using cd
:
cd my_directory/birthday
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:
touch birthday.txt
1.10. Examine the contents of my_directory
using the ls
command to confirm that the new file exists:
ls
1.11. Add your name to birthday.txt
using echo
:
echo your_name >birthday.txt
>
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
:
echo birthday >>birthday.txt
>>
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
):
echo day_of_week >>birthday.txt
1.14. Add the day of the week your birthday falls on this year using echo
:
echo birthday_in_2022 >>birthday.txt
1.15. Display the contents of the file in the terminal using less
:
less birthday.txt
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 birthday.txt
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
:
rm birthday.txt
Be careful using rm
because it will permanently delete a file.
1.18. Confirm that the file was deleted using ls
.
ls
1.19. Change out of the birthday
and my_directory
directories using cd
:
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.
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
.