I accidentally closed the tab containing the NEARLY FINISHED original post (and it did not magically save itself) so fair warning this post will be shorter, crankier, and less clever then it was originally intended.
The terminal/command line allows the user to interact with their computer without using a GUI (graphical user interface) or the mouse. Experience and comfort working in the command line is essential to bioinformatic analysis. Using it for the first time can seem intimidating, but it is fairly simple once you get started. The command line uses an operating system called unix. In the following post I will show you how to access the command line and introduce few simple commands.
A few quick definitions
command line – the command line is the place where you type commands in a terminal window. It looks something like this: (colors of text and background, may vary)
script – a short computer program. Usually computer programs are called scripts when they perform a simple function or a small number of simple functions. Scripts are often strung together into a larger program called a pipeline. Scripts are typically only run from the command line.
directory – for practical purposes, this is just another name for a folder (the same type you would encounter on your desktop or in your finder (if you’re working on Mac OS).
** These instructions are compatible with a computer running Mac OS or linux (although if you are running linux you probably already know how to access/use the command line). Future posts will mainly cover python syntax/coding either online or in a python shell so if you are a windows user you should keep following the blog!
I did find these tutorials/resources using the command line with a computer running windows
Locating the Terminal
I have included both videos and text on locating and using the terminal because while I personally hate learning things from videos, they can be helpful in transmitting information. I recommend at least watching the video on locating the terminal ( I tried to make these videos as nonirritating as possible–they may seem a little fast but when I slowed them down they became painful to watch).
To open the terminal
Go to Go>Applications>Terminal
Using the Terminal
In the terminal you move between directories (folders) using the command “cd” (change directory) followed by the path to the directory you want to go be in.
When you type in the terminal it will appear after the $. The word prior to the $ is your current directory. Everything after the $ is the code you are executing.
IMPORTANT SIDE NOTE
In unix (and many/most other coding languages) spaces are significant. This is because every unix command takes the basic form:
Since spaces are used to separate important parts of the command, file names with spaces can be problematic. For this reason most programmers replace spaces with underscores (_) in file and directory names to avoid screwing up their scripts (i.e. file_name)
dhcp16-gc1:~ Madison$ cd Desktop/Workflow/Paper
In the above example I am currently in the Madison home directory. The ~ indicates it is the home directory. To change directories I use the cd command followed by the path I want to take. I move to the paper directory via the Desktop followed by the Workflow directories. I could move through the same path in three separate steps
dhcp16-gc1:~ Madison$ cd Desktop/
dhcp16-gc1:Desktop Madison$ cd Workflow/
dhcp16-gc1:Workflow Madison$ cd Paper/
To move up a level you can use the “cd” command followed by “..”
dhcp16-gc1:Paper Madison$ cd ..
The above command moves from the Paper directory to the Workflow directory above it
dhcp16-gc1:Workflow Madison$ cd ..
The above command moves from the Workflow directory to the Desktop directory above it
To return directly to the home directory you can use the “cd ~” command
$ cd ~
If you aren’t sure where you are, or what directories are available to you type the command “pwd” which will display the path of the current directory
To see all of the files and directories in your current directory use the “ls” command
To specify a file name you need to either be in the directory containing that file, or you need to specify the path to the file
If I wanted to specify a file in the Paper directory (from the above examples) the syntax would be
Tab complete is the best! When typing the name of a file or directory after you have typed the first few letters hit tab and the computer will fill in the rest. If multiple files or directories begin with the same letters, tab complete will fill in the letters to the point at which they diverge. If you then double tap the tab button it will list all of the directories that begin with those characters.
$ less file_name
The “less” command will display the file (listed after the command) in the terminal window, “cat” and “more” perform a similar function
$ cp file1 file2
The “cp” command will make a copy of file1 named file2, if you want file2 to be located in a different directory you must specify the path before the new file name (i.e. cp file1 ~/Documents/Blog/file2)
$ mv file1 file2
The “mv” command is similar to cp, but instead of making a second copy of the file, it renames file1 (and moves if you specify a path) to file2
$ head file_name
The “head” command displays the first ten lines of the file
$ tail file_name
The “tail” command displays the last ten lines of the file
$ grep ‘keyword’ file_name
The “grep” searches a file for all instances of the keyword and displays them on the terminal screen (the keyword MUST be in single quotes i.e. ‘keyword’)
$ grep -c ‘keyword’ file_name
The “grep -c” command counts the number of times the keyword occurs in the file and displays that number on the terminal screen (again the keyword MUST be in single quotes i.e. ‘keyword’)
$ whatis command
The “whatis” command followed by a command (i.e. whatis ls) will return a brief description of the command
Here is a cheat sheet of Unix commands and their meanings
This tutorial walks you through different unix commands
Ian Korf at the UC Davis Genome Center also has an excellent unix and perl primer here
This also looked like a useful resource
Thanks to the awesome Hannah Holland-Moritz for her help writing and editing this post!