Unix Tutorial

Who this tutorial is for:

People with little to no Unix experience, or with some experience but looking for a refresher.

What are the goals of this tutorial:

To let the student practice the Unix skills and concepts needed to enjoy this course. The skills include:

What is a Unix terminal?

A terminal is a way for you to interact with your computer - just like you interact with your phone through the touch screen or your laptop through the track pad. The terminal lets you interact with your computer through typed in commands.

Why learn about Unix terminals?

Throughout this course you will be using the Unix terminal to interact with FreeSurfer and access data. Many computer programs for scientific use are designed to be operated through the terminal. By the end of this tutorial you will have familiarity with finding data and launching programs through the terminal.

How do I use the terminal?

Getting Started

TerminalIcon.png Click the terminal icon on the left side of your screen to launch the terminal.

A window will pop open on the screen, before we start typing into it it would be good to adjust the window to be easier to work with. Right click on the bar on the top of the window, then select the "Always On Top" option (this may not work if you are on Mac, if so you can continue with the tutorial anyways). That will keep the window in view while you navigate this tutorial. You can also move the window by holding left click on that top bar and moving the window, or resize it by holding left click on the corners.

OnTopCropped.png (The MacOSX equivalent of the "gnome-screenshot" command is called "screencapture".)

Whenever you launch the terminal, you can imagine it as if you are getting onto a bike. A bike has a location, and can travel to other locations. The terminal also has a location, and you can navigate it to other locations.

To see where you are, type in  pwd  and press enter. The terminal will print out where you are, locations in Unix are called "directories". pwd is short for "Print Working Directory", it prints out where you are. This is like asking your GPS "Where am I?".


Looking at files

To see what data is in this directory, type in  ls  and press enter.

You will see a list of 2 different things:




You will use  ls  a lot to take a look at where to go next while navigating the terminal, I probably use  ls  100s of times a day!  ls , as you may guess  ls  short for "list".

You find data in the Unix terminal by looking through directories, all data belongs inside a directory somewhere on the computer. If you use Windows or OSX you might know the term "folder", directories are the same concept.

One of the directories you can go to now is called "tutorial_data" lets enter that directory and explore what is inside.

Type  cd tutorial_data  and press enter

Now we are in the tutorial_data directory, try two things to explore:

  1. Type in  ls  to see what is in this directory

  2. Type in pwd to see what our working directory is now

Enter the practice_with_data directory with  cd practice_with_data 

Enter  ls  to see whats inside. There should be a directory inside called "DICOM". Enter the "DICOM" directory now, try to do it on your own, but look below if you need help.

Want to know the answer? Click and drag to highlight and reveal the text below.


Once inside DICOM, enter a command to see what data is inside. Try to do it on your own, but look below if you need help.

Want to know the answer? Click and drag to highlight and reveal the text below.


What you see is a list of data taken from a scan session, FreeSurfer takes this data and turns it into a whole lot of useful new data like 3d surfaces of brains. Let's take a look at some of that FreeSurfer data, first, leave the dicoms directory. How do you do that? Type in  cd .. 

 cd ..  will take you out of the directory you are in, and into the directory that directory is in. Let's go two more directories up, so type in  cd ..  again, hit enter. Now type in  cd ..  one more time and hit enter.

type in  pwd  and press enter to see where you are, you should be in the directory we started in when we first opened the terminal. If you are not sure, just type in  cd ~  and hit enter.  cd ~  will always send you to your starting directory.

Getting around quicker

Now type in only  cd tu  and don't press enter.


Press tab and the terminal will fill in what it thinks you will type - kind of like autocomplete on your phone or browser. It should fill it in with  tutorial_data/  , now enter that directory.


Now for a small challenge: use  ls ,  cd  and auto-completion to navigate to " /buckner_data/tutorial_subjs/004/mri "

Take a look at the files inside, these are freesurfer volumes. You will learn more about these later - but for now lets examine one.

Launching programs

Usually you can only access files and programs inside the directory the terminal is in - but we set up your computer so that you can launch FreeView (a tool to look at FreeSurfer data) anywhere. Enter this command to launch FreeView and examine some data ( you will want to make sure you are in the directory you were asked to go to last challenge, if you are not you can use this command to get there  cd $HOME/tutorial_data/buckner_data/tutorial_subjs/004/mri  )

 freeview -v orig.mgz 


Usually, after you type in a program name, you add something called arguments at the end like  -v orig.mgz . In this case the argument told FreeView to open the orig.mgz volume.

Spend some time exploring FreeView if you'd like.

Close your FreeView window (press on the word  file  in the top left, then  exit  ).

Now, an experiment for you to try. Leave the directory you are in by typing  cd ..  . Now, try to open the first volume we opened by typing in  freeview -v orig.mgz  again. You will get an error! That is expected.


The error is because there is no file called "orig.mgz" in the terminal's present working directory, the "orig.mgz" you opened earlier is in a different directory. The file "orig.mgz" is actually in the "mri" directory inside your terminal's present working directory.

Close FreeView again. Now, see if you can figure out how to open up the orig.mgz volume again with FreeView.

Were you successful? There are a couple of different ways to have done it. Highlight below to see.

Method 1:

 cd mri 

 freeview -v orig.mgz 

Method 2:

 freeview -v mri/orig.mgz 

There are often many ways to accomplish anything with the Unix Terminal, so if you ever see something done in a way you don't understand that is okay! The terminal is something you never stop learning about.

Accessing Variables

Moving in and out of directories can be exhausting! What if there was a way to tell your terminal to remember certain directories - like bookmarks in your web browser? There is. You can store directories in variables. Let's use a variable we already set up for you.

type in  cd $TUTORIAL_DATA  (hint, you can use the tab auto-complete here!)

You are back in your tutorial data directory! You can use this if you ever get lost in all of your directories. If you ever just want to take a peek to see what is inside a variable you can use  echo  type in  echo $TUTORIAL_DATA  to see that the variable is holding the path to your tutorial data directory. And if you type in  pwd  you will see that they match, because you are in the tutorial data now.

Whenever you open your terminal, we have it set up so a bunch of variables are set up for you. You can close your terminal now and re-open it to see, or just look at the screenshot below:


At the start of most tutorials you will be asked to set the SUBJECTS_DIR variable. FreeSurfer checks the SUBJECTS_DIR to find data, so we change that variable to point to whatever directory has the subject data we want FreeSurfer to process. Don't worry about knowing how to do that, each tutorial has instructions to set up the variables.

Copy paste and reading text files

Copying and pasting text into a terminal is a little different than you might be used to. First, highlight and copy the following line:  cd $TUTORIAL_DATA/buckner_data/tutorial_subjs/good_output/stats/ 

Now, go to your terminal and press your middle mouse button down to paste the text. Then press enter to run the command.

Enter  ls  and see the files inside. These are text files generated by FreeSurfer containing data about a subject. You can use the  less  command to quickly view the contents of a text file. For example, try typing in  less aseg.stats  . You may want to make the terminal larger to better view the contents. Use the up and down arrows on your keyboard to navigate the text, and press "q" to quit the less program.



You now have practiced the basic terminal skills needed to enjoy this course.

Have extra time? Play some Terminus! It's a game that will help you get very comfortable with the  cd  and  ls  commands, along with how paths work.


UnixTutorial (last edited 2019-09-23 07:42:40 by MatthewLarrabee)