The Basics Of The UNIX Operating System

Documentation Notes

All examples used in this document will be from the rci or eden systems. User input will appear in this typeface. System output will appear in this typeface. Text that is in italics represents place holders for specifics that you would supply. The notation <CTRL>d means you hold down the control key while typing the indicated character (in this case d). This document is intended to be viewed on the web and used as a self-paced tutorial, and presumes that you will be using either an eden or rci account. All commands listed in this tutorial are linked to a description of the command, simply click on the command to go to that description.

Class setup

Basic Metacharacters

Metacharacters are a group of characters that have special meanings to the UNIX operating system. Metacharacters can make many tasks easier by allowing you to redirect information from one command to another or to a file, string multiple commands together on one line, or have other effects on the commands they are issued in. The following table lists some of the metacharacters for the Rutgers default shell, more metacharacters are in "Basic UNIX Commands & Metacharacters". Shells are the user interface between people and UNIX, more information about shells is in "Intermediate Use of the UNIX Operating System".

Metacharacter

Description

<SPACE>

UNIX interprets a space as a separator not as a character.

*

A wild card character that matches any group of characters of any length, allowing a user to specify a large group of items with a short string. For example, to specify all the files that start with 'abc', you use abc*.

?

A wild card character that matches any single character. Thus ls ??? lists files in the current directory whose names are only three characters long, while ls ???.* lists those files whose name starts with any three characters, then a period, and ending with anything.

[..]

A set of characters that can be matched. Thus ls [a-c]*.??? lists all files that begin with a, b, or c and have a period followed by three characters and lpr [ad]* prints all files that begin with a or d.

!

Precedes a history substitution (see "man history").

;

Separates sequences of commands that are on the same line.

Practice 1

Editors

To create or change the contents of a data, text, or program file you use an editor. An editor is a program that reads in a file, lets a user modify it, and then, when instructed to, writes the updated copy of that file. The editors available on the Rutgers UNIX systems are full screen editors, which means they display a screen full of a file and allow you to alter any part that is on the screen. In order to change or add to the file, move your cursor to the appropriate location in the text and then use the editor commands to make the changes which will then appear on the screen.

When you are in an editor, you are actually working on a copy of the file, not the permanent file. Changes are made to the copy that the editor is maintaining in memory. When you are finished, instruct the editor to save the current working copy as the new permanent copy (or into a new file altogether). This means that if you exit the editor without saving the changes, the changes will be lost (sometimes this is desirable).

Though most Rutgers users choose Emacs as their editor, there are other editors. One advantage of Emacs is that it is available on most systems (UNIX or otherwise) here at Rutgers. Although different machines may have different versions of Emacs, the differences between versions are slight.

The best introduction to Emacs is the Emacs tutorial program; "teach-emacs". To begin the tutorial, use the command:

teach-emacs

or, from within Emacs, type:

<CTRL>h t

To edit a file with Emacs, type:

emacs filename

whether the file you name exists or not. If the file does not exist, it will be created the first time you issue a save command.

To save your changes type:

<CTRL>x <CTRL>s

To exit emacs type:

<CTRL>x <CTRL>c

If the computer does not know the kind of terminal you are using you will get the error:

emacs: terminal XXX isn't powerful enough to run Emacs

Call the NBCS Help Desk at (732) 445-HELP (4357) for help in determining your terminal type.

Command Line Editing

In the default Rutgers UNIX shell (the T shell) with the default editor set to emacs, commands may be edited and previous commands retrieved using control characters:

<CTRL>a

move the cursor to the beginning of the line

<CTRL>b

move the cursor to the left

<CTRL>c

abort the current command

<CTRL>d

if not at end of the command line, delete the character at the cursor
if at end of command line, list possible command (or file) name completions

<CTRL>e

move the cursor to the end of the line

<CTRL>f

move the cursor to the right

<CTRL>h

delete the character to the left of the cursor

<CTRL>k

erase from the cursor to the end of the line

<CTRL>l

clear the screen and display current line

<CTRL>n

bring up next command line

<CTRL>p

bring up previous command line

<CTRL>q

continue displaying to the screen (see <CTRL>s)

<CTRL>r

redisplay the current line

<CTRL>s

stop displaying to the screen (see <CTRL>q)

<CTRL>t

exchange the character to the left of the cursor with the character at the cursor

<CTRL>u

erase the current line

<CTRL>w

will erase from the cursor to the beginning of the line

<CTRL>y

yank back the last item erased by <CTRL>k, <CTRL>u, or <CTRL>w

<ESC> b

move the cursor to the left one "word"

<ESC> f

move the cursor to the right one "word"

<ESC> c

move the cursor to the right one "word", capitalizing while moving

<ESC> l

move the cursor to the right one "word", making lower case while moving

<ESC> u

move the cursor to the right one "word", making upper case while moving

Practice 2

File Access/Protection

UNIX allows you to control access to your files. If you or your start up files do nothing to change the default settings, no one else on the system will be able to read or execute your files. You may protect (or grant access) your files by the individual file or by entire directories. For someone to have access to one of your files, they first must have access to your directory. If you grant access to a directory, you can still restrict access to files.

Each file has protections for the three different types of users; owner, group, and others. The owner is the user who created the file. Someone can own a file in your directory, but you can always delete any file that is in your directory, even if it is owned by another user. Groups are set up by the system administrator. They represent users who have some common link, such as users in one class or one department. All existing groups can be found in the file /etc/group. Others are those users who do not fall into the owner or group category. When the "ls" command is used with the option "-l" the accesses are displayed, as in:

ls -l Project1
-rwxr-xr-x  2  jqsmith  users   512    Mar 12 12:02 Project1
        

The first ten characters is the permissions code (-rwxr-xr-x), next comes the number of links to this file/directory (2), the username (NetID) of the user (or owner) of this file (jqsmith), the name of the group whose members are affected by the group protections (users), this is followed by the size of the file in kilobytes (512), the last modification date/time (Mar 12 12:02), and the name of the file/directory (Project1). The permissions codes positions are:

Position

Character

Meaning

1

-

a plain file

1

d

a directory

1

l

a symbolic link

2 through 10

-

a particular access is not given

2, 5, 8

r

read access allowed

3, 6, 9

w

write/delete access allowed

4, 7, 10

x

execute access allowed

The letters are arranged in a certain way. The first is only used to give specific information about the file (see the above table). Then there are three groups of three characters. The first trio correspond to the user's rights, the next three are the group rights, and the last three are for all others. When a letter is present it means that category of user has that particular access to the file. A file that has the protection code "-rwxr-x---" is a plain file; the user can read, write/delete and execute it; people in the same group as the user can read and execute it; and everyone else can only see that the file exists (if given access to the containing directory).

The default protection given to new files is set by the "umask" command. If you want all your files to have the same default protection a "umask" command should be in your .login file (in the default set up files there is one). The use of the command "umask" is described in the "Basic UNIX Commands & Metacharacters" document.

Protections for existing files and directories can be set or changed using the "chmod" command. The "chmod" command has two modes, numeric and symbolic. The numeric mode and is used primarily by power users and the symbolic mode by general users.

In the symbolic mode the chmod command syntax is chmod usersACTIONprotections filename. Where users would be one or more of ugoa (u for user, g for group, o for others, a for all types); ACTION would be one of +-= (+ for add, allow access; - for delete, prevent access; = for set exactly to), protections would be one or more of rwx (see above), and filename would be the name of the file to be affected. If different user types are to be affected differently separate the usersACTIONprotections codes for each by a comma. The command:

chmod g+r,o-rwx file.name

adds read access for the group and removes all access for others to the file "file.name".

In the numeric mode protections are set via a three digit number "chmod ### filename". The first digit for user protections, the second for group protections and the third for others. Each digit is a number from 0 (zero) to 7 that is a sum of the numeric representation of each allowed access, 1 for execute, 2 for write/delete and 4 for read.

The most common user settings are:

7

read, write/delete and execute access

6

read and write/delete access

The most common group and other settings are:

5

read and execute access

4

read access

0

No access permitted

The command:

chmod 750 filename

sets the file to be fully accessible by the user, read and execute access for group members and no access by anyone else. The command:

chmod 644 filename

denies the user execute access (not needed for most files) and allows everyone else only to read the file. The numeric mode allows a shorter number of keystrokes to effect various settings but it is easier to make mistakes that are hard to recover from.

The use of the command "chmod" is described in more detail in the "Basic UNIX Commands & Metacharacters" document.

Directories Protection/Permissions

As previously mentioned, in order to be able to access a file, a user must have the appropriate permission for the file. In addition she/he must also have read and execute access to the directory that contains the file. Typically access to directories are set up as follows "drwxr-xr-x", which translates to: the owner can do anything with the files in this directory, all other users can read and search (execute) this directory, and thus read or execute files where such access is granted. For instance, in the "r-x" above the "r" allows other users to look at the files in the directory, the "x" allows them to look into the directory, and since "w" is missing other users can not create new files in the directory. If you want to have a totally private directory, set the protections to "drwx------". In this case no one else could look at any files in the directory. It is advisable for you to set group and other protections to deny write access.

Practice 3

Mail

UNIX has mail facilities for sending messages to other users and reading those sent to you. The RUCS supported UNIX mail package is Pine which is started via the command:

pine

Another package available with little to no support is mail. The reading of mail through mail clients on microcomputers is supported via the IMAP and POP protocols. To get information on Pine see the NBCS document "Getting Started With Email Using Pine" or view the web page http://www.washington.edu/pine/tutorial/index.html

On-line Documentation

In addition to the man pages there are the web based documentation that NBCS (and others) have produced.

All NBCS documentation is available from the NBCS Help Desk, the Campus Computing Centers, or at the NBCS documentation home page http://www.nbcs.rutgers.edu/newdocs.

Also available in the web based "Frequently Asked Questions" (and answers) on http://faq.rutgers.edu.

For More Information

Questions should be directed to the NBCS Help Desk, Room 013, Hill Center, Busch Campus, (732) 445-HELP (4357). You can also send electronic mail to the address help on any RUCS system (e.g., help@rci.rutgers.edu or help@eden.rutgers.edu) or you can visit the NBCS FAQ at http://faq.rutgers.edu.

Copyright © 2004 Rutgers, The State University of New Jersey, NBCS, Help Desk. All rights reserved.

7/26/04 Dan Koft, Author

Rutgers University Computing Services

UNIX 2