Unix Book
Unix Book
Unix Book
• An operating system is the program that controls all the other parts of a
computer system, both the hardware and the software. It allocates the
computer's resources and schedules tasks. It allows you to make use of the
facilities provided by the system. Every computer requires an operating
system.
History of UNIX
• UNIX has become the operating system of choice for engineering and
scientific workstations.
• Two variations maintain popularity today, AT&T System V based and the
Berkeley Standard Distribution.
UNIX Philosophy
• Make each program do one thing well. Reusable software tools: 1 tool =
1 function
• Use short commands and messages: reduces typing and screen output
• Upgrade Unix by writing your own functions and incorporate them into
Unix
Common UNIX Utilities
Functional List
• Information
• Miscellaneous
• Printing
• Programming Tools
• Terminal Related
• uucp / BNU
• cut - Writes out selected bytes, characters, or fields from each line of a file
Information
• apropos - locate commands by keyword lookup
Miscellaneous
Printing
Programming Tools
• cc - C compiler
• ld - link editor
• sccs - front end for the Source Code Control System SCCS. Includes a
number of related commands with their own man pages.
Terminal Related
• checknr - check nroff and troff input files; report possible errors
• ul - do underlining
uucp / BNU
• Hardware independence
UNIX Components
• Kernel
o You do not need to know anything about the kernel in order to use
a UNIX system. These details are provided for your information only.
• Shell
o Several shells are usually available on any UNIX system, each with
its own strengths and weaknesses.
C shell (csh)
Korn shell (ksh)
TC Shell (tcsh)
• Utilities
editing
file maintenance
printing
sorting
programming support
online info
etc.
• Before you can begin to use the system you will need to have a valid
username and a password. The System Administrator or a “Computer
Accounts” office typically handles assignment of usernames and initial
passwords.
• You will be given a sheet with your username, which is usually your
name’s initials (rharpaz), and a password, which is computer-generated
sequence of 8 characters (Ejh5Y7*!).
• What happens after you successfully login depends upon your system:
o You should see a prompt - usually a percent sign (%) or dollar sign
($). This is called the "shell prompt”. It indicates that the system is
ready to accept commands from you.
o At the Atrium lab the first thing you will have to do is change your
password and wait 25 minutes for the system to process it. The
password must be between 6-8 characters long and remember it is
case sensitive.
• After login, you are "put" into your home directory automatically. This is
where you start your work. example : $ acc6:/users3/user_name>
• You are in control of your home directory and the files, which reside
there. You are also in control of the file access permissions (discussed later)
to the files in your home directory. Generally, you alone should be able to
create/delete/modify files in your home directory. Others may have
permission to read or execute your files as you determine.
• In most UNIX systems, you can "move around" or navigate to other parts
of the file system outside of your home directory. This depends upon how the
file permissions have been set by others and/or the System Administrator,
however.
• All UNIX commands are case sensitive. Most commands are in lower case.
• Options
• Arguments
1. Most commands are used together with one or more arguments
• Examples
date
cd newdir
ls -als
• Multiple commands can be entered on one line if you separate each with a
semicolon.
cd newdir ; ls -l
Special Characters
ls *.txt
2. Use the ? character in file names to match any single character. The
following command:
ls ???.txt
ls chapter[1-3].txt
• To change your password: enter the command passwd and then respond
to the prompts by entering your old password followed by your new one. You
are then asked to retype your password for confirmation.
Getting Information
o man can also provide you with one line descriptions of commands
which match a specified keyword
o
o Section Description
o ------- -----------
o 1 User Commands
o 2 System Commands
o 3 Subroutines
o 4 Devices
o 5 File Formats
o 6 Games
o 7 Miscellaneous
o 8 System Administration
o l Local Commands
o n New Commands
man cp
--More--23% at the bottom left of the screen means that
only 23% of the man page is displayed. Press the space bar to
display more of it or type q to quit.
man 8 telnet
man -k mail
man -k 'copy files'
whatis more
who
who am i
finger doe
finger userid
cal 04 2000
• If you have a .logout file in your home directory, the system will execute
any commands contained there.
• All of the files in the UNIX file system are organized into a multi-leveled
hierarchy called a directory tree.
• At the very top of the file system is single directory called "root" which is
represented by a / (slash, which is also a name ). All other files are
"descendents" of root.
• Example:
/ (root)
|
------------------------------
| | | |
/bin /usr /lib /dev
|
|
-------------------
| | |
/david /rave /john
| |
------------
| |
/software /schoolwk
File Types
• Ordinary Files
o Used to store your information, such as some text you have written
or an image you have drawn. This is the type of file that you usually
work with.
• Special Files
o UNIX allows you to link commands together using a pipe. The pipe
acts a temporary file which only exists to hold data from one
command until it is read by another
o For example, to pipe the output from one command into another
command:
who | wc -l
The standard output from the who command is a list of all the users
currently logged into the system. This output is piped into the wc
command as its standard input. Used with the -l option this command
counts the numbers of lines in the standard input and displays the
result on its standard output - your terminal.
File Names
• UNIX permits file names to use most characters, but avoid spaces, tabs
and characters that have a special meaning to the shell, such as:
• Reserved Filenames:
Pathnames
• Absolute Pathname: tells how to reach a file begining from the root;
always begins with / (slash). For example:
/usr/local/doc/training/sample.f
• Relative Pathname: tells how to reach a file from the directory you are
currently in ( current or working directory); never begins with / (slash). For
example:
training/sample.f
../bin
~/projects/report.001
• For example
cd ../quattro - relative pathname
cd /usr/home/classwk2 - absolute pathname
the commands usually have several options and accept wildcard characters as
arguments.
• ls - lists files
• more - browses/displays files one screen at a time. Use h for help, spacebar
to page, b for back, q to quit, /string to search for string
more sample.f
pg sample.f
• cp - copies files. Will overwrite unless otherwise specified. Must also have
write permission in the destination directory.
• mv - moves files. Will overwrite unless otherwise specified. Must also have
write permission in the destination directory.
file filename
The pathname defines the directory to start from. Each subdirectory of this
directory will be searched. The -print option must be used to display results.
You can define the filename using wildcards. If these are used, the filename
must be placed in 'quotes'.
• sdiff - similar to diff, but displays each line of the two files side by side,
making it easier for you to see the differences between them
ln source linkname
Making a link to a file or directory does not create another copy of it. It
simply makes a connection between the source and the linkname. Allows a
single file to be "pointed to" by other filenames without having to duplicate
the file.
• sort - sorts files, merges files that are already sorted, and checks files to
determine if they have been sorted. The command syntax is:
sort options filename
By default, lines in "filename" are sorted and displayed to the screen. If the
"filename" parameter specifies more than one file, the sort command
concatenates the files and sorts them as one file.
• pwd - print working directory. Tells you which directory you are
currently in.
pwd
• mkdir - make directory. Will create the new directory in your working
directory by default.
mkdir /u/training/data
mkdir data2
rmdir project1
rm -r dirctory_name
m-tools
Access Permissions
• UNIX is a multi-user system. Every file and directory in your account can
be protected from or made accessible to other users by changing its access
permissions. Every user has responsibility for controlling access to their files.
r - read
w - write
x - execute = running a program
u - user = yourself
g - group = can be people in the same project
o - other = everyone on the system
• File access permissions are displayed using the ls -l command. The output
from the ls -l command shows all permissions for all levels as three groups of
three according to the scheme:
• chmod - is used to change access permissions for files which you own. The
syntax is:
where:
Examples:
chmod +w *
r w x
4 2 1
These values are added together for any one user category:
0 = no permissions
1 = execute only
2 = write only
3 = write and execute (1+2)
4 = read only
5 = read and execute (4+1)
6 = read and write (4+2)
7 = read and write and execute (4+2+1)
Unix Editors
• There are several standard text editors available on most UNIX systems:
o lots of others
The Standard Display Editor - vi
o replacing text
• vi uses a "buffer"
o You can also decide not to write the contents of the buffer, and
leave your original file unchanged.
o Command mode
o Insert mode
There are several commands that you can use to enter this
mode.
Must press the ESC (escape) key to exit this mode and
return to command mode.
vi Commands
• Entering vi
• Exiting vi
By character
left arrow - left one character
right arrow - right one character
backspace - left one character
space - right one character
h - left one character
l - right one character
By word
w - beginning of next word
nw - beginning of nth next word
b - back to previous word
nb - back to nth previous word
e - end of next word
ne - end of nth next word
By line
down arrow - down one line
up arrow - up one line
j - down one line
k - up one line
+ - beginning of next line down
- - beginning of previous line up
0 - first column of current line (zero)
^ - first character of current line
$ - last character of current line
By block
( - beginning of sentence
) - end of sentence
{ - beginning of paragraph
} - end of paragraph
By screen
CTRL-f - forward 1 screen
CTRL-b - backward 1 screen
CTRL-d - down 1/2 screen
CTRL-u - up 1/2 screen
H - top line on screen
M - mid-screen
L - last line on screen
Within file
nG - line n within file
1G - first line in file
G - last line in file
• Inserting text
• Deleting text
x - delete character at cursor
dh - delete character before cursor
nx - delete n characters at cursor
dw - delete next word
db - delete previous word
dnw - delete n words from cursor
dnb - delete n words before cursor
d0 - delete to beginning of line
d$ - delete to end of line
D - delete to end of line
dd - delete current line
d( - delete to beginning of sentence
d) - delete to end of sentence
d{ - delete to beginning of paragraph
d} - delete to end of paragraph
ndd - delete n lines (start at current
line)
• Changing text
• Copying lines
• Searching / Substituting
• Miscellaneous commands
vi Options
• You can change the way vi operates by changing the value of certain
options which control specific parts of the vi environment.
:set option_name
:set option_name=value
• Some examples of the more common options are described below.
• pico is a simple text editor based upon the Pine Message/mail System. It is
far easier to use than vi, with the tradeoff that is isn't as powerful.
• To start pico, simply use the command "pico" followed by the name of the
file you wish to edit. If the file exists, pico will start with that file. If it does
not exist, then pico will assume that it is a new file.
o The manner in which lines longer than the display width are dealt
is not immediately obvious. Lines that continue beyond the edge of the
display are indicated by a '$' character at the end of the line. Long
lines are scrolled horizontally as the cursor moves through them.
• Instead of retyping a command you already typed you can Press Esc and
then ‘k’ to move up through the list of commands you already typed.
• Type ‘h’ (history) to get a list of the previous commands you typed.
• To search for a command you already typed press Esc and then ‘/’
(forwardslash) and the word you are looking for.
• Instead of retyping a long command you can type part of that command
and then press Esc followed by a ‘\’ (backslash).
Printing
lp – same as lpr
print queue
There are two main C compilers in the unix operating system: acc and gcc.
progname to generate an
link progname to
generate an
executable called
newname
compile progname to
generate an object
file called progname.o
• Whenever you login to a Unix system you are placed in a program called
the shell. All of your work is done within the shell.
• There are several shells in widespread use. The most common ones are
described below.
TC Shell (tcsh)
Available in the public domain. It provides all the features of the C shell
• The local System Administrator usually establishes your login shell when
your userid is created. You can determine your login shell with the
command:
echo $SHELL
• Each shell has a default prompt. For the 5 most common shells:
Processes
• Process id: every process in a UNIX system has a unique PID - process
identifier.
$ ps
PID TTY TIME CMD
26450 pts/9 0:00 ps
66801 pts/9 0:00 -csh
To see a detailed list of all of your processes on a machine (current shell and
all other shells):
$ ps –a
$ ps –f - list of all users and their
processes
• kill - use the kill command to send a signal to a process. In most cases, this
will be a kill signal, hence the command name. Note that you can only kill
processes, which you own. The command syntax is:
Examples:
You can also use CTRL-C to kill the currently running process.
To start a job in the background, use an ampersand (&) when you invoke the
command:
myprog &
jobs
[1] + Running xcalc
[2] Running find / -name core -print
fg %2
• Stop a job running in the background: Use the jobs command to find its
job number, and then use the stop command. You can then bring it to the
foreground or restart execution later.
jobs
[1] + Running xcalc
[2] Running find / -name core -print
stop %2
• Kill a job running in the background, use the jobs command to find its job
number, and then use the kill command. Note that you can also use the ps
and kill commands to accomplish the same task.
jobs
[1] + Running xcalc
[2] Running find / -name core -print
kill %2
o If a background job tries to read from the terminal, the shell will
automatically stop it. If this happens, you must put it in the
foreground to supply the input.
The shell will warn you if you attempt to logout and jobs are still running in the
background. You can then use the jobs command to review the list of jobs and
act accordingly.
Redirection
• The following symbols are used on the shell command line to redirect a
process's stdin, stdout and/or stderr to another location, such as a file or
device.
• Examples:
• A pipe is used by the shell to connect the stdout of one command directly
to the stdin of another command.
• The symbol for a pipe is the vertical bar ( | ). The command syntax is:
Operation 1
who > temp
sort temp
Operation 2
who | sort
• Two very common uses of a pipe are with the "more" and "grep" utilities.
Some examples:
ls -al | more
who | more
who | grep kelly
Filters
• Command lines, which use a filter, will include pipes to connect it to the
stdout of one process and the stdin of another process.
• For example, the command line below takes the output of "who" and sorts
it. The sorted output is then passed to the lp command for printing. In this
example, sort is a filter.
who | sort | lp
• Both filters and pipes demonstrate a basic UNIX principle: Expect the
output of every program to become the input of another, yet unknown,
program to combine simple tools to perform complex tasks.