UNIX: A Basic Introduction

A Basic Introduction to the UNIX operating system.

Topics covered in this section include:

Many, many books have been written about UNIX; this section is only a short introduction. See Section References, for a list of references.

The UNIX User Interface: The Shell

The UNIX user interface is called the shell. The shell displays a prompt and waits for you to type a command. A command either is built into the shell, or it refers to a program somewhere on the computer disk. The shell tells UNIX to execute programs and then informs you of the results. See the section on unix shells. The tch shell (a modified C-shell) is the default shell at KCCF.

Case Sensitivity

All UNIX command names and file names are case-sensitive. Most UNIX command names are entirely in lower-case letters.

Getting On-line Help

You can see documentation on any UNIX program or command by typing:

$       man name_of_command
section Getting Help, for more explanation of the man command.

Changing Your Login Password

Type the command passwd. You will be prompted for your old password, and then for a new password. Your passwords will not appear on the screen as you type them; this is a security feature. You will be asked to repeat the new password, to make sure you typed it in correctly. You are done!

A valid password is between 6-8 characters long. If your password is longer than 8 characters, only the first 8 are significant.

UNIX Files: The Basics

The set of files on UNIX and their relationships to one another is called the UNIX filesystem.This filesystem is described (loosely speaking) by a tree (remember your graph theory?) The leaves of the tree are ordinary files. The internal nodes of the tree are called directories.Think of a directory as a special file that contains pointers to other files and directories. A directory that is a child of another directory is called a subdirectory. To be more precise, the filesystem differs from a tree in that a file can be in two or more different directories (kind of like a leaf on two different branches). Even directories may be in more than one place, but this option is only open to the System Administrator.

In addition to plain text and data files, almost everything in UNIX is considered to be a file: executable programs, directories... even your terminal and your keyboard.(By default, your terminal is called "standard output,"and your keyboard is called "standard input.") These can be reset to correspond to any file, or from any file back to the defaults (of keyboard and screen).

Executable Files

A "command" or "program" is executed on UNIX by typing the name of the file that contains the executable code, optionally followed by some optionsand some arguments,and then pressing RET. For example, to copy file `file1' to a new file `file2', you type:

$       cp file1 file2
The name of the program is cp. file1 and file2 are arguments to the cp program. There are no options in the above command. Options usually look like a single letter preceded by a dash; for example, "-p". Options are used to affect the behavior of a program.

See section Common UNIX Commands, for a table of common UNIX commands. Please read this table and try using the commands!

File Names

The root directory of the filesystem tree is simply called root,and it is represented by a single slash (`/'). The complete name for any file is given by the path from the root to that file, written from left to right. To create the name, start with a single slash for the root. Then append the names of all directory nodes from the root to the desired file, adding another slash after each directory name. Finally, append the file name itself.

For example, if you wanted to find the name of the file myFile, which a child of directory myDir, which is a child of the directory anotherDir, which is a child of the root directory, the full name is:

/anotherDir/myDir/myFile

This is called the absoluteor fully qualified filename.However, you usually refer to a file by its relative filename, relative to some other directory in the tree. We will learn how to do this later (see section Relative Names Of Files) after you have learned about directories. section Directories contains a discussion of directories.

UNIX file names are case sensitive. This means it makes a difference whether you use lowercase alphabet (such as "a") instead of an uppercase alphabet (eg. "A") and vice-versa. The file names "myFile," "myfile," and "MyFILE" refer to different files.

Listing Your Files (ls)

To see the names of all the files in your current directory, type ls. A file has many attributes associated with it: its name, its size, the name of its owner, the date it was last modified, and so on. To see the full attributes of a particular file, type ls -l name_of_file. Suppose your filename is "myFile" and you typed ls -l myFile. You would see something like this:

$   ls -l myFile
-rw-r--r--  1 smith         28745 Jun  2 15:46 myFile
Reading from right to left, we see:

For full details on the many ways you can look at your files and their attributes, type man ls.

Directories

A directory is a file that points to the locations of other files and directories. It is an intermediate node in the filesystem tree.

Your Current Directory

Whenever you are using UNIX, you are considered to be working "in" a particular directory. This is called your current directory orworking directory. You can find out the name of your current directory using the pwd ("print working directory") command.Simply type pwd.

Changing Directories

You move from directory to directory using the cd ("change directory") command. This allows you to move up and down the directory tree. If you type cd name_of_directory, this makes the directory name_of_directory your current (working) directory.

NOTE: You must have "execute" permission on a directory in order to change to that directory. However, you will need "read" permission as well if you want to list the contents of the directory. section File Protection: chmod, to learn about the chmod command and directory permissions.

Your Home Directory

Every user has a home directory. This directory contains all of your personal files and subdirectories. If you type cd with no directory name after it, you are moved to your home directory. To find out the name of your home directory, type cd followed by pwd.

You are considered the owner of all files and subdirectories in your home directory. This means that you have total, unrestricted access to these files.

Four Special Directories

There are four directories that people refer to all the time. So, UNIX and the shell provide special symbols for referring to them. (section The TC Shell (tcsh), for a discussion of the shell.)

Relative Names Of Files

Instead of referring to a file by its absolute, or fully qualified, name, you can use its relative name. This name depends on which directory is your current directory.

The Rule Of Filenames is this: If you refer to a filename that begins with a slash, then it is considered to be an absolute filename. Otherwise, it is considered to be a relative filename, and the name of your current directory is "attached" to the beginning of the filename.

For example, if your current directory is /usr/foo, and you refer to the file myDir/myFile, then the actual file you are referring to is /usr/foo/myDir/myFile. Here are some more examples, using the four special directories listed elsewhere (section Four Special Directories):

The last example, ~bob/foo, does not begin with a slash, but it is still an absolute filename. Why? Remember that the tilde `~' is a shorthand for the absolute name of your home directory!

File Protection: chmod

Every file on UNIX has a mode or protection.

A file may be readable, writable (deletable), and executable, in any combination. In addition, a file can be accessible to a single user, a group of users, or all users.

How do you find out the mode of a file? Type ls -l name_of_fileand look at the protection mode. See section Listing Your Files (ls), for more information about ls. It is a field 10 characters long. The first character tells you what kind of file it is. Plain files have a dash ("-") there, and directories have a "d" there. (There are other types of files too.)

The remaining 9 characters are really three sets of three characters. The first three represent the read ("r"), write ("w"), and execute ("x") access of the file by its owner. The second set represents the read, write, and execute access of the file by a chosen group of people. The last set represents the read, write, and execute access of the file by all users (the "world") on your machine. A dash ("-") in one of these locations represents "no access."

Changing the protection of a file is done using the "change mode" command, chmod. Imagine a sequence of nine bits,broken into three sets of three. These bits correspond exactly to the three sets of three characters (r, w, and x) in the previous paragraph.

The first set of three bits represents you. The second set represents a specific group of people that you designate. (The System Administrator can make a group for you.) The last set represents everybody else on the machine. In each set of three bits, the first represents read permission, the second write permission, and the third execute permission. A "1" bit means that permission is on, and a "0" bit means that permission is off ("no access").

Each set of three bits can be represented by a single digit between 0 and 7 (octal). So, one form of the chmod command looks like:

$       chmod three_octal_digits filename
There are other forms of the chmod command as well. See the man page for chmod. Some common uses of chmod are:

NOTE: You need "execute" permissions on a directory in order to enter it with the cd (change directory) command. However, you will need "read" permissions as well if you want to list the contents of the directory.

Default File Permissions (umask)

Whenever you create a new file, its protection mode is set to some default value, called your "umask". Normally, that value is "755", which means that directories will get created with mode 755, and files will get created with mode 644 (no execute). If you wish to change this default value, use the command umask. To change it, type umask three-octal-digits, where three-octal-digits is the complement of the mode you want. For example, to set your default mode to 755, type umask 022. To see your default protection value, simply type umask.

umask is really a shell command, not a UNIX command. Type man tcsh to learn more about it. section The TC Shell (tcsh) for more details about the shell.

If you change your umask value, this new value lasts only until you log out. To make it permanent, put a umask command in your `~/.cshrc' file. see Thomas C Shell, for more information about your `~/.cshrc' file.

Security Concerns

It is important to do your best to ensure that your account is secure. This means no one must be able to use your account. It is also important to ensure that other users get no more access to your files and directories than is necessary.

Password Choice

You must choose your password such that it is very difficult to guess, even if someone is prepared to try very hard. This is to prevent others from using your account. See section Changing Your Login Password about changing your password. You should avoid using You should not make any slight modification (such as a permutation or a mutation) to a password that falls into any of the above categories - it can be guessed.

So what makes a good password ? A misspelt word, embedded with one or more extra characters (numbers are good), and typed with weird capitalization (if you can consistently type it fast). You can vary this theme by concatenating two such wOr2ds or interleaving two or more such words. Just remember only the first 8 characters are significant.

You should also change your password fairly regularly (ie. don't wait until you suspect that someone knows it), but make sure you don't do it so often that you forget the new one. Tell no one your password -- not even someone who claims to be the System Administrator! The System Administrator never needs to know your password. The security of your computer account may be jeopardized if you tell anyone your password.

File Permissions

See section File Protection: chmod to find out how to set the permissions of a file. This subsection discusses the implications for the permissions you give to a file.

You should generally not allow others to have write access to your home directory and its child directories. But you may want to allow read and execute permission so that others can read (list) some of your directories and also cd to some of your subdirectories. If you don't want anyone to read the files in a directory, you have to remove the read permission, of course.

Shell Scripts

You can also execute a set of UNIX commands by putting those commands into a text file (just as you would type them), and making that text file "executable." This kind of file is called a shell script. (Some other computers call them "batch files.") To make a shell script file executable, type:

$       chmod +x filename
See section below for Writing Shell Scripts, for more information on writing shell scripts.

Writing Shell Scripts

A shell script is a text file containing UNIX and shell commands. This file is made executable by using chmod (see section File Protection: chmod). You can then make the shell execute all the UNIX commands in the file by typing that file's name.

By default, the Bourne shell (`/bin/sh') is used to execute a shell script, regardless of which shell you use regularly. If you would like another shell, or even another program, to execute your shell script, you must put a special line at the top of your shell script:

#!full_name_of_program

All lines in the file after this one will be given as input to that program. For example, if you want the C shell (`bin/csh') to execute your script, you must put this as your first line in the file:

#!/bin/csh

Shell scripts may contain more than just UNIX commands. They may also contain control structures such as loops, conditionals, and jumps. Writing a shell script is like writing a very high-level computer program. Each shell has its own syntax; consult the man page for that shell to find out proper syntax.

Two very important shell scripts that get executed every time you log in (if you use csh) are your files `~/.login' and `~/.cshrc'. See see Thomas C Shell, for more information about your `~/.cshrc' file.

Common UNIX Commands

Here is a brief list of common UNIX commands. Many of them are covered in other sections of this manual. A typical UNIX command looks like:

$       `command_name' options arguments

All of the commands listed below have different options you can use. Type man command_name for information on any command. Type man -k keyword for a list of commands dealing with keyword. (The -k is an option for the man command.)

Processes

Every program that you ever execute is called a process while it is running. Every process has a unique integer called the process ID number or PIDassigned to it. You use the pscommand ("processor status")to see what processes are running, and to find out the PID of a particular process.

To see what processes you are running, type ps. Some processes, such as shells, do not show up unless you type ps -x. To see what processes everybody is running, type ps -ua or ps -uax

You can kill (terminate) a running process by typing the command kill PID, where PID is the process ID number of that process. If this command doesn't work, try kill -9 PID.

Type man ps to learn more about processes and ps.

Environment Variables

A variable is a word that stands for a particular value. An environment variable has a value that is known to your login process. Any UNIX program that you run has access to this variable.

You declare environment variables by giving them values with the setenv command. Type:

$       setenv variable_name value
You do not need to specify a type for your variable. value may be an integer or a string.

To refer to the value of a variable, precede its name with a dollar sign. For example, to refer to the value of variable "foo", use "$foo". Some variables have special meaning to your login; for example, the value of "HOME" is your home directory. To see the values of all your environment variables, type printenv.

The scope of an environment variable is your login process. This is different from a shell variable, whose scope is just the shell in which it was declared. Many people run only one shell during their login, so they might not notice a difference between shell variables and environment variables. See  Shell Variables in TC shell, for more information on shell variables.

Environment variable values disappear when you log out, except for those variables that are declared for you (such as "HOME"). To make an environment variable value permanent, add setenv commands to your `~/.chsrc' file. See section Tailoring UNIX The Way You Like It for an introduction to your `~/.chsrc' file.

Tailoring UNIX The Way You Like It

Every time you log in, all commands in your file `~/.chsrc' (~ is your home directory) are executed. You may add any UNIX commands you like to this file. Take a look at your current `~/.chsrc' file to see what happens every time you log in.

Realize that changes to your `~/.chsrc' file do not take effect until `~/.chsrc' is executed. You can execute your `~/.chsrc' file by logging out and then logging back in, or by typing source ~/.chsrc.

There is another file, called `~/.logout', that executes every time you log out. You may also put commands into your `~/.logout' file.

See Thomas C Shell, for more information about tailoring your UNIX account. It discusses the `~/.cshrc' file, which is another way to customize UNIX for yourself.

References

There are hundreds of UNIX references. When choosing a UNIX book, you should remember that there are two different types of UNIX: "Berkeley" and "System V". ULTRIX and Sun UNIX are based on Berkeley UNIX. System V is developed by Bell Labs. IRIX is based on UNIX System V.

Also, try to choose a recent book. UNIX has changed a lot over the years.

One well-known book is The UNIX Programming Environment by Brian Kernighan and Rob Pike (Prentice-Hall, Inc., 1984).