Unix Tutorial and Command Reference

Not familiar with Unix? Never fear; here's a handy guide to all you need to know to get around in the Unix shell.

First you'll want to connect to the Unix shell by telnetting to the machine. If you're using Windows, you already have a telnet program installed. Go to Start>Programs>Windows Explorer. In c:\windows there should be a file called telenet.exe; click it to launch telnet.

Mac OS X users - you already have Unix underneath. Just launch the Terminal application (in /Applications/Utilities).

Getting Started

Once you've logged into the Unix host, you'll be in the shell. What you first see on your screen may look something like this:

Last login: Thu Jan 28 09:02:47 1999 from as2-dialup-133.io.com
You have new mail.

In this example, the % is called the "prompt". When you type, your typing will appear to the right of the prompt, and when you hit return, the shell will attempt to run the command you typed, then display another prompt.

One thing to remember when working in the shell: Unix is case sensitive. "CD" is not the same as "cd". Turn your caps-lock off, and leave it off while you work in the shell — all shell commands are lowercase.

All of the commands shown below are the actual command you should type at the Unix prompt. The basic command is shown in a fixed-width font, like so:


Optional arguments are shown in brackets following the command:

command [options]

Where optional arguments exist, they should be typed after the command itself, without the [] brackets.

All of these commands also have online documentation, called man (manual) pages. For more information about any of these, just type

man command

For example, man pwd will tell you more about the pwd command.

Figuring out where you are

Example filenames are given below as "filename". You should, of course, substitute "filename" with the name of the actual file you want to


Prints the current (working) directory, like so:

% pwd

Changing to another directory

cd [directory]

Changes the current working directory. To back up a directory, you'd do

cd ..

To change to a subdirectory in your current directory, you can just type the name of that subdirectory:

cd public_html

To change to some other directory on the system, you must type the full path name:

cd /tmp

Seeing what's here

ls [-options] [name]

List the current directory's contents. By itself, ls just prints a columnar list of files in your directory:

% ls
News                 letters              public_html
admin                lynx_bookmarks.html  scripts
bin                  mail                 tempo.el
biz                  moo                  tiny.world
bkup                 perl-mode.el         tmp
html-helper-mode.el  perlmods

Here are a few other options that can format the listing or display additional information about the files:

-a     list all files, including those starting with a "."
-d     list directories like other files, rather than displaying their contents
-k     list file sizes in kilobytes
-l     long (verbose) format — show permissions, ownership, size, and modification date
-t     sort the listing according to modification time (most recently modified files first)
-X     sort the files according to file extension 
-1     display the listing in 1 column

Options can be combined; in this example, we show a verbose listing of files by last modification date:

% ls -lt
total 94
drwx------   6 kira     kira         1024 Feb 28 19:42 admin
drwx------   2 kira     kira         1024 Feb 28 19:41 scripts
drwx------  13 kira     kira         1024 Feb 28 19:39 perlmods
drwxr-xr-x   2 kira     kira         1024 Feb 28 19:38 public_html
drwx------   7 kira     kira         1024 Feb 28 19:00 moo
drwx------   2 kira     kira         1024 Feb 26 18:45 mail
-rw-------   1 kira     kira           29 Feb 21 16:29 tiny.world
drwx------   2 kira     kira         1024 Feb 20 09:25 bin
drwx------   2 kira     kira         1024 Feb 14 01:29 tmp
drwx------   2 kira     kira         1024 Feb 12 13:40 letters
drwx------   2 kira     kira         1024 Feb  1 17:19 biz
drwx--x--x   2 kira     kira         1024 Jan  9 20:59 News
drwx------   2 kira     kira         1024 Nov 13 09:02 bkup
-rw-r--r--   1 kira     kira          592 Nov  8 18:12 lynx_bookmarks.html
-rw-rw-r--   1 kira     kira        23815 Oct 25 07:35 tempo.el
-rw-rw-r--   1 kira     kira        25802 Oct 25 07:35 perl-mode.el
-rw-rw-r--   1 kira     kira        27491 Oct 25 07:35 html-helper-mode.el

Also, you can specify a filename or directory to list:

% ls -l public_html/
total 1
-rwxr-xr-x   1 kira     kira          436 Feb 28 19:52 index.html

The verbose listing shows the file permissions of a given file:


Directories have a "d" in the first column; regular files have a "-". The remaining 9 characters indicate the owner, group, and world permissions of the file. An "r" indicates it's readable; "w" is writable, and "x" is executable. A dash in the column instead of a letter means that particular permission is turned off. So, "-rwxr-xr-x" is a plain file that is read-write-execute by the owner, and read-execute by group and world. "drwx------" is a directory that is read-write-execute by owner, and group and world have no permissions at all.

File and Directory Permissions

chmod [permissions] [file]

Changes the permissions of the named file. There are several ways to specify the permissions. You can use numbers, like so:

chmod 755 index.html

The first number translates to permissions by the owner. The second is permissions for the group. The third is permissions for everyone.

Number          Perms
  0              ---          no permissions
  1              --x          executable only
  2              -w-          writable only
  3              -wx          writable and executable
  4              r---         readable only
  5              r-x          readable and executable
  6              rw-          readable and writable
  7              rwx          readable, writable, and executable

A second way of setting permissions is with letters:

chmod u+rwx,go+rx index.html

u is the owner's ("user's") permissions; g is the group permissions, and o is "other" or world permissions. The + sign turns the stated permissions on; a — sign turns them off. So, if you want to change a file so that it's group writable, but not readable or executable, you'd do:

chmod g+w,g-rx filename

Directories should always have the "x" permission set, at least for the owner. If you accidentally unset a directory's x bit, you will no longer be able to do anything in that directory (and neither will the web server). If you do this to your home directory, you probably won't even be able to login. Also, a directory doesn't have to be readable for the web server to read and execute files within that directory. Only the files themselves must be readable. For security purposes, you should probably set your web directories to be mode 711, like so:

drwx--x--x   2 kira     kira         1024 Feb 28 19:52 public_html

This keeps other users from snooping around in your directory, while still allowing the webserver to call up your pages and run your scripts.

File Names

Unix filenames can't have spaces, slashes, or weird characters in them. (Or, sometimes they can, but this will make your life miserable, because referring to strange characters requires a backlash in the filename.) Also, file names are case sensitive, so if you create a script and upload it as "COUNTER.CGI", while your page is doing <!--#exec cgi="counter.cgi"-->, it won't work, because Unix can't find "counter.cgi" in your directory.

Creating Files

You can create files by editing them with an editor, or ftp'ing them into your directory. Most Unix systems include pico, a very simple text editor. To use it, just type

pico newfile.cgi

You'll be placed in the editor, where you can type new lines of text, and use arrow keys to move around the document. Pico offers a limited set of cut and paste utilities, which are viewable at the bottom of your edit screen. When you're through editing, just type control-X to save the file.

Other editors, such as vi and emacs, are also available, though they are not as easy to learn and use. Whole books have been written about these editors. If you're interested in using them, try the man pages first, then search the web; a number of good tutorial websites exists for these.

There's also a way you can create an empty file without editing it: the touch command.

touch filename

The main use of touch is to update the timestamp on a file; if you touch an existing file, it changes the last modification date of that file to now. However if the file doesn't exist, touch creates an empty file. This may be useful for creating counter data files or output logs:

touch outlog
chmod 666 outlog

Copying Files

cp [options] source dest

Copies the source file to the destination. The source file remains after this. Options:

-b    backup files that are about to be overwritten or removed
-i    interactive mode; if dest exists, you'll be asked whether to overwrite the file
-p    preserves the original file's ownership, group, permissions, and timestamp

Moving (Renaming) Files

mv [options] source dest

Moves the source file to the destination. The source file ceases to exist after this. Options:

-b    backup files that are about to be overwritten or removed
-i    interactive mode; if dest exists, you'll be asked whether to overwrite the file

Viewing Files

more filename
less filename

These two commands allow you to page through a file. less is often preferred because it allows you to back up in a file. Both commands scroll through the file, starting at the first line, and displaying one page at a time. Press the space bar to continue to the next page. In less, pressing "b" instead of the spacebar will backup to the previous page. A variety of other scrolling and searching options exist; consult the man pages for a detailed listing.

head [options] filename
tail [options] filename

head displays lines from the beginning of a file. If no options are given, the default is 10 lines. An optional argument of —lines can be used to specify the number of lines to display. For example, to list the first 5 lines of a file, you'd do:

head -5 filename

tail is similar, except it shows lines from the end of a file. Again, with no arguments, it shows the last 10 lines. tail also supports the —f option, which loops forever trying to read characters from the end of a file. This is especially useful for viewing log files that are constantly growing:

tail -f access_log

However, the only way to break out of a tail -f is to send an interrupt (usually control-C).

Searching for something in a file

grep [options] pattern filenames
fgrep [options] string filenames

grep and fgrep search a file or files for a given pattern. fgrep (or "fast grep") only searches for strings; grep is a full-blown regular-expression matcher. Some of the valid options are:

-i     case-insensitive search
-n     show the line# along with the matched line
-v     invert match, e.g. find all lines that do NOT match 
-w     match entire words, rather than substrings

An example: if you wanted to find all instances of the word "Fred" in a file, case-insensitive but whole words (e.g. don't match "Frederick"), and display the line numbers:

% grep -inw "Fred" fnord

There are a great many other options to grep. Check the man page for more information.

Deleting Files

rm [options] filenames

Deletes the named file(s). Options:

-f      force, delete files without prompting
-i      interactive — prompts whether you want to delete the file
-R      recursively delete all files in directories

Creating Directories

mkdir dirname

Creates the named directory. If a full path is not given, the directory is created as a subdirectory of your current working directory. You must have write permissions on the current directory to create a new directory.

Deleting Directories

rmdir dirname

Deletes the named directory. If the directory is not empty, this will fail. To remove all files from the directory, first do "rm -rf dirname".

Who's Online


Both of these commands give a listing of who's online. "who" generally only shows the login names, the time they logged in, and the host they logged in from. "w" gives the system uptime, along with a list of users, their login time, idle time, CPU usage, and last command.

Back to Top