HomepageTeaching PageResearch PageResources Page

Terminal Tutorial


An introduction to using a Unix/Linux/Something-else-nix Terminal.


Terminal comic from thedesignteam.io

First, find the Terminal or Konsole program. In Ubuntu, it's located on the side bar. In Mac OSX, it's often already on the dock. If not, it's in the Applications -> Utilities folder. Run the program and you should see a window that looks something like the following.
something-here:~$ 

The characters before the $ will be different depending on your computer. We'll simplify things by removing everything before the $, like so:
$ 

The Terminal is a way to interact with the computer using a Command Line (Text) interface instead of a Graphical User Interface (GUI). You can enter commands and press enter to execute them. For example, if you want to find out what the working directory is, use pwd:
$ pwd
/home/urawesome
$
(Your terminal will say something different depending on the settings and your username.)

You can see which files and subdirectories are in the current directory by typing ls:
$ ls
Desktop       monkey.txt
Documents     zebra.txt
$
(Again, your results will be different.)

You can create a new directory using mkdir:
$ mkdir programs
$ ls
Desktop       programs
Documents     zebra.txt
monkey.txt
$

Change to a new directory by using cd:
$ cd programs
$ ls
$
Notice there are no files in the new directory. Now let's use pwd to show the current directory:
$ pwd
/home/urawesome/programs
$

You can create a new empty file using touch:
$ touch textFile.txt
$ ls
textFile.txt
$

Let's find out more about the new file by using ls with the -l parameter, like so:
$ ls -l
-rw-rw-r-- 1 urawesome urawesome 0 Jan  1 14:11 textFile.txt
$

We can see any hidden files or folders by using the -a parameter:
$ ls -a
.  ..  textFile.txt
$
. refers to this directory, while .. refers to the parent directory (where you were before changing directory).

Command parameters can be combined, like so:
$ ls -al
drwxr-xr-x  2 urawesome urawesome 4096 Jan  1 14:09 .
drwxr-xr-x 49 urawesome urawesome 4096 Jan  1 14:10 ..
-rw-rw-r--  1 urawesome urawesome    0 Jan  1 14:11 textFile.txt
$

There's more we can do with ls! We can list the elements in a place other than the current working directory by specifying the directory:
$ ls ..
Desktop       programs
Documents     zebra.txt
monkey.txt
$ ls ../
Desktop       programs
Documents     zebra.txt
monkey.txt
$
Or we can specify only specific types of files:
$ ls *.txt
textFile.txt
$
* is a 'wildcard' symbol, so that command will display all files ending in .txt (all text files). We can combine the two parts like so:
$ ls ../*.txt
monkey.txt  zebra.txt
$
However:
$ ls ..*.txt
ls: cannot access ..*.txt: No such file or directory
$

Folders are separated using the / (slash). For example, another way to list the files in the current (programs) directory is:
$ ls ../programs
textFile.txt
$ ls ../programs/*.txt
textFile.txt
$

Let's play around with cd a bit more:
$ cd ..
$ pwd
/home/urawesome
$ cd ..
$ pwd
/home
$
Alternatively, you can specify the full directory path:
$ cd /home/urawesome/programs
$ pwd
/home/urawesome/programs
$ cd ~
$ pwd
/home/urawesome
$ 
(The tilde, ~, is shorthand for the home directory.)

Let's change the name of the file we created before!
$ cd programs
$ ls
textFile.txt
$ mv textFile.txt emptyFile.txt
$ ls
emptyFile.txt
$

mv is short for move. We can use it to move files from one place to another:
$ mv emptyFile.txt ../emptyFile.txt
$ ls
$ cd ..
$ ls
Desktop     emptyFile.txt   monkey.txt
Documents   programs        zebra.txt
$
The signature for mv always looks like: mv <source> <destination>
$ mv emptyFile.txt programs/emptyFile.txt
$ cd programs
$ ls
emptyFile.txt
$
If we specify the destination folder name, but not the destination filename, it will preserve the old name:
$ mkdir textFiles
$ ls
emptyFile.txt   textFiles
$ mv emptyFile.txt textFiles/
$ ls
textFiles
$ cd textFiles
$ ls
emptyFile.txt
$

We can move entire directories!
$ cd ..
$ ls
textFiles
$ mv textFiles textOnly
$ ls
textOnly
$ ls textOnly/
emptyFile.txt
$

cp (copy) is nearly the same as mv, except that it doesn't delete the original file or folder:
$ cp textOnly/emptyFile.txt textOnly/blankFile.txt
$ ls textOnly/
blankFile.txt   emptyFile.txt
$

cp is a bit more ornery with folders. Let's try to (re)create our textFiles directory by copying textOnly:
$ cp textOnly textFiles
cp: omitting directory `textOnly'
$
We can get around this by using the -r parameter:
$ cp -r textOnly textFiles
$ ls
textFiles   textOnly
$

Sometimes you have excess files that you don't need around anymore. We can delete them with rm (remove), like this:
$ ls textFiles/
blankFile.txt   emptyFile.txt
$ rm textFiles/blankFile.txt
$ ls textFiles/
emptyFile.txt
$ 

You can remove multiple files by using a star (*):
$ rm textOnly/*.tx
rm: cannot remove `textOnly/*.tx': No such file or directory
$ rm textOnly/*.txt
$ ls textOnly/
$ 
Warning: there is no undo command! rm will permanently delete your files! Always proceed with caution, especially when using wildcards! As a safeguard, rm won't delete directories unless you include the -r parameter. Don't type in the following commands unless you want to remove all files, then all folders in the directory you're in.
$ rm *
rm: cannot remove `textFiles': Is a directory
rm: cannot remove `textOnly': Is a directory
$ rm -r *
$ ls 
$
Do not use rm lightly!

Using tab completion will make your Terminal maneuvering much faster! Whenever you are typing in a string (a sequence of characters) and you think there is only one possible file/folder/command that starts with those characters, try pressing tab. For example, type the following commands, but don't hit enter at the end of the last one.
$ mkdir superMonkeyBonusDirectory
$ mkdir superMonk
We don't want to type out the whole thing. Instead, hit tab. Since there is only one possibility, it will automatically fill out the rest for you:
$ mkdir superMonkeyBonusDirectory
$ mkdir superMonkeyBonusDirectory/
We're not done with that line yet; we're going to make a subdirectory of the previous one:
$ mkdir superMonkeyBonusDirectory
$ mkdir superMonkeyBonusDirectory/jackalCupcake
$
Let's make yet another directory inside that last one. Use tab completion to make it easy:
$ mkdir s
Since there is only one file/folder in the working directory starting with 's', press tab:
$ mkdir superMonkeyBonusDirectory/
Since there is only one file/folder in the subdirectory, press tab again:
$ mkdir superMonkeyBonusDirectory/jackalCupcake/
Now type in the name of the new (subsub)directory you're creating:
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel
$

Often you'll want to execute a command identical or similar to one you've recently written. You can scroll through these by pressing the up (and down) arrows. Perhaps we want to create (yet another) subdirectory inside the one we just created. Consider the previous command:
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel
$
Press up once to see:
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel
Tab to get the slash:
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel/
And now enter the name of the new folder.
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel
$ mkdir superMonkeyBonusDirectory/jackalCupcake/oleOleBiscuitBarrel/cheeseWeasel
$ 
You can press up multiple times to see old commands. This is very useful if you're running the same program multiple times between editing files.

Awesome, now you've got a handle on basic terminal commands! This is only the start! Sorry I didn't say anything about how to edit files, but I don't want to choose a side.

Acknowledgements
Thanks to Dale Skrien and Eric Mann for helpful comments on drafts of this. If you notice some way to improve this, please let me know! :)

I'd love to add more things to this page; please let me know of anything you could suggest!