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. 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.


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 2 directories. Enter the dicoms 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.

cd dicoms

Once inside dicoms, 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 "/home/nmrclass", your home directory we started in. If not, just type in  cd ~  and hit enter.  cd ~  will always send you home.

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/nmrclass/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 orig.mgz in the directory you are in, the orig.mgz is in a different directory. Orig.mgz is actually in the mri/ directory inside the directory your terminal is in right now, there are 2 different ways you can open it again.

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:

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.

Other useful info

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.