Sie sind auf Seite 1von 57

What Is UNIX?

• UNIX is a computer operating system.

• 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.

• UNIX is a multi-user, multi-tasking operating system. Multiple users may


have multiple tasks running simultaneously. This is very different than PC
operating systems.

• UNIX is a machine independent operating system. Not specific to just one


type of computer hardware. Designed from the beginning to be independent
of the computer hardware.

• UNIX is a software development environment. Was born in and designed


to function within this type of environment.

History of UNIX

• 1969: Developed at AT&T Bell Labs by Ken Thompson and Dennis


Ritchie.

• Created in an environment when most computer jobs were fed into a


batch system (running single jobs, using punch cards and waiting for
output).

Developed as a response to the frustration of programmers who needed a set


of computing tools to help them with their projects and their collaborators.
Allowed a group of people working together on a project to share selected
data and programs.
• Unix was originally written in assembly language, thus moving Unix from
one computer to another involved a large amount of rewriting of the
programs constructing Unix.

• 1973: Thompson and Ritchie successfully rewrote Unix in C , making it a


portable operating system.

• 1975: AT&T makes UNIX widely available - offered to educational


institutions at minimal cost. Becomes popular with university computer
science programs. AT&T distributes standard versions in source form:
Version 6 (1975), Version 7 (1978), System III (1981).

• 1984 to date: University of California, Berkeley adds major


enhancements, creates Berkeley Standard Distribution (BSD)

• 1984 to date: Many Berkeley features incorporated into new AT&T


version: System V

• 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

• Enable the output of one program become the input of another, to


combine simple tools to perform complex tasks

• Prototyping: get something small working as soon as possible and modify


it incrementally until it is finished

• 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

• Files and Directories

• Information

• Mail

• Miscellaneous

• Printing

• Programming Tools

• Shells, Job Control and Access

• Terminal Related

• Text Formatting and Editing

• User Information and Communications

• uucp / BNU

Files and Directories

• ar - create library archives, and add or extract files

• basename - display portions of pathnames and filenames

• cat - concatenate and display

• cd - change working directory

• chgrp - change the group ownership of a file

• chmod - change the permissions mode of a file

• cmp - perform a byte-by-byte comparison of two files

• comm - selects or rejects lines common to two sorted files

• compress - compress files (see uncompress also)


• cp - copy files

• cpio - copy file archives in and out

• csh - a shell with a C-like syntax and advanced interactive features

• cut - Writes out selected bytes, characters, or fields from each line of a file

• df - report free disk space on file systems

• diff - display line-by-line differences between pairs of text files

• du - display the number of disk blocks used per directory or file

• expand - expand TAB characters to SPACE characters (see unexpand)

• file - determine the type of a file by examining its contents

• find - find files by name, or by other characteristics

• install - install files

• ftp - file transfer program

• grep - search a file for a string or regular expression

• head - display first few lines of specified files

• ln - make hard or symbolic links to files

• ls - list the contents of a directory

• mkdir - make a directory

• more - browse or page through a text file

• mv - move or rename files

• od - octal, decimal, hexadecimal, and ascii dump

• page - browse or page through a text file

• paste - joins lines from one or more files


• pwd - display the pathname of the current working directory

• rcp - remote file copy

• rev - reverse the order of characters in each line

• rm - remove files or directories

• rmdir - remove directories

• size - display the size of an object file

• sort - sort and collate lines

• spell - report spelling errors

• split - split a file into pieces

• strings - find printable strings in an object file or binary

• strip - remove symbols and relocation bits from an object file

• tail - display the last part of a file

• tar - create tape archives, and add or extract files

• tee - replicate the standard output

• tftp - trivial file transfer program

• touch - update the access and modification times of a file

• uncompress - uncompress files (see compress also)

• uniq - remove or report adjacent duplicate lines

• unexpand - restores TAB characters from SPACE characters (see expand)

• wc - display a count of lines, words and characters

• zcat - display compressed files

Information
• apropos - locate commands by keyword lookup

• hostname - set or print name of current host system

• man - display reference manual pages

• rup - show host status of local machines RPC version

• ruptime - show host status of local machines

• uptime - show how long the system has been up

• whatis - describes what function a command performs

• whereis - locates source, binary, or manual for program

• which - locates a program file, including aliases and paths

Mail

• biff - give notice of incoming mail messages

• mail, Mail - read or send mail messages

• vacation - reply to mail automatically

Miscellaneous

• banner - display a string in large letters

• cal - display a calendar

• calendar - a simple reminder service

• date - display or set the date

• join - relational database operator

• echo - echo arguments to the standard output

• look - find words in the system dictionary or lines in a sorted list

• leave - remind you when you have to leave


• script - make typescript of a terminal session

• tsort - topological sort

• units - conversion program

• yes - outputs an affirmative response repetitively

Printing

• cancel - cancel requests to a printer

• lp - send requests to a printer

• lpq - display the queue of printer jobs

• lpr - send a job to the printer

• lprm - remove jobs from the printer queue

• pr - prepare files for printing, perhaps in multiple columns

Programming Tools

• ar - create library archives, and add or extract files

• awk - pattern scanning and processing language

• cb - a simple C program beautifier

• cc - C compiler

• cpp - the C language preprocessor

• dbx - source-level debugger

• indent - indent and format a C program source file

• gprof - display call-graph profile data

• ld - link editor

• lex - lexical analysis program generator


• lint - a C program verifier

• lorder - find an ordering relation for an object library

• make - maintain, update, and regenerate related programs and files

• mkstr - create an error message file by massaging C source files

• nm - print symbol name list

• prof - display profile data

• ranlib - convert archives to random libraries

• sccs - front end for the Source Code Control System SCCS. Includes a
number of related commands with their own man pages.

• size - display the size of an object file

• strip - remove symbols and relocation bits from an object file

• xstr - extract strings from C programs to implement shared strings

• yacc - yet another compiler-compiler: parsing program generator

Shells, Job Control and Access

• at - execute a command or script at a specified time

• batch - runs jobs when the system load level permits

• expr - evaluate arguments as a logical, arithmetic, or string expression

• false - provide truth values (see true also)

• kill - send a signal to a process, or terminate a process

• login - log in to the system

• nice - run a command at low priority

• passwd - change local password information

• ps - display the status of current processes


• rlogin - remote login

• rsh - remote shell

• sh - shell, the standard UNIX system command interpreter and command


level language

• sleep - suspend execution for a specified interval

• su - super-user, temporarily switch to a new user ID

• telnet - user interface to a remote system using the TELNET protocol

• test - evaluates conditional expressions

• time - time a command

• true - provide truth values (see false also)

• wait - wait for a process to finish

• xargs - constructs parameter lists and runs commands

Terminal Related

• clear - clear the terminal screen

• mesg - permit or deny messages on the terminal

• stty - sets, resets, and reports workstation operating parameters

• tabs - set tab stops on a terminal

• tty - display the name of the terminal

Text Formatting and Editing

• addbib - create or extend a bibliographic database

• checknr - check nroff and troff input files; report possible errors

• checkeq - checks documents formatted with memorandum macros

• colcrt - filter nroff output for a terminal lacking overstrike capability


• ctags - create a tags file for use with ex and vi

• deroff - remove nroff, troff, tbl and eqn constructs

• e - starts the INed editor

• ed, red - basic line editor

• edit - provides a simple line editor for the new user

• eqn - formats mathematical text for the troff command.

• ex - edits lines interactively, with a screen display

• fmt - formats mail messages prior to sending

• fold - folds long lines for finite-width output device

• lookbib - find references in a bibliographic database

• neqn - formats mathematical text for the nroff command.

• nroff - format documents for display or line-printer

• roffbib - format and print a bibliographic database

• sed - stream editor

• sortbib - sort a bibliographic database

• tbl - format tables for nroff or troff

• troff - typeset or format documents

• ul - do underlining

• vi, vedit - visual display editor

• view - starts the vi editor in read-only mode

User Information and Communication

• finger - display information about users


• groups - display a user's group memberships

• last - indicate last logins by user or terminal

• rusers - who's logged in on local machines RPC version

• rwall - write to all users over a network

• rwho - who's logged in on local machines

• talk - talk to another user

• users - display a compact list of users logged in

• w - who is logged in, and what are they doing

• wall - write to all users logged in

• write - send messages to other users on the system in real time

• who - identifies the users currently logged in

• whoami - display your username

uucp / BNU

• uucp - system to system copy

• uuencode, uudecode - encode a binary file, or decode its ASCII


representation

• uulog - provides information about BNU file-transfer activities on a


system.

• uuname - provides information about other systems accessible to the local


system

• uusend - send a file to a remote host

• uux - runs a command on another UNIX-based system


Why UNIX?

• Hardware independence

o operating system code is written in C language rather than a


specific assembly language

o operating system software can be easily moved from one hardware


system to another

o UNIX applications can be easily moved to other UNIX machines.


Porting is usually as simple as transfer of the source and a recompile

o Device-independent I/O operations, Unix treats al devices as files


allowing redirection

• Productive environment for software development

o rich set of tools

o versatile command language

• UNIX is available at virtually all computing centers, allowing researchers


relative ease in utilizing the facilities at each center.

• Multi-user and multi-tasking capability

• Hierarchical file system providing easy management

• More than 100 utilities

UNIX Components

• Kernel

o The core of the UNIX system. Loaded at system start up (boot).


Memory-resident control program.
o Manages the entire resources of the system. Provides service to user
applications such as device management, process scheduling, etc.

o Example functions performed by the kernel are:

 managing the machine's memory and allocating it to each


process.

 scheduling the work done by the CPU so that the work of


each user is carried out as efficiently as is possible.

 accomplishing the transfer of data from one part of the


machine to another

 interpreting and executing instructions from the shell

 enforcing file access permissions

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 Is an interface, whenever you login to a Unix system you are placed


in a shell program. The shell's prompt is usually visible at the cursor's
position on your screen. To get your work done, you enter commands
at this prompt.

o The shell is a command interpreter; it takes each command and


passes it to the operating system kernel to be acted upon. It then
displays the results of this operation on your screen.

o Several shells are usually available on any UNIX system, each with
its own strengths and weaknesses.

o Different users may use different shells. Initially, your system


administrator will supply a default shell, which can be overridden or
changed. The most commonly available shells are:

 Bourne shell (sh)

 C shell (csh)
 Korn shell (ksh)

 TC Shell (tcsh)

 Bourne Again Shell (bash)

o Each shell also includes its own programming language. Command


files, called "shell scripts" are used to accomplish a series of tasks.

• Utilities

o UNIX provides several hundred utility programs, often referred to


as commands.

o Accomplish universal functions

 editing

 file maintenance

 printing

 sorting

 programming support

 online info

 etc.

o Modular: single functions can be grouped to perform more


complex tasks

Other Unix systems

• AIX – IBM’s version of Unix

• Linux – a variant of the Unix operating system, designed to run on personal


computers. Shares many features of Unix system V

• Solaris – by Sun Microsystems, based on systems V and BSD, used in the


atrium lab.
Logging On To The System

• 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*!).

• To login to your account

1. Type your username at the login prompt. UNIX is case sensitive.


When the password prompt appears, type in your password. Your
password is never displayed on the screen as a security measure. It
also is case sensitive.

• What happens after you successfully login depends upon your system:

o Many UNIX systems will display a login banner or "message of the


day". Make a habit of reading this since it may contain important
information about the system.

o Other UNIX systems will automatically configure your


environment and open one or more windows for you to do work in.

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.

Your Home Directory


• Each user has a unique "home" directory. Your home directory is that
part of the file system reserved for your files.

• 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.

Using UNIX Commands

• UNIX commands are executable files and built-in utility programs

• All UNIX commands are case sensitive. Most commands are in lower case.

• Commands typically have the syntax:

command - option(s) argument(s)

• Options

1. Modify the way that a command works

2. Usually consist of a hyphen followed by a single letter

3. Some commands accept multiple options which can usually be


grouped together after a single hyphen

4. A small number of commands require each option to be given


separately

5. Should be separated from the command name by a space

• Arguments
1. Most commands are used together with one or more arguments

2. Some commands assume a default argument if none is supplied

3. Arguments are optional for some commands and required by others

4. In most cases, multiple arguments should be separated from each


other by a space. They should be separated from the command name
and/or options by a space also.

• Examples

1. To use a command with no argument: the "date" command displays


the current time and date.

date

2. To use a command with a single argument: the "cd" command


changes to the directory of its argument, newdir

cd newdir

3. To use a command with both an option and an argument: the wc


command counts the number of words, characters, and lines in a file.
By using a different option you can choose what is counted.

wc -w file1 counts the words in file1


wc -c file1 counts the characters in file1
wc -l file1 counts the lines in file1

4. To use a command with several arguments: the cat command takes


the names of three files as arguments. It displays contents of file1
followed by file2 and then file3.

cat file1 file2 file3


5. To use a command with multiple options and default argument: the
ls command provides a long (l) listing of all (a) files and also shows
the file size (s). Because no argument is specified, the default
argument (current directory) will be used.

ls -als

• Multiple commands can be entered on one line if you separate each with a
semicolon.

cd newdir ; ls -l

• Commands can continue beyond one line by using a backslash (\)


character.

Special Characters

NEWLINE - initiates command execution


; - separates commands on same line
( ) - groups commands or identifies a function
& - executes a command in the background
| - pipe
> - redirects standard output
>> - appends standard output
< - redirects standard input
* - wildcard for any number of characters in a
file name
? - wildcard for a single character in a file
name
\ - quotes the following character
' - quotes a string preventing all substitutions
" - quotes a string allowing variable and
command substitution
` - performs command substitution
[ ] - denotes a character class in a file name
$ - references a variable
{ } - command grouping within a function
. - executes a command (if at beginning of line)
# - begins a comment
: - null command
• Examples

1. Use the * character in file names to match any number of characters.


The following command:

ls *.txt

2. Use the ? character in file names to match any single character. The
following command:

ls ???.txt

3. Use the [ ] characters in file names to match any character within a


range of characters. The following command:

ls chapter[1-3].txt

Terminal Control Keys

• Several key combinations on your keyboard usually have a special effect


on the terminal.

• The most common control keys are listed below:

CTRL-u - erase everything you've typed on


The command line

CTRL-c - stop/kill a command

CTRL-h - backspace (usually)

CTRL-z - suspend a command

CTRL-s - stop the screen from scrolling

CTRL-q - continue scrolling

CTRL-d - exit from an interactive program


Changing Your Password

• 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

• The "man" command

o The "man" command man gives you access to an on-line manual


which potentially contains a complete description of every command
available on the system.

o man can also provide you with one line descriptions of commands
which match a specified keyword

o The online manual is divided into sections:

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

o Examples of using the man command:

1. To display the manual page for the cp (copy files) command:

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.

2. By default, the man page in section 1 is displayed if multiple


sections exist. You can access a different section by specifying
the section. For example:

man 8 telnet

3. Keyword searching: use the -k option followed by the keyword.


Two examples appear below.

man -k mail
man -k 'copy files'

4. To view a one line description of what a command does:

whatis more

• who - shows who is on the system

who
who am i

• finger - displays information about users, by name or login name or userid

finger doe
finger userid

• cal – displays the calendar for a specified year, or month of year

cal 04 2000

Logging Off The System

• To finish using a UNIX system you must go through a process known as


"logging out" or "logging off".
• To logout enter the command logout or exit. If this does not, work press
Ctrl-d.

• If you have a .logout file in your home directory, the system will execute
any commands contained there.

Hierarchical File Structure

• 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.

• The number of levels is largely arbitrary, although most UNIX systems


share some organizational similarities.

• Example:

/ (root)
|
------------------------------
| | | |
/bin /usr /lib /dev
|
|
-------------------
| | |
/david /rave /john
| |
------------
| |
/software /schoolwk
File Types

The UNIX file system contains several different types of files:

• 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.

o Always located within/under a directory file

o Do not contain other files

• Directories (which are also files)

o Branching points in the hierarchical tree

o Used to organize groups of files

o May contain ordinary files, special files or other directories

o Never contain "real" information which you would work with


(such as text). Basically, just used for organizing files.

o All files are descendants of the root directory, ( named / ) located at


the top of the tree.

• Special Files

o Used to represent a real physical device such as a printer, tape


drive or terminal, used for Input/Ouput (I/O) operations

o Unix considers any device attached to the system to be a file -


including your terminal:

 By default, your terminal as the standard input file (stdin)


from which to read its input

 Your terminal is also treated as the standard output file


(stdout) to which a command's output is sent

o Usually only found under directories named /dev


• Pipes

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:

& ; ( ) | ? \ ' " ` [ ] { } < > $ - ! /

• Case Sensitivity !!!

• Length: can be up to 256 characters

• Extensions: may be used to identify types of files

libc.a - archive, library file


program.c - C language source file
xwd2ps.o - Object/executable code
mygames.Z - Compressed file
• Hidden Files: have names that begin with a dot (.) .these are files created
by the system (but you can also create them) For example:

.cshrc .login .mailrc .mwmrc

• Uniqueness: as children in a family, no two files with the same parent


directory can have the same name. Files located in separate directories can
have identical names.

• Reserved Filenames:

/ - the root directory (slash)


. - current directory (period)
.. - parent directory (double period)
~ - your home directory (tilde)

Pathnames

• Specify where a file is located in the hierarchically organized file system

• 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

File and Directory Commands

the commands usually have several options and accept wildcard characters as
arguments.

• ls - lists files

ls - show contents of working directory


ls file - list file, if it exists in working directory
ls dir - show contents of the directory dir
ls -a - shows all your files, including hidden ones
ls -al - give detailed listing of contents
ls -F - mark directories with "/" and executable
files with "*"
ls *.doc - show all files with suffix ".doc"

• 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 - browses/displays files one screen at a time. Similar to the more utility


in function but has different commands and options.

pg sample.f

• head - displays the first n lines of a file

head sample.f - display first 10 lines (default)


head -5 sample.f - display first 5 lines

• tail - displays the last n lines or n characters of a file

less sample.f - display last 10 lines (default)


less -5 sample.f - display last 5 lines
less -5c sample.f - display last 5 characters
• cat - dumps the entire file to the screen without paging. This command is
more useful for concatenating (hence the name "cat") files together than it is
for reading files.

cat myprog.c - diplays entire file


cat -b myprog.c - shows line numbers
cat file1 file2 > file3 - adds file1 and file2 to make
file3

• cp - copies files. Will overwrite unless otherwise specified. Must also have
write permission in the destination directory.

cp sample.f sample2.f - copies sample.f to sample2.f


cp -R dir1 dir2 - copies contents of directory
dir1 to dir2
cp -i file.1 file.new - prompts if file.new will be
overwritten
cp *.txt chapt1 - copies all files with .txt
suffix to directory hapt1
cp /usr/doc/README ~ - copies file to your home
directory
cp ~betty/index . - copies the file "index" from
user betty's home directory to current directory

• mv - moves files. Will overwrite unless otherwise specified. Must also have
write permission in the destination directory.

Also used to rename files

mv sample.f sample2.f - moves sample.f to sample2.f


mv dir1 newdir/dir2 - moves contents of directory
dir1 to newdir/dir2
mv -i file.1 file.new - prompts if file.new will be
overwritten
mv *.txt chapt1 - moves all files with .txt
suffix to directory chapt1

• rm - deletes/removes files or directories if file permissions permit.

rm sample.f - deletes sample.f


rm chap?.txt - deletes all files with chap as the
first four characters of their name
and with .txt as the last four
characters of their name
rm -i * - deletes all files in current directory
but asks first for each file
rm -r /olddir - recursively removes all files in the
directory olddir, including the
directory itself

• file - identifies the "type" of file

file filename

• find - finds files. The syntax of this command is:

find pathname -name filename -print

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'.

find . -name mtg_jan92 -print - looks for the file


mtg_jan92 in current directory
find ~/ -name README -print - looks for files called
README throughout your home directory
find . -name '*.fm' -print - looks for all files
with .fm suffix in current directory
find /usr/local -name gnu -type d -print
- looks for a directory called gnu within the
/usr/local directory

• grep (global regular expression print) – searches for a special pattern in


file(s). if the specified pattern is found, the line containing the pattern is
displayed on your terminal.

grep unix myfile - will display all lines in myfile


containing unix

grep “#include <stdio.h>” *.c – will display all lines in


all files with extension c in current directory that
contain the string above.
Grep –i UnIx myfile – i option to ignore distinction
between upper and lower case.

who | grep –I david – find out if david is logged in

• diff - comparing two files or directories. Indicates which lines need be


added (a), deleted (d) or changed (c). Lines in file1 are identified with a (<)
symbol: lines in file2 with a (>) symbol

diff file1 file2 - compares file1 to file2


diff -iw file1 file2 - compares two files ignoring
letter case and spaces
diff dir1 dir2 - compares two directories
showing files which are
unique to each and also,
line by line differences
between any files in common.

• 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 - link one file name to another. The command syntax is:

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.

ln results.1 last.run - links filename "last.run"


to the real file results.1
in the current directory.

• 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.

An output file can be specified with the -o flag.

sort addresses - sorts the file addresses and displays


output on screen

sort -o sorted addresses - sorts the file addresses


and writes output to the file called sorted.

sort -u -o mail_labels addresses - removes all


duplicate lines from the file addresses and writes
the output in the file mail_labels.

sort +2 -4 addresses - sorts the file by its third and


fourth fields. Note that +2 means to skip first
two fields and -4 means to stop after the fourth
field.

• 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

• cd - change to specified directory. May specify either the absolute or


relative pathname. cd with no pathname changes to your home directory.
cd /usr/local - change to /usr/local
cd doc/training - change to doc/training in
current directory
cd .. - change to parent directory
cd ~/data - change to data directory in
home directory
cd ~joe - change to user joe's home
directory
cd - change to home directory

• rmdir - remove directory. Directories must be empty before you remove


them.

rmdir project1

To recursively remove nested directories, use the rm command with the -r


option:

rm -r dirctory_name

m-tools

Working with the disk drive and floppy disks

mdir – see contents of floppy disk

mcopy filename a: - copy filename to disk using


same name

mcopy filename a:newname – copy filename to


disk giving it a newname

mcopy a:filename . – Copy filename from floppy


disk to current directory

mcopy a:filename ~/mywork – copy filename from


floppy disk to mywork
directory in home directory

mdel filename – delete filename from floppy


disk

eject – taking the floppy disk out of drive

• A summary of commands and utilities related to the UNIX file system


appears below. See the corresponding man pages for detailed information.

awk -search for and process patterns in a file,


cat -display, or join, files
cd -change working directory
chgrp -change the group that is associated with a
file
chmod -change the access mode of a file
chown -change the owner of a file
comm -compare sorted files
cp -copy files
df -display the amount of available disk space
diff -display the differences between two files
du -display information on disk usage
file -display file classification
find -find files
fsck -check and repair a file system
grep -search for a pattern in files
head -display the first few lines of a file
ln -create a link to a file
lp -print files (System V)
lpr -print files (Berkeley)
ls -list information about files
mkdir -create a directory
more -display a file one screen at a time
(System V)
mv -move and/or rename a file
od -dump a file
pg -display a file one screen at a time
(Berkeley)
pr -paginate a file
pwd -print the working directory
rm -remove (delete) files
rmdir -remove (delete) a directory
sed -stream editor (non-interactive)
sort -sort and/or merge files
spell -check a file for spelling errors
tail -display the last few lines of a file
tar -store or retrieve files from an archive
file
umask -set file creation permissions
uniq -display the lines in a file that are
unique
wc -counts lines, words and characters in a
file
whatis -list man page entries for a command
whereis -show where executable is located in path
which -locate an executable program using "path"

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.

• Permissions for a file or directory may be any or all of:

r - read
w - write
x - execute = running a program

• Each permission (rwx) can be controlled at three levels:

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:

which are displayed as: -rwxrwxrwx

Example outputs from the ls -l command:

-rw------- 2 smith staff 3287 Apr 8 12:10 file1

column 1 – consists of 10 characters. The first


indicates the file type (d for directory) and the rest
indicate the file’s access mode. Here User has read
and write permission. Group and
others have no permissions.

Column 2 – number of links to the file

Column 3 – owner’s name

Column 4 – group’s name

Column 5 – size of file in bytes

Column 6 – date and time of last modification

Column 7 – name of file

-rw-r--r-- 2 smith staff 13297 Apr 8 12:11 file2

- User has read and write permission. Group and


others can only read the file.

drwxr-x--- 2 smith staff 1024 Jun 17 10:00 SCCS

- This is a directory. The user has read, write and


execute permission. Group has read and execute
permission on the directory. Nobody else can access
it.

• chmod - is used to change access permissions for files which you own. The
syntax is:

chmod permission_triads filename


[who][action][permissions]

where:

who action permissions

u = user + = add r = read


g = group - = remove w = write
o = other x = execute
a = all

Examples:

chmod a+r sample.f

- Adds read permission for all users to the file


sample.f.

chmod o-r sample.f


- Removes read permission for others to the file
sample.f.

chmod og+rx prog*

- Adds read and execute permissions for group and


others to all files which contain "prog" as the
first four characters of their name.

chmod +w *

- Adds write permission for user to all files in


current directory.

• File access permissions can also be changed by a numerical (octal) chmod


specification. Read permission is given the value 4, write permission the
value 2 and execute permission 1.

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)

So access permissions can be expressed as three digits. For example:

user group others

chmod 640 file1 rw- r-- ---


chmod 754 file1 rwx r-x r--
chmod 664 file1 rw- rw- r--
Standard UNIX File System

• There is no single standard UNIX file structure. Most UNIX systems


however, follow a general convention for filesystem organization at the
highest level.

/(root) - The top level directory referred to


as root.
Contains all files in the file
system.

/bin - Executable files for standard UNIX


utilities

/dev - Files that represent input/output


devices

/etc - Miscellaneous and system


administrative
files such as the password file and
system
start up files.

/lib - UNIX program libraries

/tmp - Temporary space that can be used by


programs or users.

/usr/bin - More UNIX utilities. By


convention /bin
contains standard utilities and
/usr/bin
contains less common utilities.

/usr/bin/X11 - X windows binaries

/usr/lib - More UNIX libraries

/usr/lib/X11 - X windows libraries


/usr/local - Programs installed by local site

/usr/ucb - Berkeley utilities

/u - User home directories

/var - Variable sized files - can grow and


shrink dynamically, such a users mail
spool and print spool files.

Unix Editors

• What is a text editor?

o A text editor is a program, which enables you to create and


manipulate character data (text) in a computer file.

o A text editor is not a word processor although some text editors do


include word processing facilities.

o Text editors often require "memorizing" commands in order to


perform editing tasks. The more you use them, the easier it becomes.
There is a "learning curve" in most cases though.

• There are several standard text editors available on most UNIX systems:

o ed - standard line editor

o ex - extended line editor

o vi - a visual editor; full screen; uses ed/ex line-mode commands for


global file editing

o sed - stream editor for batch processing of files

• In addition to these, other local "favorites" may be available:

o emacs - a full screen editor and much more

o pico - an easy "beginner's" editor

o lots of others
The Standard Display Editor - vi

• vi supplies commands for:

o inserting and deleting text

o replacing text

o moving around the file

o finding and substituting strings

o cutting and pasting text

o reading and writing to other files

• vi uses a "buffer"

o While using vi to edit an existing file, you are actually working on a


copy of the file that is held in a temporary buffer in your computer's
memory.

o If you invoked vi with a new filename, (or no file name) the


contents of the file only exist in this buffer.

o Saving a file writes the contents of this buffer to a disk file,


replacing its contents. You can write the buffer to a new file or to
some other file.

o You can also decide not to write the contents of the buffer, and
leave your original file unchanged.

• vi operates in two different "modes":

o Command mode

 vi starts up in this mode

 Whatever you type is interpreted as a command - not text to


be inserted into the file.
 The mode you need to be in if you want to "move around"
the file.

o Insert mode

 This is the mode you use to type (insert) text.

 There are several commands that you can use to enter this
mode.

 Once in this mode, whatever you type is interpreted as text


to be included in the file. You can not "move around" the file
in this mode.

 Must press the ESC (escape) key to exit this mode and
return to command mode.

vi Commands

• Entering vi

vi filename - The filename can be the name of an


existing file or the name of the
file
you want to create.
view filename - Starts vi in "read only" mode.
Allows
you to look at a file without the
risk
of altering its contents.

• Exiting vi

:q - quit - if you have made any changes,


vi
will warn you of this, and you'll
need
to use one of the other quits.
:w - write edit buffer to disk
:w filename - write edit buffer to disk as
filename
:wq - write edit buffer to disk and quit
ZZ - write edit buffer to disk and quit
:q! - quit without writing edit buffer to
disk

• Positioning within text

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

a - append text after cursor *


A - append text at end of line *
i - insert text before cursor *
I - insert text at beginning of line *
o - open a blank line after the current
line for text input *
O - open a blank line before the current
line for text input *

* Note: hit ESC (escape) key when finished


inserting!

• 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

cw - replace word with text *


cc - replace line with text *
c0 - change to beginning of line *
c$ - change to end of line *
C - change to end of line *
c( - change to beginning of sentence *
c) - change to end of sentence *
c{ - change to beginning of paragraph *
c} - change to end of paragraph *
r - overtype only 1 character
R - overtype text until ESC is hit *
J - join two lines

* Note: hit ESC (escape) key when finished changing!

• Copying lines

yy - "yank": copy 1 line into buffer


nyy - "yank": copy n lines into buffer
p - put contents of buffer after current
line
P - put contents of buffer before
current
line

• Moving lines (cutting and pasting)

ndd - delete n lines (placed in buffer)


p - put contents of buffer after current
line
P - put contents of buffer before
current
line

• Searching / Substituting

/str - search forward for str


?str - search backward for str
n - find next occurrence of current
string
N - repeat previous search in reverse
direction

The substitution command requires a line range


specification. If it is omitted, the default
is the current line only. The examples below
show how to specify line ranges.

:s/old/new - substitute new for first occurrence


of old in current line
:s/old/new/g - substitute new for all occurrences
of old in current line
:1,10s/old/new - substitute new for first occurrence
of old in lines 1 - 10
:.,$s/old/new - substitute new for first occurrence
of old in remainder of file
:.,+5s/old/new - substitute new for first occurrence
of old in current line and next 5
lines
:.,-5s/old/new - substitute new for first occurrence
of old in current line and previous
5 lines
:%s/old/new/g - substitute new for all occurrences
of old in the entire file
:%s/old/new/gc - interactively substitute new for
all
occurrences of old - will prompt
for
y/n response for each substitution.

• Miscellaneous commands

u - undo the last command (including


undo)
. - repeat last command
xp - swap two adjacent characters
m[a-z] - set a marker (a - z)
'[a-z] - go to a previously set marker (a -
z)
:!command - execute specified UNIX command
:r filename - read/insert contents of filename
after
current line.
:1,100!fmt - reformat the first 100 lines
:!fmt - reformat the entire file

vi Options

• You can change the way vi operates by changing the value of certain
options which control specific parts of the vi environment.

• To set an option during a vi session, use one of the commands below as


required by the option:

:set option_name
:set option_name=value
• Some examples of the more common options are described below.

:set all - shows all vi options in effect

:set ai - set autoindent - automatically


indents
each line of text

:set noai - turn autoindent off

:set nu - set line numbering on

:set nonu - turn line numbering off

:set scroll=n - sets number of lines to be scrolled


to n. Used by screen scroll
commands.

:set sw=n - set shiftwidth to n. Used by


autoindent
option.

:set wm=n - set wrapmargin to n. Specifies


number
of spaces to leave on right edge of
the
screen before wrapping words to
next
line.

:set showmode - reminds you when you are inserting


text.

:set ic - ignore case of characters when


performing a search.

• Options can be set permanently by putting them in a file called .exrc in


your home directory. A sample .exrc file appears below. Note that you do not
need the colon (:) as part of the option specification when you put the
commands in a .exrc file. Also note that you can put them all on one line.

set nu ai wm=5 showmode ic


pico: One Alternative to vi

• 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.

• Editing commands are entered using control-key combinations. The most


important commands are displayed at the bottom of the screen, facilitating
ease of use.

• 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.

• The basic commands provided by pico include:

CTRL-G - get help


CTRL-X - exit
CTRL-O - write out to a file
CTRL-J - justify text
CTRL-R - read a file
CTRL-W - where is (search)
CTRL-Y - previous page
CTRL-V - next page
CTRL-^ - mark cursor position as start of selected
text
CTRL-K - cut text
CTRL-U - uncut text / paste
CTRL-T - check spelling
CTRL-C - cursor position information
CTRL-F - move forward a character
CTRL-B - move backward a character
CTRL-P - move to the previous line
CTRL-N - move to the next line
CTRL-A - move to the beginning of the current line
CTRL-E - move to the end of the current line
CTRL-L - refresh the display
CTRL-D - delete the character at the cursor
position
CTRL-I - insert a tab at the current cursor
position

Note that cursor positioning can also be


accomplished by
using the up, down, right and left arrow keys.

• A few additional comments/notes:

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.

o Context sensitive help is a feature included with pico.

o A file browser is included as part of the "write" and "read" file


commands.

Editing command line

• 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

• To print the text of a file:

lpr filename – prints 80 characters per line and

60 lines per page

lpr file1 file2 – will print both files for the

lp – same as lpr

lpq – displays a list of all jobs waiting on the

print queue

lprm job_num – cancel a print job after it has

been sent to the print queue


Compiling programs

There are two main C compilers in the unix operating system: acc and gcc.

To compile a single program type :

acc progname.c - this command will

compile and link

progname to generate an

executable called a.out

( you can also use gcc progname.c)

acc –o newname progname.c - this command

will compile and

link progname to

generate an

executable called

newname

acc –c progname.c - this command will only

compile progname to

generate an object
file called progname.o

acc progname.c progname2.c – this will

compile and link both


files to generate one
executable called a.out

What is the Shell?

• 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.

• The shell is your interface to the operating system. It acts as a command


interpreter; it takes each command and passes it to the operating system. It
then displays the results of this operation on your screen.

• There are several shells in widespread use. The most common ones are
described below.

Bourne shell (sh)


Original Unix shell written by Steve Bourne of Bell Labs. Available on all
UNIX systems. The Bourne shell does provide an easy to use language with
which you can write shell scripts.
C shell (csh)
Written at the University of California, Berkley. As it name indicates, it
provides a C like language with which to write shell scripts.
Korn shell (ksh)
Written by David Korn of bell labs. It is now provided as the standard shell
on Unix systems. Provides all the features of the C and TC shells together
with a shell programming language similar to that of the original Bourne
shell.

TC Shell (tcsh)
Available in the public domain. It provides all the features of the C shell

Bourne Again Shell (bash)


Public domain shell written by the Free Software Foundation under their
GNU initiative. Provides all the interactive features of the C shell (csh) and
the Korn shell (ksh). Its programming language is compatible with the
Bourne shell (sh).

• 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:

$ (dollar sign) - sh, ksh, bash


% (percent sign) - csh, tcsh

• Depending upon the shell, certain features will be available.

Processes

• Whenever you enter a command at the shell prompt, it invokes a program.


While this program is running it is called a process. Your login shell is also a
process, created for you upon logging in and existing until you logout.

• UNIX is a multi-tasking operating system. Any user can have multiple


processes running simultaneously, including multiple login sessions. As you
do your work within the login shell, each command creates at least one new
process while it executes.

• Process id: every process in a UNIX system has a unique PID - process
identifier.

• ps - displays information about processes. Note that the ps command


differs between different UNIX systems - see the local ps man page for
details.

To see your current shell's processes:

$ 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:

kill [-signal] process_identifier(PID)

Examples:

kill 63878 - kills process 63878


kill -9 1225 - kills (kills!) process 1225.
Use if
simple kill doesn't work.
kill -STOP 2339 - stops process 2339
kill -CONT 2339 - continues stopped process 2339
kill -l - list the supported kill signals

You can also use CTRL-C to kill the currently running process.

• Suspend a process: Use CTRL-Z.

• Background a process: Normally, commands operate in the foreground -


you cannot do additional work until the command completes. Back
grounding a command allows you to continue working at the shell prompt.

To start a job in the background, use an ampersand (&) when you invoke the
command:

myprog &

To put an already running job in the background, first suspend it with


CRTL-Z and then use the "bg" command:
myprog - execute a process
CTRL-Z - suspend the process
bg - put suspended process in
background

• Foreground a process: To move a background job to the foreground, find


its "job" number and then use the "fg" command. In this example, the jobs
command shows that two processes are running in the background. The fg
command is used to bring the second job (%2) to the foreground.

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

• Some notes about background processes:

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

• Redirection refers to changing the shell's normal method of handling


standard output (stdout), standard input (stdin) and standard error (stderr)
for processes.

• 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.

> - redirect stdout (overwrite)


>> - redirect stdout (append)
< - redirect stdin

• Examples:

mail tony < memo - uses the file memo as


input
to the mail program
ls -l > my.directory - redirects output of ls
-l
command to a file
called
my.directory. If the
file
already exists, it is
overwritten
cat Mail/jsmith >> Oldmail - appends the contents of
Mail/jsmith to the file
Oldmail (does not
overwrite)
Pipes

• 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:

command1 [arguments] | command2 [arguments]

• Pipes accomplish with one command what otherwise would take


intermediate files and multiple commands. For example, operation 1 and
operation 2 are equivalent:

Operation 1
who > temp
sort temp

Operation 2
who | sort

• Pipes do not affect the contents of the input files.

• 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

• A filter is a command that processes an input stream of data to produce an


output stream of data.

• 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.

Das könnte Ihnen auch gefallen