Total beginner's guide to the shell / terminal

Getting a Terminal

If you are using OS X, hit Apple Key and Space bar at the same time, type "terminal", and hit enter. Voila! You are ready to move to the next step, Browsing Directories.

If you are using Windows, you have two options: install Cygwin, or get a bootable DVD for a linux distro such as Ubuntu. If you are using Cygwin, you will be presented with a terminal the first time you run it. If you are using a linux boot DVD, you can proceed to the immediately following Linux section.

If you are using Linux and are running a graphical user interface, you can hit Alt and Ctrl and F1 to get a terminal, or (if you are using Ubuntu), click on Applications -> Accessories -> Terminal.

Browsing Directories

First off, where the heck are we? There's a black screen, and not much more. Type pwd to print the working directory. This is likely going to be your home directory, which contains your Desktop directory, Documents directory, and other files belonging to you. To get a list of the files (and directories) in the current directory, you can type ls, short for list. Furthermore, we will add an l parameter to print out the details in a listed format, making the command ls -l

philippp@notphilatall:~$ pwd
/home/philippp
philippp@notphilatall:~$ ls -l
drwxr-xr-x  8 philippp philippp 4096 2011-02-19 13:27 .
drwxr-xr-x 44 philippp philippp 4096 2011-02-19 13:24 ..
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Desktop
drwxr-xr-x  4 philippp philippp 4096 2011-02-19 13:27 Documents
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Downloads
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Music
-rw-r--r--  1 philippp philippp   47 2011-02-19 13:27 notes.txt
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Pictures
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Public

There is a lot of information presented here, but most important to us is the column on the left (ex: drwxr-xr-x) and the filename on the right (ex: Desktop). You may have noticed that some of rows (like the row for notes.txt) do not have a d in the left column. This is to distinguish regular files from directories: Directories start with a d in the left column, but regular files start with a - in the left column. These directories may contain files and directories, while files are simply files. You can change directories with the cd command. When used by itself, cd will bring you into your home directory, making it a great and quick way to get back to where you started. When used with a directory name, cd "directory name" will bring you into the "directory name" directory. To go up one level in the directory tree, type cd ..

philippp@notphilatall:~$ cd Documents
philippp@notphilatall:~/Documents$ pwd
/home/philippp/Documents
philippp@notphilatall:~/Documents$ ls -l
-rw-r--r-- 1 philippp philippp 1012 2011-02-19 13:25 quick_notes.txt
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:24 Song Lyrics
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:24 Tax Documents
philippp@notphilatall:~/Documents$ cd
philippp@notphilatall:~$ pwd
/home/philippp
philippp@notphilatall:~$ cd Downloads
philippp@notphilatall:~/Downloads$ cd ..
philippp@notphilatall:~$ pwd
/home/philippp

Protip: If you want to go into a directory that contains a space, you will have to put the directory name in quotes. For example cd "Song Lyrics".

Great! Now we know how to:

Connecting Remotely

So far you have looked at files and directories on your own computers, but remote computers (such as web servers, and file servers) are just as interesting. You can use the exact same commands, and the exact same interface on pretty much every computer connected to the internet!

We will be using a tool called ssh (short for secure shell) to connect to a remote system. This assumes that you have (1) a network connection and (2) a remote system on which you have a login and password. For this example, we will pretend that the hostname of the remote system is notphil.com, our login is guest, and our password is pass9

The format for this command is ssh login@hostname, so to connect to our example server, we will enter ssh guest@notphil.com. When we are prompted for a password, we simply enter pass9, and we find ourselves in our home directory on the remote system. As mentioned before, we can use the exact same commands to navigate around. When we ready to disconnect, simply type exit to return to your local system.

Transferring Files

Copying, moving, and deleting locally

The copy command is cp, and the format for its invocation is cp source_file destination_directory_or_file. Similarly, the command to move a file is mv, and the format for its invocation is mv source_file destination_directory_or_file. Lastly, the command to remove a file is rm, and the format for its invocation is rm file_to_remove.

In the following example, we will copy the file notes.txt from our home directory into the Documents directory, and then delete the original notes.txt file in our home directory:

philippp@notphilatall:~$ ls -l
drwxr-xr-x  8 philippp philippp 4096 2011-02-19 13:27 .
drwxr-xr-x 44 philippp philippp 4096 2011-02-19 13:24 ..
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Desktop
drwxr-xr-x  4 philippp philippp 4096 2011-02-19 13:27 Documents
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Downloads
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Music
-rw-r--r--  1 philippp philippp   47 2011-02-19 13:27 notes.txt
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Pictures
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Public
philippp@notphilatall:~$ cp notes.txt Documents/
philippp@notphilatall:~$ cd Documents
philippp@notphilatall:~/Documents$ ls
-rw-r--r-- 1 philippp philippp 1012 2011-02-19 13:25 quick_notes.txt
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:24 Song Lyrics
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:24 Tax Documents
-rw-r--r-- 1 philippp philippp   47 2011-02-19 13:27 notes.txt
philippp@notphilatall:~/Documents$ cd ..
philippp@notphilatall:~$ rm notes.txt

Our more astute audience members may have noticed that copying a file and then deleting the original is no different from moving it. The following example has the exact same end-effect, and makes use of the mv command instead of cp and rm.

philippp@notphilatall:~$ ls -l
drwxr-xr-x  8 philippp philippp 4096 2011-02-19 13:27 .
drwxr-xr-x 44 philippp philippp 4096 2011-02-19 13:24 ..
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Desktop
drwxr-xr-x  4 philippp philippp 4096 2011-02-19 13:27 Documents
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Downloads
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Music
-rw-r--r--  1 philippp philippp   47 2011-02-19 13:27 notes.txt
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Pictures
drwxr-xr-x  2 philippp philippp 4096 2011-02-19 13:27 Public
philippp@notphilatall:~$ mv notes.txt Documents/
philippp@notphilatall:~$ cd Documents
philippp@notphilatall:~/Documents$ ls -l
-rw-r--r-- 1 philippp philippp 1012 2011-02-19 13:25 quick_notes.txt
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:24 Song Lyrics
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:24 Tax Documents
-rw-r--r-- 1 philippp philippp   47 2011-02-19 13:27 notes.txt

Copying to remote servers

Finally, we are going to use the secure copy command to copy a file from our local system to a remote system. The invocation for this command is a mixture of that of ssh and cp: scp local_file username@hostname:path_to_remote_directory to copy a file from your local system to a remote system, and scp username@hostname:path_to_remote_file local_file to copy from the remote system to your local system.

In our next example, we will copy our notes.txt file from our local Documents directory to the htdocs directory on notphil.com. We will then connect to the system using ssh, and browse to the htdocs directory to ascertain that the file was copied.

philippp@notphilatall:~$ scp Documents/notes.txt guest@notphil.com:htdocs/
philippp@notphilatall:~$ ssh guest@notphil.com
philippp@notphil.com's password:
Last login: Thu Feb 10 23:02:00 2011 from c-76-126-10-250.hsd1.ca.comcast.net

guest@notphil:~$ cd htdocs/
guest@notphil:~/htdocs$ ls -l
drwxr-xr-x 2 philippp philippp 4096 2011-02-19 13:38 images
-rw-r--r-- 1 philippp philippp  142 2011-02-19 13:39 index.html
-rw-r--r-- 1 philippp philippp   47 2011-02-19 13:27 notes.txt
guest@notphil:~/htdocs$ exit
logout
Connection to notphil.com closed.
philippp@notphilatall:$