100% found this document useful (1 vote)
264 views

Getting Started With Unix

Uploaded by

Thaval
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
264 views

Getting Started With Unix

Uploaded by

Thaval
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

- Training

UNIX, Getting Started with


Information Technology Services - Training
The University of Texas at Austin
http://www.utexas.edu/its/training
e-mail: [email protected]
© 2002

Instructional Technology Services

The University of Texas at Austin

September 23, 2002

The University of Texas at Austin Page 2


Training September 23, 2002
Table of Contents
Why use UNIX? .............................................................................................................. 6
Logging in, Logging out and Passwords ......................................................................... 7
Log in ........................................................................................................................... 7
Changing passwords..................................................................................................... 7
Changing Information in the Personal Information Block........................................... 8
Logging Out ................................................................................................................. 8
UNIX Command and File Specification ......................................................................... 8
Listing Files - the ls command ..................................................................................... 9
File names .................................................................................................................. 10
Working with command options ................................................................................ 11
File Management ........................................................................................................... 12
Displaying the contents of a file - the cat command .................................................. 12
Displaying output one page at a time - the more command....................................... 12
Directories .................................................................................................................. 13
Changing directories .................................................................................................. 14
Determining your working directory - the pwd command......................................... 15
Pathnames................................................................................................................... 15
Making directories - the mkdir command .................................................................. 16
Copying Files - the cp command................................................................................ 16
Copying files to other directories............................................................................... 17
Renaming and Moving Files - the mv command ....................................................... 17
Deleting files - the rm command................................................................................ 18
Removing directories - the rmdir command .............................................................. 18
Filters.......................................................................................................................... 18
Sorting lines in a file - the sort command............................................................ 18
Searching for a text pattern - the grep command ................................................ 19
Word count - the wc command............................................................................ 20
Protecting Your Files and Directories........................................................................ 21
File and Directory Protections............................................................................. 21
Changing File Protections - the chmod command............................................... 22

The University of Texas at Austin Page 3


Training September 23, 2002
Changing Directory Protections .......................................................................... 24
Setting the Protection Default - the umask command ......................................... 25
Printing .......................................................................................................................... 26
On-line Documentation - using the man command....................................................... 27
Working with the Shell.................................................................................................. 29
Shell Duties ................................................................................................................ 30
Processes .................................................................................................................... 30
Environment Variables............................................................................................... 31
The Path Variable....................................................................................................... 32
Standard Input/Output and Standard Error ................................................................ 32
I/O Redirection........................................................................................................... 33
Pipes ........................................................................................................................... 34
Sequential Commands................................................................................................ 34
Concurrent Execution................................................................................................. 34
Standard Process Control ........................................................................................... 35
csh - Job Control ........................................................................................................ 35
csh - History ............................................................................................................... 36
Event Selectors........................................................................................................... 37
csh - Aliases ............................................................................................................... 37
Shell Procedures......................................................................................................... 38
Creating Your Own Commands ................................................................................. 38
Setting Terminal Types .............................................................................................. 38

The University of Texas at Austin Page 4


Training September 23, 2002
UNIX, Getting Started with
An operating system manages the use of hardware resources, the storage and recall of
data, controls the flow of data, and provides an environment for application programs.
The UNIX operating system offers an interactive, multi-user, multitasking environment
for users and provides for the creation and management of processes, the file system, and
communications. UNIX can be installed and used on many different types of computers.
The UNIX operating system can be used on a CRAY supercomputer or on a
microcomputer in your home. The beauty of UNIX is its portability and consistency
across many different platforms. This course is tailored to cover important commands
used for UNIX-based Web servers.
Objectives
After completing the course, you should be able to:
• Connect to and log in to a ITS UNIX system.
• Perform basic file management: creating directories and copying and deleting files
• Print a File
• Search for files on the system with whereis and find.
• Count items with wc command.
• See who is logged on the system.
• Sort files and other input.
• View and edit file permissions with ls and chmod.
• Create regular expressions and search for text using grep.
Workbook Conventions
The following conventions are used in this workbook:
Names of files and directories appear in bold letters with the first letter capitalized (e.g.
Myfile.doc).
Keyboard shortcuts are represented as <Ctrl C>. This means hold down the Ctrl key and
press the letter C.
All text to be input by student appears in bold type. (e.g.The University of Texas at
Austin.)
A series of commands is typed in as follows:
Select File > New
Class exercises appear in boxes like the example below.
Exercise:
Replace this line with the first step of your exercise.
Replace this line with the second step of your exercise.
Replace this line with the third step of your exercise.

The University of Texas at Austin Page 5


Training September 23, 2002
UNIX, Getting Started with

Why use UNIX?


UNIX may be used for many different types of computing activities. It may be used to
develop your own programs, to access many academic tools, or communicate with users
on other systems. A list of commonly used UNIX applications is provided below.
Task Description Application
Mail Each UNIX system provides you with an email address. pine
Your email address is: elm
mail
username@machinename
So, the user pcarter on the host:ccwf.cc.utexas.edu would have
an email address of
[email protected]
World Wide Web Not all UNIX systems have Web browsers installed. Netscape
(Requires use
of X-
Windows)
Lynx
Editors Editors are used to create and/or edit text files. You may use an vi
editor to compose a letter, make a list of things to do, or to write emacs
source code for C++, HTML, PERL etc…. pico
USENET/Newsgroups USENET is a collection of electronic discussion forums tin
available via the Internet. You may access the USENET trn
newsgroups via your UNIX account. This will allow you to pine
read and post messages to discussion groups.
Statistics/Higher Many faculty, staff , and students need access to applications Maple
Mathematics for specific academic and research purposes. Our UNIX SAS
systems provide access to a variety of different software IMSL
applications. SPSS
Ingres
File Transfer You may transfer files between your UNIX account and other Kermit
network connected systems, such as your home or office ftp
computer (if the machines are connected to the Internet).
Program Compilers/ Our UNIX systems offer access to a number of compilers and C++
scripting languages. You may write your own programs and or FORTRAN
Scripting Languages
scripts. C
Pascal
Text Processing We also provide access to a number of text formatting and Troff
typsetting tools. TeX
LaTeX

The University of Texas at Austin Page 6


Training September 23, 2002
Logging in, Logging out and Passwords
The most basic operations you will perform on any UNIX system are logging in and
logging out. This section discusses these operations and good password maintenance.

Log in
The first time you log in on a UNIX systems, you will be asked for your username and
password. At the login: prompt, type in your username, then press the <RETURN>
key.

login: ifaa123
You are then prompted for your password as follows:

Password:
Type the password issued to you with your user number then press the <RETURN> key.
Your password will not be displayed. UNIX is case sensitive so type lower case and
upper case characters accordingly.

Changing passwords
Passwords should be changed on a periodic basis to protect your files and your user
number. To change a password, type the following command at the command prompt:

% passwd
You will be prompted first for your current password, then twice for a new password (
the second prompt provides verification). In subsequent logins your old password will
not be accepted.
Choosing good passwords - basic rules of thumb
When selecting a password, the following guidelines should be followed:
• Choose a password that is meaningful only to you, something that will not be
easily guessed.
• Use 6 to 8 characters, or longer if permitted.
• Mixing case or inserting digits in the middle of your password makes it difficult
for someone else to guess.
• Choose a password that you can remember.
• Do not choose your initials or nickname.
• Do not choose an ordinary word found in the dictionary.

The University of Texas at Austin Page 7


Training September 23, 2002
Changing Information in the Personal Information Block
If you did not supply information in the first log in session, or if you want to add, delete
or revise information, use the chfn command.

% chfn
The chfn command displays one line of the personal information at a time. To leave the
information as it is, press <RETURN>. To change the information, type the new value
and press <RETURN>. To leave an item blank, type none and press <RETURN>.
After you have responded to all the prompts, chfn displays the message "Updating user
information" and exits.
Note: This option is not currently available on all UNIX systems.

Logging Out
To end your session, type logout at the prompt.

% logout

UNIX Command and File Specification


A UNIX command is composed of a command name, options and arguments. The
command name and its options and arguments must be separated by "white space".
White space is defined to be any number of spaces or tabs.

% commandname [options] [arguments]


[ ] indicate that this part of the command line is optional. The optional parts of a
command line vary from one command to another and depend on the use of the
command.
• The commandname is always listed at the beginning of the command line.
• An option changes the default behavior of a command. An option may give
additional information or it may change the way in which the information is
displayed. Options follow the command and usually begin with a "-" (the minus
character).
• An argument can be a filename, a directory name or a device name and is
sometimes optional. Arguments are normally listed last on the command line.
Some command lines may include several arguments.
In the examples below, the list files command, ls, is used with the -l option and
various arguments.

The University of Texas at Austin Page 8


Training September 23, 2002
List the files in the current directory:

% ls
List the files in the current directory in long format (the -l option has been added):

% ls -l
List the file named test.doc in long format (a specific argument has been added):

% ls -l test.doc
List the files named test.doc and unix.class in long format:

% ls -l test.doc unix.class
Multiple commands may be entered on the same line, but the commands must be
separated by semicolons. Command lines may contain as many as 1024 characters.
UNIX commands usually do not provide notification of successful completion of a
command; notification occurs only on an error.

Listing Files - the ls command


The ls command is used to list the names of the files contained in a directory. To
execute the ls command, type ls at the prompt.

% ls
The system responds with a list of files contained in your current directory. In the BSD
version of UNIX the default is to list files in the ASCII alphabetic order.
Exercise:
1.Use the ls command to display the files in your working directory.

The University of Texas at Austin Page 9


Training September 23, 2002
File names
A file is a collection of related data. A file might contain a mail message that you have
composed or an executable program. Every file must have a name.
Filenames in most versions of UNIX can contain up to 255 characters, but older versions
may limit the length of a filename to 14 characters. General rules for composing
filenames are listed below.
• Case sensitive.
• All characters are allowed except the / character.
• Not all characters are good choices.
• Recommended characters:
◊ letters (a-z and A-Z)
◊ numbers (0-9)
◊ period (.)
◊ underscore (_)
By convention, certain suffixes indicate specific file types to some UNIX utilities. End
file names with these characters only when you want to identify the files as follows:
File Extension File Type
.c A file containing a C language source program
.C A C++ source file
.f A file containing a FORTRAN language source program
.h A C language include file
.o Object code (output from a compiler)
.p A Pascal source file
.Z A compressed file
.tex A TeX or LaTex source file
.html A file containing html source code
.perl A file containing a perl source code
.cgi Common Gateway Interface script or program
.gif Graphics file
.jpg Graphics file

The University of Texas at Austin Page 10


Training September 23, 2002
Working with command options
Options are used with commands to select a specific command parameter. For example,
an option used with the ls command may give additional information or it may change
the way in which the information is displayed. The -l option used with the ls
command lists the contents of a directory in long format.

% ls -l
The ls command has several additional options. The -rc option lists files in reverse
order of creation date. Other options for the ls command are listed below:
-a List all hidden files, those filenames that begin with a . (period)
-R Lists entries recursively, includes current directory and all subdirectories.
-l List in long format, includes file permissions, owner, size in bytes and time of last modification
for each file.
-F Lists files with a symbol used to denote file type (a / character following a file name denotes a
directory file).
-r Reverse the order of the sort, to reverse alphabetic order or reverse order of creation, as
appropriate
-t Sort by time modified

Files beginning with a "." (the period character) are called hidden files. These files are
used to configure your environment and to set the default behavior of various utilities and
application programs. An example is the .login file which contains commands that are
read and executed each time you log in. Another hidden file is the .mailrc file which is
used to configure and change the default behavior of the mail program. Hidden files are
not displayed by the ls command unless the -a option is used.
Exercise:
1.Use the ls command with each of the options listed above to display the
contents of your working directory. Use the information displayed to
answer the following questions:
Which file was modified last?
What is the name of the largest file?

2.Use the ls command with the -l option to list the following files:
test.doc unix.class neon students.doc

The University of Texas at Austin Page 11


Training September 23, 2002
File Management
This sections covers the basic skills of creating and managing directories and basic file
management.

Displaying the contents of a file - the cat command


The cat command (derived from concatenate - to connect in a series) is used to display
the contents of text files. To use the cat command, type cat filename. The entire
contents of filename will be displayed on your screen.

% cat unix.class
% cat unix.class test.doc project_plan

Exercise:
1.Use the cat command to display the contents of the file named
unix.class.
2.Execute the cat command only one time to display the contents of the
files named test.doc, short_course.1 and project_plan.

Displaying output one page at a time - the more command


The more command is used to display the contents of a file one screen at a time. The
more command is very useful when viewing the contents of large files. To use the
more command, type more filename. The notation ---More --- and a percentage of
the file displayed is seen at the bottom of your screen.

% more test.doc
• To advance to the next screen press the space bar.
• To advance only one line, press the <Return> key.
• To exit the more command, type q.

The University of Texas at Austin Page 12


Training September 23, 2002
The more command has an entire control language that can be used while displaying any
file. Listed below are the most common control keystrokes used with the more
command.

Keystroke Action
<Return> Display next line of text
d Scroll forward lines [11] lines (Default is 11 lines.)
f Scroll forward one screenful
b Scroll back one screenful
<interrupt> Quit
q Exit from more
<space bar> Next screenful of text
h or ? Access the on-line help information

Exercise:
1.Use the more command to control the display of the file named
test.doc.
2.Execute the more command only one time to display the contents of the
files named test.doc, short_course.1 and project_plan.
3.View the contents of the files listed in step 2 by using both the space bar
and the <Return> key to advance the screen output.
4.Use each of the keystrokes listed above while displaying the contents of
the files named test.doc and unix.class.

Directories
A directory is a file that contains information that is used by the system to find other
files. When you log in to a UNIX machine you are automatically placed in a directory
called your home or login directory. The home directory is created for you when you set
up a computer account with ITS. This directory can be used to organize your files.
The directory that you are using is referred to as your working directory. When you first
log in, your login directory and your working directory are the same. If you move to
another directory using the appropriate commands, the new directory becomes your
working directory.

The University of Texas at Austin Page 13


Training September 23, 2002
A directory that is beneath another directory is referred to as a subdirectory. You can
create or delete subdirectories from your home directory as needed. The directory above
a subdirectory is called the parent directory of the subdirectory.
The UNIX file system organizes directories in a hierarchical structure. The structure can
be described as an inverted tree with the top level called the root. The root is always
signified by the initial / character. A set of directories and files make up the second level
and another set of directories and files make up a third level and so on. Below is a figure
that shows a hierarchical directory structure.
Root

bin etc usr tmp lib home

emx ccix ccwf sirius

u0 u1 u2 u3

cc

username

Changing directories
To change directories, use the cd command. For example, to move into the subdirectory
called bin (in your home directory), type cd bin.

% cd bin
In the UNIX operating system, a naming convention has been established to easily
identify your current directory and its parent directory. A single period or dot (.) refers to
the current directory. Two periods or dots (..) refer to its parent directory. The
command cd .. moves up one level in the directory hierarchy.

% cd ..
To go to the root directory, type cd /.

% cd /
To return to your home directory from any location, type cd.

% cd

The University of Texas at Austin Page 14


Training September 23, 2002
Exercise:
1.Change from your home directory into the subdirectory named bin.
2.Move back up to the parent directory using the cd .. command.
3.Move to the root directory using the cd / command.
4.Move back to your home directory using the cd command.

Determining your working directory - the pwd command


To determine the directory where you are currently located, use the pwd (print working
directory) command. UNIX responds with the pathname of your current directory
location. The pathname specified by the pwd command lists your location in relationship
to the root and lists each directory that is in the path from the root to your current
location. Each directory is separated from the next by the / character.

% pwd
/home/ccwf/u0/cc/username

Exercise:
1.Use the pwd command to determine your current directory.

Pathnames
Each file and directory has a pathname that uniquely identifies that specific file.
Anywhere you can use an ordinary file name, a pathname can be used. A pathname that
specifies the path from the root to your working directory is called an absolute pathname.
An absolute pathname is always read and written left to right, with the root (designated
by the / character) always starting the pathname. The other type of pathname is a relative
pathname. A relative pathname specifies the name of a file or the path to a directory
starting at your current working directory. A relative pathname never begins with /.
Absolute pathname: /home/ccwf/u0/cc/username
Relative pathname: bin/email.course
Because pathnames are unique, it is possible to have two directories or files with the
same name in two different directories. It is not possible to have two directories or files
with the same name in the same directory.

The University of Texas at Austin Page 15


Training September 23, 2002
Exercise:
1.List the absolute pathname for your current working directory.
2.List the relative path to the bin directory located one level under your
home directory.
3. Find two directories named bin. List the pathnames for each on the
lines provided below.

Making directories - the mkdir command


Directories can be created using the mkdir command. As you increase the number of
files that you use, you will find directories can be very useful in organizing and
maintaining your files. To create a new directory, type mkdir directoryname
where directoryname is the name of the new directory.

% mkdir research

Exercise:
1.Create the following directories using the mkdir command.
research programs documents book correspondence

Copying Files - the cp command


The cp command is used to copy files. When modifying a file, you may wish to make a
copy of the file to keep as a backup. The cp command creates a new copy of the existing
file and leaves the original file intact. To copy a file, type
cp sourcefile destinationfile

cp unix.class training.unix

Exercise:
1.Use the cp command to copy unix.class to a new file named
training.unix.
2.Copy the file named chap1.1 to a new file named book_review.
3.Copy the file named roast to a new file named beef.

The University of Texas at Austin Page 16


Training September 23, 2002
Copying files to other directories
Files can be copied from one directory to another using the cp command. Copying files
to another directory uses the exact same command syntax that copying a file uses.
However, to successfully copy files to another directory, you must specify the correct
pathname to the new directory. If you are copying a file not contained in your current
working directory, you must be certain to specify the correct pathname for the source file.

% cp saved.text.mss documents

Exercise:
1.Copy the following files to the directory named documents.
chap1.1 saved.text.mss
2.Copy the following files to the directory named book.
text.group students.doc

Renaming and Moving Files - the mv command


To rename or move a file, type
mv sourcefile destinationfile
When using the mv command, only one copy of the file will exist both before and after
execution of the command. If you move a file to a different directory that contains a file
with the same name, the new file writes over the contents of the old file.

% mv project_plan train_project

% mv Text.Group bin

Exercise:
1.Use the mv command to rename project_plan to train_project.
2.Rename the file called text.group to Text.Group.
3.Rename the file named UNIX_training_materials_1.8.92 to
training_1.27.92.
4.Move the file named chap1.1 to the research directory.
5.Move the file named Text.Group to the bin directory.

The University of Texas at Austin Page 17


Training September 23, 2002
Deleting files - the rm command
The rm command (abbreviated from remove) is used to remove files. To delete a file,
type rm filename. When used alone, the rm command will delete files without any
confirmation from you.. If you use the -i option when issuing the rm command, a
confirmation of the removal is required. Respond with a y for yes or a n for no.

% rm -i training.unix
% rm: remove training.unix?

% rm -i bin/Text.Group
% rm: remove bin/Text.Group?

Removing directories - the rmdir command


When a directory is no longer useful, it can be deleted using the rmdir command. A
directory cannot be removed with the rmdir command unless it contains no files. To
delete a directory, type rmdir directoryname.

% rmdir research

Exercise:
1.Delete the following directories using the rmdir command.
research correspondence

Filters
Filters are tools that allow you to manipulate text. Filters allow you to perform a task on
a file and then filter the changes on to another file. Filters do not alter the original file.

Sorting lines in a file - the sort command


The sort command sorts the lines in a file alphabetically or numerically. The default sort
is alphabetical. Options are used to determine the type of sort that is used. The valid
options are listed below.

-n Sort by arithmetic value (Ignore blanks and tabs)


-r Reverse the sort order
-f Sort regardless of upper or lower case
+x Limit sort to field x

The University of Texas at Austin Page 18


Training September 23, 2002
To use the sort command, type sort, space, an option if desired, space and the filename.

% sort -f students.doc

Exercise:
1. Use the sort command to sort the contents of the file students.doc.
Which line is listed last? Why is this line listed last?
2. Using the cat command, display the contents of the file students.doc.
Has the file been modified?
3. Sort the file students.doc in reverse alphabetical orrder.
4. Sort the file students.doc, telling UNIX to ignore the case.
5. Sort the file students.doc in reverse order and ignoring case.

Searching for a text pattern - the grep command


The grep (global regular expression printer) command is used to search for patterns in a
file. To use the grep command, type grep, space, the string of characters you wish to
search for, space, and the filename.

% grep nd test.doc
Since grep distinguishes between upper and lower case, the -i option can be used to
ignore case.

% grep -i nd test.doc
The -n option is used to display the line number for each string located.

% grep -in bush students.doc

Exercise:
Using the grep command on the file students.doc, determine if the
following students took the class.
Boris Yeltsin
Steve Allen
Bill Clinton

The University of Texas at Austin Page 19


Training September 23, 2002
Word count - the wc command
The wc command reads a file and displays the number of lines, words and characters
contained in the file. The output from the wc command lists the number of lines, the
number of words, the number of characters in a file and is followed by the filename. A
"word" is defined as a string of characters surrounded by white space.

% wc test.doc

55 400 2369 test.doc


lines words characters filename

Exercise:
1. Using the wc command, determine how many lines are in the
students.doc file.
2. Does this give you an accurate count of the number of students in the
class? Why or why not?
3. How many words are in this file?

The University of Texas at Austin Page 20


Training September 23, 2002
Protecting Your Files and Directories

File and Directory Protections


File and directory protections are displayed using the ls command with the -l option.
When executed the "long" form of the ls command displays output similar to the sample
below.

File No. of Date last Time last


Protections file links User Group Size modified modified Filename
- rw- r- -r- - 1 username 204 Mar 16 13:59 README
drwxr-xr-x 2 username 2369 Jan 12 21:57 bin

The first character in the long file description signifies whether the file is a ordinary file
(a dash "-" character signifies an ordinary file) or a directory file (a "d" signifies a
directory file). The next 9 characters define the type of permissions set for the file. If
permission is not granted, a dash "-" character is displayed.

Character #1
2 3 4 5 6 7 8 9 10
File/Directory User Group Others
read write execute read write execute read write exec
ute
(-/d) (r/-) (w/-) (x/-) (r/-) (w/-) (x/-) (r/-) (w/-)
(x/-)

There are three levels of file protection - user, group and others. They are defined as
follows:
User (u)The user or owner of the file or directory.
Group (g)A defined set of users that share access to the file or directory.
Others (o)The remainder of authorized users on this system.
Every file or directory in a UNIX file system has three types of permissions (or
protections) that define whether certain actions are permissible for the user, a group and
others. These permissions are:
read (r)a user who has read permission to a file may look at its contents. For a directory,
read permission enables a user to list the files in that directory.
write (w) a user who has write permission to a file can modify the contents of that file.
For a directory, the user can create and delete files in that directory.

The University of Texas at Austin Page 21


Training September 23, 2002
execute (x)a user who has execute permission for a file can execute the file (providing
that it is a valid executable file). For a directory, execute permission allows a user to
change to that directory, search the directory for a specified file and include the directory
name in a path.
The number of file links is usually 1. A directory has at least 2 file links. It is possible to
have a number of directory entries all pointing to one file.
The next two fields display the owner's name and the group name designations for the
file. The group field is left blank unless the -g option is specified.
The group is followed by the size of the file in bytes, date last modified, time last
modified and the filename.

Changing File Protections - the chmod command


The chmod command (abbreviated from change mode) is used to change the permissions
for a file. The chmod command is listed first on the command line and is followed by a
space. Next, the set of users whose permissions you wish to change must be designated.
Any or all sets may be changed. Following the set of users, the action is specified. An
action can add, remove, or set the specified permissions. The action is followed by the
permissions of read, write and execute. A single permission, multiple permissions or all
permissions may be changed. The last item on the command line is the filename.

possible levels possible actions possible


permissions
u - user only + add r read
g - group only - remove w write
o - others only = sets the specified permissions x execute
and removes previous settings
a – all
ug - user and group
go - group and others only
ugo - user, group and others

The University of Texas at Austin Page 22


Training September 23, 2002
% chmod a+rw neon
adds read and write permissions for user, group and others to the file named neon

% chmod o-rwx neon


removes read, write and execute permissions for others to the file named neon

% chmod u=r neon


sets permissions to read only for the owner of the file and removes all other permissions
for the owner to the file named neon

Exercise:
1. Set the file permissions for the file chap1.1 to read and write for
members of your group. Use the ls –l command to confirm your
changes.
2. Set the file permissions for the file victor to read only for user, group
and others. Use the ls –l command to review your changes.
3. Now change the file permissions for the file victor to read and write for
the user only (removing all other permissions. Use the ls -l command to
review your changes.

Another method used to specify permissions for the chmod command uses a 3 digit octal
number, with each octal digit representing the permissions for a particular set of users
(user, group, other). The table below provides the octal numbers for each possible
combination of symbolic permissions.

Symbolic Octal Number


Code
r-- 4
-w- 2
--x 1
rw - 4 +2 = 6
r-x 4+1=5
- wx 2+1=3
rwx 4+2+1=7
--- 0

The University of Texas at Austin Page 23


Training September 23, 2002
A specific octal number designates each of the possible permission combinations. A
single octal number can be used to describes the permissions for user, group or others. A
three digit octal number can be used to describe the permissions for user, group and
others. If the permissions you wish to specify are rw - r - -r - -, then the octal number can
be determined as shown in the diagram below.

user group others


symbolic rw- r - - r - -
\ / \ / \ /
4+2=6 4+0=4 4+0=4
\/ \/ \/
octal 6 4 4

Using the octal numbers in the diagram above, the chmod command would be issued as
follows:

% chmod 644 test.doc

Changing Directory Protections


As with files, directories also have permissions. To list only directory files and their
permissions, issue the following command

% ls -ld
To set the permissions such that the group can read, write and execute the directory
named research, type:

% chmod g=rwx research


Alternatively, using the octal notation, you could just type:

% chmod 770 research


To change the permissions for you current directory to only user read, write and execute,
type:

% chmod 700 .
To change the permissions for you parent directory to only user read, write and execute,
type:

% chmod 700 ..

The University of Texas at Austin Page 24


Training September 23, 2002
To limit access to your directory for group and others, use the following permissions:
• drwx may do anything to this directory
• d--- may do nothing to this directory
• d--x may look at files (only files with read permission) if you
know the filenames
• dr - x may list the contents of the directory, look at the files, but not
create or remove file in the directory
• d - wx may create, remove or access files in the directory if you
know the filenames (cannot list filenames)

Exercise:
1. Use the ls –ld command to review the directory permissions for you
home directory.
2. Set the file permissions on your home direcotry so that all users in your
group can execute your directory, but not see the contents of your
directory. Now, use the ls –ld command to review your changes.
3. Using the mkdir command, create a directory call documents and set
the file permissions so that members of your group can review
information about files in this directory (read the list of files in the
directory).

Setting the Protection Default - the umask command


When you create a file, the system gives it a default set of permissions. The default
permissions are controlled by the system administrator and will vary from installation to
installation. When changed, the umask command will only affect newly created files.
To change the permissions for previously existing files, use the chmod command.
The umask command uses octal numbers to indicate permissions that have been
turned OFF. (Please note that the chmod command is exactly opposite.) For example,
a umask command using the octal number of 027 can be converted into the following
symbolic expression for file permissions:
rwxr- x - - -
To see what your current umask settings are, issue the umask command without any
arguments. The system will display the octal number.

% umask

22

The University of Texas at Austin Page 25


Training September 23, 2002
Leading zeros are not displayed, so the 22 designation above can be interpreted as 022.
No permissions for the user are turned off, write permissions for group and others are
turned off. The symbolic expression is rwxr - xr - x.
The easiest way to compute the umask is to subtract the permissions that you want
from 777. For example if you wish to have permissions of 644, then subtract 644 from
777 to obtain the umask setting.
777
- 644
133
To change the permission defaults, type umask, space and the appropriate three digit
octal number.

% umask 133

Note: The umask command only effects the current login session. To permanently
change your umask setting, the umask command must be added to the .cshrc file.

Printing
The Printing files - the lpr command
The lpr command (lpr is an abbreviation for lineprinter.) is used to send a copy of your
file to a printer. The option -P is used to name a print site which follows the option. To
use the lpr command, type lpr, space, -P, the print site, space and the filename.

% lpr -Ptay_lw test.doc


For a complete list of print sites available, type man sites at the prompt.

% man sites
If your file is a PostScript file (produced by troff, TeX or LaTeX text formatters), it will
not print accurately on a lineprinter. A PostScript file has %! as its first two characters
and contains formatting instructions that cannot be interpreted by a lineprinter.
PostScript files should be sent to a laser printer (output sites for Laser Writers end with
an lw). For more information about printing PostScript output files, see the man pages
(man lpr).

The University of Texas at Austin Page 26


Training September 23, 2002
On-line Documentation - using the man command
The on-line documentation or help system in the UNIX operating system is known as the
"man" pages or manual pages. The man command is used to view the on-line
documentation. Each man page is divided into the sections described below.
•Name A short description of the command.
•Synopsis A synopsis of the command's syntax.
•Description A description of what the command does.
•Options A list and description of options that can be used with
the command.
•Files A list of the files used or created when the command
is executed.
•See Also A list of relevant commands or files.
•Bugs A list of known bugs and limitations.
As with most of the commands used in UNIX, the ls command has several options that
are available to you. To view the options available for the ls command, type man ls.

% man ls
A complete description of the ls command and its available options are displayed.

Exercise:
1.Use the man command to look up the on-line documentation on the ls
command. Use the information listed in the man pages to answer the
question below.
List two options that can be used with the ls command.
2.Using the man pages, look up the more command. What is the purpose
of the more command?

The University of Texas at Austin Page 27


Training September 23, 2002
The man pages are available for most UNIX commands and are organized into sections
by function. Below is a list of each section and the functions contained.
Section Description
1 General user commands
2 System calls and system errors
3 Library routines (C and FORTRAN)
4 Special files and device drivers
5 File formats (No intro man page)
6 Games (No intro man page)
7 Miscellaneous information pages
8 System maintenance commands

The man command searches the sections in the order 1, 6, 8, 2, 3, 4, 5, 7 and displays the
first man page found.
Section 7 is used at the ITS for UT specific information or for information that does not
fit in any other section.
To use the man pages to look up a specific section, type man sectionnumber and the
command or term.

% man 7 sites

Exercise:
1.Use the man pages to look up information in section 7 pertaining to print
sites.

The University of Texas at Austin Page 28


Training September 23, 2002
Working with the Shell
The shell acts as a user interface that communicates both with you, the user and with the
operating system or kernel. The shell is known as a command interpreter because the
shell interprets the commands that you type, starts the programs that you request and
works as a buffer between the user and the operating system. One of the most
outstanding characteristics of the UNIX operating system is that the shell is just a
program. You may choose to run any of several shell programs. The diagram below
represents the relationship between the shell, the kernel and the hardware.

csh

kernel cc

vi
Hardware
"O
pe g
r t i n
Sy a m"
s t e User
sh Program

The shell can also be used as a programming language. You can use a shell to execute a
set of shell commands placed in a file, called a shell script or shell "program". That shell
script can then be invoked at any time and will perform the listed commands.
The most common shells are described below. The ITS machines use the C-shell as the
default interactive shell. The Bourne shell, by convention, is the shell used for writing
shell programs.
sh The "Bourne shell" was named for the shell's author, Steven Bourne of Bell Labs.
The Bourne shell is found on every UNIX system. The usual convention is to use the
Bourne shell to write shell programs.
csh The "C-shell" was written by a group of people at Berkeley. Many of the
programming language constructs resemble the C language, hence the name C-shell. The
C-shell is commonly used for interactive use. The C-shell is known for the three major
features described below.
• Job Control allows the user to switch between multiple processes.
• History keeps a list of previously executed commands which can be
recalled and executed.
• Aliases allow the user to abbreviate commands.
ksh The Korn shell, written by David Korn is available from the AT&T Toolchest.
The Korn shell adds job control, history, command line editing, aliases and subroutines to
the Bourne shell. The Korn shell is compatible with Bourne shell scripts and is

The University of Texas at Austin Page 29


Training September 23, 2002
commonly available on System VR4 UNIX machines. The Korn shell is only available
on emx at the ITS.
tcsh The tcsh shell is public domain software. Tcsh is basically the C-shell with
command completion (allows the user to type partial commands which are completed by
the system) and command line editing.
bash The bash shell (Bourne Again SHell) is offered free by the GNU foundation. The
bash shell is bourne shell compatible and offers command line editing, command aliases
and history.
To identify the shell you are currently using, type printenv SHELL at the prompt. In the
example below, the shell in use is the C-shell.

% printenv SHELL

/bin/csh

Shell Duties
• Locates commands; passes arguments and control to the command
• Handles the sequential and concurrent execution of commands
• Performs I/O redirection
• Provide wildcards for filenames
• Maintains environment variables
• Handles pipes (chain output of one program into another program)
• Provides job control
• Provides command aliases
• Provides a programming language

Processes
Each program that you run while logging in or working in the shell is called a process.
After the login process is complete, the C-shell process is running and provides the
prompt that you see. At this point, additional processes may be run. Examples of other
processes one might choose to run are the mail program, vi, troff, a C compiler or another
C-shell.

In the diagram above, the C-shell represents the login shell. A child process is a program
that was started under the current shell. The shell and its children are arranged in a

The University of Texas at Austin Page 30


Training September 23, 2002
hierarchical structure, much like the file system. The additional processes running under
the login C-shell are known as children. During a UNIX session, you may start as many
processes as you need.

Environment Variables
An environment variable can store information that is available to the current shell and to
its children. Examples of some environment variables are shown below.
The printenv command is used to display the current environment variables. To use the
printenv command, type printenv at the command prompt. The shell displays the current
environment variables. An example of the output displayed from the printenv command
with an explanation for each variable is listed below.

% printenv
HOME=/home/path/u0/cc/userdirectory your login or home directory pathname

SHELL=/bin/csh your default shell

TERM=vt100 terminal type, used by editors

USER=username your login name

PATH=/usr/local/bin:/usr/local:/usr/ucb:/bin:/usr/bin:

the list of directories searched for commands

EDITOR=vi name of your default editor

The variables listed in the previous example are known as global variables. This set of
variables is available to all processes. The setenv command is used to set global
environment variables. The unsetenv command is used to unset global environment
variables. To change the EDITOR environmental variable to ed, type setenv, space,
EDITOR (in caps), space and ed.

% setenv EDITOR ed
To change the EDITOR environment variable back to vi, type the command again
substituting vi for the ed value.
In the C-shell you have the option to create and set additional "local" variables which are
used in the current process. The set command is used to create and set a local variable.
The UNIX convention for variables is that UPPERCASE LETTERS are used for
GLOBAL VARIABLES and lowercase letters are used for local variables. In the
example below, the set command is used to set a local variable g to the value
/home/games.

% set g=/home/games

The University of Texas at Austin Page 31


Training September 23, 2002
To display a list of local variables, type set at the prompt. To delete a local variable, type
unset, space and the variable name.

% unset g
The environment variables that you will use frequently are HOME, PATH and TERM. A
discussion of each is provided.

The Path Variable


The PATH environment variable is a list of directories separated by colons that the shell
searches when it attempts to execute a command. The order of the directories listed in
the PATH variable is significant since the shell will search for a command using the
order specified.
For example, if a program is stored both in the /bin directory and in your home directory,
when the program is executed it will search the PATH for the first directory listed that
contains the program. If the /bin directory is listed first in the PATH, then it will be
searched first and the program will be executed from the /bin directory. If your home
directory is listed in the PATH first, the program will execute from your home directory.
To see the current PATH environment variable setting, type printenv at the prompt. The
shell will display the current environment variables including PATH.
When the shell starts up, it builds a "hash" table listing the commands found in each
directory in the PATH. The rehash command is used to rebuild this table. If you
change the PATH, use the rehash command to update the table and to use the new PATH
in your current login session. If you do not use the rehash command, the changes will
not take effect until the next time you login.

Exercise:
1.Using the pwd command, print out the path to your current working
directory.

Standard Input/Output and Standard Error


The place from which a program reads input is called Standard Input. Standard input
by default is the keyboard. For some commands, if no file argument is specified, the
input will be read from standard input (the keyboard).
The place to which a program writes its output is called Standard Output. Standard
output by default is the terminal screen (or monitor).
Standard Error is used to notify the user about errors. Standard error by default is
displayed on your screen.
As long as the standard input, standard output, and standard error defaults are not
changed, the operating system expects to read information from your keyboard, writes
output to your screen and displays standard error on the screen.

The University of Texas at Austin Page 32


Training September 23, 2002
I/O Redirection
Both standard input and standard output can be changed. For example, standard input
can come from a file and standard output can be written or appended to a file. The
characters <, >, and >> are used to redirect standard input and output. The I/O (short for
input/output) redirection characters are used in a command line between file or process
names. All I/O redirection is handled by the shell.
The command "who" generates a list of users currently using the system. The standard
output for this command is the terminal screen. To redirect the output to a new file
named users, type who, the > character and the filename.

% who > users


The table below displays the various I/O redirection characters that are available.
> filename Redirects standard output to a file
>> filename Appends standard output to an existing file or create a
new file if the file does not exist
< filename Redirects standard input from a file

Examples of standard I/O redirection:


who > users output from who is redirected into a file
named users (a new file)
who >> test.doc output from who is appended to the file
named test.doc
cat test.doc > Saved.text.mss output from the catenation of test.doc is
written to Saved.text.mss (Saved.text.mss
will be overwritten)
sort < student.doc > student.sort The file student.doc is input for the sort
command . The output from the sort
command creates a file named student.sort

Exercise:

1. Use the who command to determine the users currently logged in.
2. Issue the who command again, but this time redirect the output to a file
called users.
3. Sort this file and redirect the output to a file called users.sorted.

The University of Texas at Austin Page 33


Training September 23, 2002
Pipes
A Pipe connects the standard output of one program to the standard input of another
program. The "|" character is used to signify a pipe. More than two commands can be
connected with a pipe. A series of commands can be connected together, the standard
output from each becoming the standard input to the next command in the series.
For example, if you want to display a list of the first ten entries of a sorted list of users on
the system, you could issue the following command:

% who | sort | head

Exercise:

Use pipe (|) with ls -l to see a list of your files one screen at a time?
We want to see who is currently logged on, but we want the information
sorted and displayed one screen at a time. Use the pipe (|) in conjunction
with the who, sort and more commands to achieve this goal.

Sequential Commands
A series of commands can be listed on one command line using the semicolon (;)
character to separate each command. The commands will be executed sequentially. If
one command fails, the other commands are still executed.

% who ; cat students.doc ; more test.doc

Concurrent Execution
The UNIX operating system is a multitasking. The ampersand (&) character placed at
the end of a command line is used to run a process or job in the background. The process
in the foreground continues to accept standard input and is displayed on your screen. To
start a process and run it in the background, type the command followed by the
ampersand character.

% cc program.c &
After you enter the command, the process id is displayed and the command continues to
run in the background.

% cc program.c &
[4] 17738

The University of Texas at Austin Page 34


Training September 23, 2002
Standard Process Control
The ps (process status) command displays the status of all processes that are running. To
use the ps command, type ps at the prompt. A list of your current processes is displayed.

% ps

PID TT STAT TIME COMMAND

5218 2f T 0:01 vi

7937 2f T 0:00 vi

16462 2f E 0:01 csh


The kill command is used to send a signal to a process. There are two common options
used with the kill command. The first option, -HUP sends a hangup signal to a process.
The HUP option allows the process to clean up after itself before it dies. For example,
the vi text editor will save a copy of your file before exiting the program when it receives
a hangup signal. To use the kill command with the -HUP option, type kill, space, -HUP,
space and the PID number (Process Identification Number). The word "Hangup" is
displayed as a confirmation that the process has been terminated.

% kill -HUP 5218

Hangup
The second option, -9 sends a kill signal to a process. The -9 option cannot be ignored
and will always kill the process. The process will not have an opportunity to clean up
after itself. To use the kill command with the -9 option, type kill, space, -9, space and
the PID number.

% kill -9 10433
The kill command given with the C-shell id will kill all processes within that shell.

csh - Job Control


The C-shell provides additional commands that are used to control jobs or processes.
You may suspend a job, display a list of currently executing jobs, send a job to the
background or call a job to the foreground.
To suspend a job, type <Control z>.
The jobs command displays a list of jobs currently running. The information displayed
includes job number and the status and name of the job.

% jobs

[1] + Stopped vi neon

The University of Texas at Austin Page 35


Training September 23, 2002
After a job has been stopped, you are free to do another task, for example reading your
mail. After you finish this task, you can resume the suspended job exactly where the job
left off. To resume a job, you can use the foreground command. A job can be moved to
the foreground by typing only fg or fg, space and the job number. Any job output or
screen display is immediately seen on your screen.

% fg %1
Before you logout, you must resume and finish or kill suspended jobs.

Often, you may start a time consuming job and wish to continue working on other
projects at the same time. In the example below, you will format a long document using
the troff text formatting program.

% troff -man csh.man > csh.dvi


After you start a job, suspend it with <Control-z>.

[1] + Stopped troff - man csh.man


Then, the job can be sent to the background using the bg command. The background
command is used by typing bg, space and the job number.

% bg %1

[1] troff -man csh.man &

csh - History
The c shell keeps a list of commands that have been executed during this login session.
The list is called history and is stored in the memory of the system. History is listed in
the order of execution (with your last command listed last). The number of commands
stored and displayed in history is set using variables. To see the current history, type
history at the prompt.

% history

The University of Texas at Austin Page 36


Training September 23, 2002
Event Selectors
Each event listed in the history is numbered A command from history may be
substituted at your command prompt by using an event selector. An event selector
always begins with an exclamation point (! also known as "bang"). A list of the valid
event selectors is provided in the following table.
Event Selector Detail
!31 Selects command number 31
!ec The last command beginning with the letters “ec”
!?xy? The last command containing “xy”
!-3 Three commands ago.
To use an event selector, type the appropriate selector at the prompt.

% ! 15

Exercise:
Use the history command to get a list of your previous commands.
Use each of the event selectors listed in the table above to execute a
command from history.

csh - Aliases
An alias is used to create shorthand commands. To display the current aliases that have
been defined, type alias.

% alias

h history

l ls -FC
To create a new alias, type alias, space, the new alias, space and the name of the
command.

% alias d date
To delete an alias, type unalias, space and the name of the alias you wish to remove.

% unalias d

Exercise:
1.Create an alias for the date command.
2.Create an alias for the who command.
3. Delete both aliases.

The University of Texas at Austin Page 37


Training September 23, 2002
Shell Procedures
As mentioned previously, the shell , our command interpreter, implements a
programming language. You can place these language constructs or programs in a file.
These files are called shell scripts.
The shell programming language implements variables, control structures (if statement,
while loops, for statements), parameter passing and interrupt handling. Examples of shell
scripts are the .login, .cshrc and .logout files.

Creating Your Own Commands


To create your own command follow the steps listed below:
• Create a shell script
• Make the file executable (chmod command)
• Place the file in a directory, usually the $HOME/bin directory is used
• Alter the PATH variable to include that directory
• Type rehash to rebuild the C-shell's internal table (csh only)
To execute the new command, type the command name.

Setting Terminal Types


There are many different types of terminals that can be used to log in to a Computation
Center system. It is important to correctly specify the terminal type so that programs
may properly control your terminal. This is most important for full screen editors and
menu systems. This information is passed to programs via the global shell variable
TERM. You can use the setenv command to set this variable.

% setenv TERM vt100


See the list below for some of the common terminal types at UT Austin.
Terminal Type
DEC VT 100 vt100
Kermit vt100
Micro-Term Ergo Series* vt100
Micro-Term MIME-2A emulating enhanced VT52 vt52
Sun Workstation sun
Televideo 950 tvi950
*These terminals can emulate a variety of terminals. The most common terminal setting used at UT is
vt100.
If your terminal is not listed, you can leave your terminal unidentified without serious
problems.
Note: You will not be able to use a screen editor without specifying your terminal type.

The University of Texas at Austin Page 38


Training September 23, 2002

You might also like