On the resume that convinced Wired to hire me, I said that I knew enough about Unix that it didn’t scare me anymore. This wasn’t exactly true. Unix was still a chilling concept for me when I arrived at the San Francisco office armed with a copy of Unix for Dummies. The managing editor steered me to my desk and instead of the Macintosh I was hoping for, there sat a purple SGI machine.
I realized then that I needed to learn a lot about Unix fast. Initially I tried using SGI’s graphical user interface, which mimicked the Macintosh desktop fairly well, but soon realized that it was just too damn slow. So I stole some better Unix books from the engineering staff and found a nice Unix expert to help me. Soon I was cp-ing, mv-ing, and chmod-ing like lightning. Unix still gives me the occasional nightmare, but basically I love it. It’s fast, it makes sense (most of the time), and anyone can figure it out with a little work. Plus, it looks great on a résumé. If you can convince a prospective employer that you have a working knowledge of Unix, you’re one step ahead of everyone else who is too scared even to try figuring it out.
I’ve put together a very basic explanation of Unix to get you started. But first, a warning. Unix is very powerful. The wrong collection of keystrokes can blow away files that you’ll probably never be able to recover, so practice on sample files before you move on to anything important.
Before you can do anything, you need an account. Find someone at your school or workplace whose title is webmaster, server administrator, systems administrator, or Unix god. Be very nice to this person. Bring gifts and libations, and then ask them to set up an account and user directory for you. Promise that you won’t bug them very often with questions, even though this may indeed turn out to be a white lie.
Keep in mind that Unix installations vary widely. If something doesn’t work for you here, it may be that a program wasn’t installed or your account was misconfigured. There’s also a strong possibility that you’re using one of several different kinds of Unix, which means your commands may vary slightly from what you learn here. If you run into trouble, go ask the person who set up your account to help you — but don’t forget to bring a gift.
Once you’ve got your account, go to the bookstore and purchase a few Unix books. My favorite is O’Reilly & Associates’ UNIX in a Nutshell. It’s a fabulous dictionary of commands.
To access your account, you’ll need a shell (a program that sends whatever you type to the host computer). The easiest way to go about this is to open a Telnet window. (If you’re sitting at the host computer, you’ll already have a shell.) Accessing files with Telnet is essentially the same as opening the Finder on your Mac or opening the Windows Explorer on a Microsoft machine — except that when you get into the file structure, you won’t find all those sissy color-coded directories.
So open Telnet, and choose Open Connection from the File menu. Fill in the Host/Session Name slot with your host’s address and hit Connect. You’ll be asked to enter the username and password that your nice sysadmin assigned you when your account was set up. You should also have a user directory, which is the first place you’ll end up when you Telnet to your Host. When you log on, you should get something that looks like this:
IRIX (sutro) login:pam Password: IRIX Release 5.3 IP20 sutro Copyright 1987-1994 Silicon Graphics, Inc. All Rights Reserved. Last login:Fri Dec 13 14:04:59 PST 1996 by UNKNOWN@hill.hotwired.com You have mail. sutro[~]%
Although you probably won’t see it when you log on, the actual location of your user directory is
I’ve put together a few commands to get you started. Most of these commands have several options that make them even more powerful, but I won’t go into those here. You can find them in the Unix Online Manual, which I’ll explain in a minute (or you can use that book you went out and bought).
ls – list files in a directory syntax:ls [options] [names]
You should get something that looks like this:
sutro.hotwired.com[~]% ls airwolf.au goo.html unixclass apanel.parameters graphics
In this example I have two files ( airwolf.au and goo.html) and three directories (unixclass, apanel.parameters, and graphics) in my user directory.
cd – change directory syntax:cd [dir]
Here’s an example of it what the result might be:sutro.hotwired.com[~]% cd unixclass
sutro.hotwired.com[~/unixclass]% ls one one.html two two.html sutro.hotwired.com[~/unixclass]%
In this case, I’ve changed from my user directory to the unixclass directory, which holds one directory and two files (if you want to move back one directory use % cd ../).
mv – moves or renames a file or directory syntax:mv [options] sources target
Here’s what it might look like:
sutro.hotwired.com[~/unixclass]% mv one/ two/ sutro.hotwired.com[~/unixclass]% ls one.html two two.html sutro.hotwired.com[~/unixclass]% ls two/ one sutro.hotwired.com[~/unixclass]%
I moved the directory one into directory two.
If you use mv to rename a file, it will look something like this:
sutro.hotwired.com[~/unixclass]% mv two/ somethingdifferent sutro.hotwired.com[~/unixclass]% ls one.html somethingdifferent two.html
In this example, I’ve renamed the directory two. Now it is called somethingdifferent.
rm – removes a file syntax:rm [options] [files]
Here’s an example of removing a file:
sutro.hotwired.com[~/unixclass]% rm one.html sutro.hotwired.com[~/unixclass]% ls somethingdifferent two.html
I removed the file one.html from the directory unixclass.
rm -ir – removes a directory
Here’s what happens when you remove a directory:
sutro.hotwired.com[~/unixclass]% rm -ir somethingdifferent/ Directory somethingdifferent/. Remove ? (yes/no)[no] :yes Directory somethingdifferent//one. Remove ? (yes/no)[no] :yes sutro.hotwired.com[~/unixclass]% ls two.html
In this example, I’ve used rm -ir to remove the directory somethingdifferent. Before the directory is removed, I must confirm that I really want it gone. Once I do, somethingdifferent no longer exists. Be very careful with rm, because once files or directories are gone, they’re probably gone forever.
mkdir – creates new directory syntax:mkdir [options] directories
Here’s how you do it:
sutro.hotwired.com[~/unixclass]% mkdir waga sutro.hotwired.com[~/unixclass]% ls two.html waga
I used mkdir to create the directory waga.
more – more a file when you just want to read it (not edit it)
less – less really is more. It does the same thing as more, but it allows you to search through a file (among other things).
exit – allows you to log out of your host
date – prints the current date and time
cal 1997 – prints the 1997 calendar
whoami – prints who is currently logged on to your terminal
mail – brings up a simple mail editor
pwd – tells you where you are; it prints the full pathname of the current directory
Unix online manualIf you want more details about a particular command, you can check out the Unix main page, which is basically just a manual for Unix. For example, if you want to find out more about ls, type
sutro.hotwired.com[~/unixclass]% man ls
And you’ll get:ls(1)
NAME ls - list contents of directory SYNOPSIS ls [-RadLCxmlnogrtucpFbqisf1AM] [names] DESCRIPTION For each directory argument, ls lists the contents of the directory; for each file argument, ls repeats its name and any other information requested. The output is sorted alphabetically by default.
To quit man, type q.
As you travel around your host computer, you’re going to find there are lots of things you can’t do. You won’t have the freedom to move and edit files as you did in your user directory.
Remember ls from our basic commands? One of this command’s options (ls -l) gives you loads of information about files and directories.
Here’s an example:
sutro.hotwired.com[~/unixclass]% ls -l total 20 drwxrwxr-x 2 pam staff 512 Dec 5 09:34 one -rw-rw-r-- 1 pam staff 4233 Dec 5 09:35 one.html drwxrwxr-x 2 pam staff 512 Dec 5 09:34 two -rw-r--r-- 1 pam staff 4233 Dec 5 09:36 two.html
Here we have the contents of the directory unixclass, which contains two directories (one and two) and two files (one.html and two.html). The first column of letters determines who can read, write, or execute your files and directories. (I know it just looks like a bunch of letters, but give me a second and I’ll explain.)
The second list (pam, pam, pam, pam) tells who owns the file. In this case, it’s me.
The next column tells which group I was part of when I made these files. To find out what groups you belong to, type groups and your name, like this:
sutro.hotwired.com[~/unixclass]% groups pam infomgr staff prod edit
The fourth column shows the size of the files.
The fifth is the date and time the file was last modified; the sixth is the name of the file.
What the Heck is rwx?
As I mentioned before, these letters determine who can read, write, or execute your files. They break down like this:
r = read – you can read the file (more or less it)
w = write – you can write to the file (edit it with vi, Emacs, or Pico)
x = execute – you can execute the file (run a Perl program)
r = read – you can read the directory
w = write – you can create, move, rename, or remove files or directories
x = execute – you can search the directory
Take a look at this ls -l example:
drwxrwxr-x 2 pam staff 512 Dec 5 09:34 one/ -rw-rw-r-- 1 pam staff 4233 Dec 5 09:35 one.html
For every folder and file there are four sets of rwx. In the example above, they are easy to spot because they’re set apart by hyphens. The first set shows your permissions, the second is for a group, the third is for anything else (other), and the fourth is for everyone (all).
Now, to make things a little more complicated, each one is referred to by a single letter:
you = u group = g other = o all = a
To add permissions on a file or directory, you use +, and to remove them, you use -. Let’s try it out. We’ll remove group write privileges for this file:
-rw-rw-r-- 1 pam staff 4233 Dec 5 09:35 one.html
To do this, we must also use the chmod command to change the access mode of the file.
sutro.hotwired.com[~/unixclass]% chmod g-w one.html sutro.hotwired.com[~/unixclass]% ls -l total 19 -rw-r--r-- 1 pam staff 4233 Dec 5 10:59 one.html
To make a program executable for everyone, we’d do this:
sutro.hotwired.com[~/unixclass]%ls -l svensprogram.pl -rw-rw-r-- 1 pam staff 0 Dec 5 11:02 svensprogram.pl sutro.hotwired.com[~/unixclass]% chmod a+x svensprogram.pl sutro.hotwired.com[~/unixclass]% ls -l total 19 -rwxrwxr-x 1 pam staff 0 Dec 5 11:02 svensprogram.pl
To change who actually owns a file, use the chown command:
sutro.hotwired.com[~/unixclass]% ls -l svensprogram.pl -rwxrwxr-x 1 pam staff 0 Dec 5 11:02 svensprogram.p sutro.hotwired.com[~/unixclass]% chown sven svensprogram.pl sutro.hotwired.com[~/unixclass]% ls -l total 19 -rwxrwxr-x 1 sven staff 0 Dec 5 11:02 svensprogram.pl
To change which group can access the file, use the chgrp command:
sutro.hotwired.com[~/unixclass]% ls -l svensprogram.pl -rwxrwxr-x 1 sven staff 0 Dec 5 11:02 svensprogram.pl sutro.hotwired.com[~/unixclass]% chgrp infomgr svensprogram.pl svensprogram.pl - Not privileged
D’oh! Not privileged. Until you become a powerful Unix god, you will see this message many times. But keep at it, and deityhood may be closer than you think.
The most commonly available text editor is [[Tutorial:Vi Tutorial for Beginners|vi] (pronounced vee-eye), which I especially recommend for Mac users. You really only need to know a few commands to get started.
To start vi, simply type vi at the command prompt:
sutro.hotwired.com[~/unixclass]% vi myfile.html
and you’ll be instantly thrown into this crazy-looking environment with a ~ on every line. Don’t be afraid. The annoying thing about vi is that you have to tell it when you’re adding and deleting text. You can’t simply type and delete, you have to use the following commands:
- To add text – hit the esc key and the letter i (for insert)
- To delete text – hit the esc key and the letter x (for delete)
- To delete an entire line – hit the esc key and the letters dd
- To save your file – hit the esc key and type :w and then hit return
- To save and exit vi – hit the esc key and type :wq and hit return
Emacs is the favorite editor of many, but Macintosh users may tell you not to bother because they find it really irritating. PC users will probably offer a different opinion.
If you’re a beginner, pico is probably your best bet. Simply type in
and you’ll be thrown into the editor. All the commands are listed on the top and bottom of the screen. Be sure to save frequently.