What is Terminal
Basically, Terminal is a nifty way to manipulate data, create programs, edit programs, run programs, and do all sorts of other things that can be difficult if not impossible without it. This post will focus on the basics I find I use most often, and I will assume you’ve never used Terminal before. I will also only focus on syntax for Macs, as Windows has its own syntax sometimes.
Please note that I know there are many, many other useful commands. These are just the commands I find I use most often, and knowing these will get you on your feet.
To access Terminal the quick way, hold down the command key and press the spacebar, then type in Terminal and hit Return.
To access Terminal the long way, go to Applications, then Utilities, then Terminal.
You should see a window pop up with something like this:
Now that you’re in the terminal, let’s say you want to figure out where in your computer you are. In other words, what’s the working directory (the folder) you’re in? Type pwd into the command line to print working directory, then hit the return key, and you’ll see something like this:
Lees-MacBook:~ leeorg$ pwd /Users/leeorg Lees-MacBook:~ leeorg$
But let’s say I don’t want to be here in /Users/leeorg. I want to change directories and go to a folder in my Documents folder. Use cd to change directory to the directory of your choice. (Tip: once you type in the first few characters of your directory name, you can often autocomplete by pressing the tab key.)
Lees-MacBook:~ leeorg$ cd Documents/ Lees-MacBook:Documents leeorg$
Now I’m in my Documents folder. But what was the name of the file I wanted to go into? I can’t really remember the exact name, but that’s ok, I’ll list all the files and directories here using ls:
Lees-MacBook:~ leeorg$ ls Security_project MISL_project my_favorite_foods.txt
As you can see, I have two folders and one text file. But before I go on, I’m getting kind of annoyed at how my terminal looks. It’s a bit cluttered:
Lees-MacBook:~ leeorg$ pwd /Users/leeorg Lees-MacBook:~ leeorg$ cd Documents/ Lees-MacBook:Documents leeorg$ ls Security_project MISL_project my_favorite_foods.txt
You can clear all this from the screen by typing clear then hitting return. You can always scroll up to see what you’ve previously cleared.
Creating and removing stuff via Terminal
Now say I want to create a new file, maybe a document listing my favorite scientists, or an essay on why robots will one day dream of electric sheep. Write touch then type the name of your document’s name, like this:
Lees-MacBook:~ leeorg$ touch robots_will_dream_of_electric_sheep.txt
Now if you use ls again, you’ll see the new file, robots_will_dream_of_electric_sheep.txt has appeared.
To remove a file, write rm then type the name of the file you want to remove. To remove an entire folder and everything in it, type rm -R then the name of the directory. BE CAREFUL. This will delete everything in this folder.
Look at data in hard-to-open and/or large files
This is, in my novice opinion,where terminal can really become handy. As I mentioned in the previous post Reading Data into Python (Easy Mode), it can be extremely handy to see what data is in a file via Terminal. For example, all my NGS data is in a file format I can’t easily just click on and open up like a text file. And other times, opening the entire file will take up too much of your computer’s memory, so it’s best if you only see a portion of the contents at a time. To do this, I use head and tail to look at the first or last few lines of a file respectively.
Lees-MacBook:~ leeorg$ head -10 your_filename_here.fastq
That snippet will print out the first 10 lines of your file. You could also have written tail to see the last ten lines of the file (you don’t have to use 10, you can use any number).
How to run your Python progam in Terminal
This part is surprisingly easy! The easiest way to do this is to navigate to the directory your python program is in, then just type python and the file name, hit enter, and your program will start running!
Lees-MacBook:~ leeorg$ python my_program.py
Not sure which version of Python you’re running? Or even more handy (for me anyways), need to quickly pop into Python’s shell to remind yourself what range(20) outputs because it’s been a really long day and you’ve been writing for hours and you haven’t seen the sun in a week and you’re hungry and tired and you just can’t remember anything right now? No problem. Just type python then hit return, and the shell will open right up, showing you the version at the top. You can see I’m running Python 2.7.10 here:
Lees-MacBook:Documents leeorg$ python Python 2.7.10 (default, Feb 7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Type exit() and hit return to exit the shell and get back to where you were.
But why run your Python program in Terminal at all? Well, a lot of programs take in arguments. For example, let’s say a labmate gives you a piece of code, but for the code to run you have to tell it which file to look at. You can’t really do this by opening up the Python program in your favorite editor and pressing “go”. You need to use Terminal to pass the argument to the program. Each program will (or should) come with documentation on what arguments it takes, but to run the program it will basically look like this:
Lees-MacBook:Documents leeorg$ python some_program.py arg1 arg2
If your labmate does not give you documentation, you should publicly shame them in lab meeting.
How to best edit your program in Terminal
If you search the internet for the answer to this, you will quickly run into some highly opinionated articles. Basically…there’s no super simple way to open up your python script, edit your stuff real quick, then pop back out. When you get to a point where you want to do this (it can be very handy), I recommend either emacs or vim. I’ve used both, but have gotten used to emacs.
|pwd||print working directory|
|cd directory_name||move into directory_name directory|
|cd ..||move one level up from your current directory|
|ls||list all files/directoried in current directory|
|clear||clear the current view of all previous commands|
|touch||create a file of your choosing|
|rm||remove a file of your choosing|
|rm -R||CAUTION- remove directory and all files in that directory of your choosing|
|head -7||see only the first 7 lines of a file of your choosing|
|tail -5||see only the last 5 lines of a file of your choosing|
|python program.py||run your program of choice|
|python||opens Python’s interactive shell, when done, use exit()|