You are on page 1of 50

7/30/2012

UNIX
Dr. Emmanuel Shubhakar Pilli
Graphic Era University

Unix History
q

In early 60s, computers were expensive, had


minimal memory and were single user
u

All activities were designed to make things easy for the


computer

In the mid 60s, MIT, Bell Labs, and General


Electric teamed to build an OS for a large
computer, the GE-645 that would accommodate
1,000 simultaneous users
q Multics (Multiplexed Information and Computing
Service)
q In March 69, Bell pulled out of the project
q

Three people could overload it.


2

7/30/2012

The Roots of Unix


q
q
q
q

Ken Thompson had been using Multics to run


Space Travel but it cost $70 a run
Thompson found a cast-off PDP-7 with the intent
to write a file system
This file system became the roots of Unix
Unix was originally called UNICS, a pun on
Multics (Uniplexed Information and Computing
Service)
Unix struggled along on an obsolete, underpowered machine until the PDP-11 was released

Lessons Learned From Multics


q
q

q
q

Tree-structured file system


Separate, identifiable program to do command
interpretation (the shell)
Structure files with no structure except as a
sequence of bytes, in most cases with no
interpretation by the OS
Text files as simple sequences of characters
separated by new-lines
Semantics of I/O operations (read and write) as
referring to a file handle, buffer, and a count, thus
concealing the underlying structure of the device
4

7/30/2012

The Birth of Unix


In 1970, Thompson and Ritchie convinced the
Bell Labs Patent department to fund purchase of
a PDP-11/20 for use in text processing
q This ruse allowed them to develop Unix
q We knew there was a scam going on--wed
promised a word processing system, not an
operating system. Dennis Ritchie
q BTL Patent department became first users of
Unix, taking over the machine but giving
Thompson and Ritchie enough funds to purchase
a PDP-11/45
q

Thompson, Ritchie with PDP-11

7/30/2012

Unix Philosophy
Write programs that do one thing and do it well
q Write programs that work together, allowing the
output of one to become the input of another
q

Write programs that handle text streams, because that


is a universal interface

Dont hesitate to build new programs to do a job

The Unix Explosion


February 1973, the Third Edition of the UNIX
PROGRAMMERS MANUAL appeared, along
with a C compiler and debugger
q Thompson and Ritchie submitted an abstract for a
presentation to the ACM Symposium on
Operating Systems Principles
q When the paper was published in
Communications of the ACM in July 1974, Unix
took off
q In 1983, they shared the ACMs Turing Award for
their work
q

7/30/2012

The Law
January, 1949 DOJ filed an antitrust complaint
against Western Electric and AT&T
q In January, 1956, a consent decree was entered
q

u
u

AT&T and Western Electric were enjoined from


commencing ... manufacture for sale or lease any
equipment [other than that used in providing telephone
or telegraph services],
from engaging ... in any business not of a character or
type engaged in by Western Electric or its subsidiaries
AT&T was enjoined from engaging in any business
other than the furnishing of common carrier
communications services
9

Results of Consent Decree


AT&T was required to reveal what patents it held
and to license to anyone at nominal fees
q The lawyers at Bell Labs, a wholly owned
subsidiary of AT&T and Western Electric were
conservative
q

No business but phones and telegrams

Thompsons presentation at the ACM SOSP put


them in a quandary
u
u

Suddenly requests started flowing in for Unix licenses


Computers werent phones and telegrams thus putting
them in danger of antagonizing the Justice Department
10

7/30/2012

AT&T Unix Policy


To preclude any conflict with the Consent Decree,
AT&T would license Unix but make it clear that it
had no intention of pursuing software as a
business
q Bell System support policy:
q

no advertising
no support
no bug fixes
payment in advance

11

The Growth of Unix


q
q
q

q
q
q

May 1974 - nearly a dozen


Spring 75 - three dozen
June 75 - plus 20 new installations
October 75 - 60 installations
March 76 - 80 installations
September 76 - 138 installations
Canada
Great Britain
Australia 4
Israel
The Netherlands

13
10
3
3

Austria
Belgium
Germany
Venezuela
United States

1
1
1
1
101
12

7/30/2012

Berkeley Unix
Robert Fabrey, of UC Berkeley, was on the SOSP
program when Thompson presented his paper
q Together with the Math department, he purchased
a PDP-11/45 and had Unix running by Jan 74
q By 78 Bill Joy had produced a Pascal compiler
and began producing the Berkeley Software
Distribution (BSD) for $50 containing:
q

u
u

Unix Pascal System


Ex Text Editor

13

BSD Unix and the Internet


DARPA wanted its contractors on a common
computer system
q In late 1980, Bill Joy convinced DARPA that the
software platform should be Unix, DARPA had
already decided on the DEC VAX for the
hardware
q TCP/IP, the language of the internet, was added
to BSD 4.2 at DARPAs request, along with the
Berkeley Fast File System
q Unix emerged from this as the machine that
fueled the original internet (ARPANET)
q

14

7/30/2012

Dueling Unixes
Up until 78, Unix meant AT&Ts operating system
q After this, there were two principle flavors of Unix
q

u
u

BSD
System V

Commercially, both were a success but were not


totally compatible with each other
q This led to the formation of the Open Software
Foundation and the competing Unix International
q

15

Why Unix Instead of Other OSs?


q

Portability
u
u

q
q

q
q
q

Written in C, only a small portion is in assembly


Runs on micros, minis, main frames, and super
computers

Ran on (relatively) inexpensive hardware


Designed to be adaptable to changing user
environments
It was inexpensive
Source was available
Permits access to its operating features
u

Wonderful programmers environment


16

7/30/2012

Challenges of Unix
q

Powerful system with many tools and capabilities


u

Takes considerable effort and time to master

Most commands are short, terse, and nondescriptive


q Relatively unforgiving of user mistakes
q

17

18

7/30/2012

19

Major Personalities
q
q
q
q
q
q
q
q
q
q
q
q

Ken Thompson
Dennis Ritchie
Joe Ossanna
Rudd Canaday
Robert Fabrey
Steve Johnson
Stephen Bourne
Doug McIlroy
Kirk McKusick
Armando Stettner
Heinz Lycklama
Bill Joy

q
q
q
q
q
q
q
q
q
q
q
q

Co-Inventor of Unix
Co-Inventor of Unix
Author of troff
Original file system collaborator
Obtained DARPA funding for BSD
Author of lint and yacc
Author of the Bourne shell, sh
Originator of pipe concept
Wrote Berkeley Fast File System
Got DEC to acknowledge Unix
Chaired first Unix standards group
Author of csh, ex, vi, termcap, and
much of BSD
20

10

7/30/2012

Ken Thompson and Dennis Ritchie

21

Whats An Operating System?


A program that acts as an intermediary between
the computer hardware and the user
q Primary goal is to make the computer system
convenient to use
q Secondary goal is to use the hardware resources
efficiently
q Unix can be divided into roughly four components
q

u
u
u
u

the hardware
the kernel, the hardware interface unique to a platform
the shell and other system utilities
the application programs
22

11

7/30/2012

The System View of Unix

23

So Why Unix?
q

Unix is a multi-tasking operating system based


around a hardware specific kernel
u

Multi-tasking/multi-processing means more than one


command, program, process can seemingly be in
simultaneous execution

Hardware specific kernel means that a small core


piece of functionality is coded specifically for the
platform
u

All the rest of Unix remains unchanged

24

12

7/30/2012

Unix Shells
A shell is the user interface or set of programs
user to interact with Unix and process commands
q Common shells are:
q

Bourne
u C
u Korn
u

When a shell is executed, it


inherits environmental variables set by the previous
shell
u stops the previous shell
u becomes the current shell
u

25

Bourne Shell (sh)


Written by Dr Steven Bourne of Bell Labs
q Both a command interpreter and a high-level
programming language
q Typically the default Unix shell
q Fast
q

Approximately 20 times faster than C shell because it is


simpler and doesnt carry as much baggage

26

13

7/30/2012

C Shell (csh)
Written by Bill Joy at University of California at
Berkeley
q Slower and more complex than Bourne shell, but
has facilities to make it more user friendly
q

alias
u history
u job control
u filename completion
u

27

Korn Shell (ksh)


Written by David Korn of AT&T, released in 1986
q Includes features of both the Bourne and C shell
q Introduces several new user interface features
including command line editing
q Adds features that improve its usefulness as a
programming language
q

report formatting capabilities


u built-in arithmetic
u data types
u

28

14

7/30/2012

Superuser
q

A user with essentially all privileges


u

Often referred to as root privileges

Allowed access to all files


q Allowed to run all commands
q System administrator has superuser privileges
q Can be very dangerous, all the Unix built in
protections are by-passed
q

29

Password Security
Unfortunately, password security is a necessity
q Good passwords have several characteristics
q

Minimum of six (6) characters


u Mixture of alpha and numeric characters
u Mixture of upper and lower case
u No real words
l Susceptible to a dictionary attack
u

It is also good practice to avoid:


Family names
u Birthdates
u Pets names
u Any other personal data
u

30

15

7/30/2012

Logging In and Out


login: username
q password: your_password
q

To change your password


l clyde% passwd
l Changing password for ??? on clyde.
l Old password: your_current_password
l New password: your_new_password
l Retype new password: your_new_password
l clyde%

To logout
u
u

clyde% exit
or clyde% logout
31

To connect via internet


telnet your.favorite.computer
q or telnet 127.0.0.0
q Your favorite computer may respond with
something like:
q

SunOS UNIX (yof)


login: username
Password: your_password
Last Login: Sat Aug 7 10:15:50 on ttyp0
SunOS Release 4.1.3_U1 (GENERIC) ..........
yof%

32

16

7/30/2012

What happens when you login?


q

Unix runs the login program


u

Then the shell specified in /etc/passwd is


executed and user is placed in his HOME
directory
u

If it exists, .login script is executed

If it exists, .cshrc (if csh is your default shell) is


executed

So whats the difference? Why two initialization


files?
u
u

.login is executed only at login


.cshrc is executed every time a new shell is spawned
33

exit or logout
q

exit terminates your current shell


u

if it is also your login shell, exit will exit and logout

logout terminates a login shell

34

17

7/30/2012

Process Concepts
q
q

q
q
q

Early or simple (DOS) computer systems allowed


only one program to be executed at a time
Modern computer systems like Unix are multitasking, allowing multiple programs to be loaded
and executed concurrently
This requires more control of programs, leading to
the notion of a process
A process is a program in some stage of
execution
A modern computer system is a collection of
processes
u

Operating system processes and User processes


35

Unix Processes
Every command executed on Unix is a process
q Unix processes are hierarchical
q

Parent and child processes

Every process is automagically assigned three


standard files
input
u output
u error
u

36

18

7/30/2012

Process Blocks
q

Each process has a process block that includes


u
u
u
u

a process identifier
the process state
the value of the processs program counter
other information specific to the process such as
l memory limits
l files in use
l processor time used
l ....

37

Command Components
q

Commands consist of:


Command name
u Options, or flags
u Arguments
u

Arguments are the things that the command will


operate on
q Options modify the behavior of the command
q

38

19

7/30/2012

Command Notation
command name
q [ ] optional arguments or options
q

can be nested
l [ arg1 [ arg2 ] ]
l [...]

| is an OR condition
u

arg1 | arg2

39

who
q

Who is on the system and info about their login


u

User_name, terminal_name, login_time

Syntax: who [am i]


q Example:
q

clyde% who
root
console
krf
ttyp0

Jul 22 09:36
Aug 7 10:32 (dal-tx2-33.ix.ne)

am i option lists requestor info only


q Related command: whoami
q

Lists requestors user_name only

40

20

7/30/2012

CALendar - cal
q

Prints a calendar for the specified year


u

Default is current year

Syntax: cal [ [ month ] year ]


u
u

month - number from 1 to 12


year - number from 1 to 9999

Note: September 1752 is odd, 11 days were


skipped to make up for lack of prior leap year
adjustments
q Examples:
q

clyde% cal
clyde% cal 9 1752
41

date
Display or set the date
q Syntax:date [-u] [-a [-] sss.fff] [yymmddhhmm [.ss]]
q

u - display date in GMT, default is local time


u a - slowly adjust system clock
u yymmddhhmm [.ss] - set system date and time
l Only superuser can set the date and time
u

If the argument begins with a +, the output of date


is under user control
q Examples:
q

clyde% date
clyde% date -u
clyde% date +%T
42

21

7/30/2012

finger
Displays information about users
q Syntax: finger [options] user_name
q By default, finger displays user_names:
q

u
u
u
u
u
u
u

login name
full name
terminal name
idle time
login time
location
first line of .plan file

43

mail
Read or send electronic mail messages
q Syntax: mail [-deHinNUv] [-f [filename | +folder] ]
[-T file]
or: mail [-dFinUv] [-h number] [-r address] [-s
subject] recipient ...
q First form is for reading mail, second is for
sending mail
q For this class:
q

u
u
u

Read mail: mail


Send mail: mail -s subject recipient
or: mail -s subject recipient < file_name
44

22

7/30/2012

MANual pages - man


Read online documentation or find reference
pages by keyword
q Syntax: man [[section] title]
or: man [[-k keyword] | [-f filename]]
q Examples:
q

clyde% man ps
clyde% man -k compile
clyde% man -f /var/spool/mail
clyde% man link
clyde% man 2 link
clyde% man 8 link
45

whois
q

Internet white pages


u
u

Syntax: who [-h host] identifier


u

Searches for a TCP/IP directory entry


Used to find people or domain owners/contacts
host - name of host computer to use for lookup
l Default is nic.ddn.mil, which no longer supports
anything but MILNET, Current host is rs.internic.net
l Names are stored as
last name, first name, titles

Examples:
clyde% whois -h rs.internic.net Earthlink.com
clyde% whois -h rs.internic.net Krol, Ed*
46

23

7/30/2012

show ProcesSes - ps
Displays the status of current processes
q Syntax: ps [ [-] acCegjklnrSuUvwx] [-tx] | [num]
[kernel-name] [c-dump-file] [swap-file]
q Default is to display only processes with your
effective user ID
q We will only use the following options:
q

a - include processes that are not owned by you


u x - show processes that dont have a controlling
terminal
u r - show only running processes
u

Example:
clyde% ps -a
47

talk Example
clyde% talk krf
Message from Talk_Daemon@clyde at 17:39 ...
talk: connection requested by krf@clyde.dcccd.edu.
talk: respond with: talk krf@clyde.dcccd.edu
Respond as shown and a split screen will be
displayed with your input in one half and your talkmates output in the other
q To end the session, type Control-c
q

48

24

7/30/2012

MESsaGe - mesg
Permit or deny messages on your terminal
q Syntax: mesg [n] [y]
q

u
u
u

default reports current state without changing it


n - forbids messages to be sent to you from talk or write
y - reinstates permission

49

Print Working Directory - pwd


Displays the pathname of the current working
directory
q Syntax: pwd
q Example:
q

clyde% pwd
/home/clyde/krf
clyde%

50

25

7/30/2012

Set TTY - stty


Display and set terminal options
q Syntax: stty [-ag] [option] ...
q

u
u

a - Report all option settings


g - Report current settings in a format that can be used
as an argument to another stty command\

Most common user use is to remap control keys,


such as erase
To set the erase command to be a backspace:
clyde% stty erase ^h
u Key mapping good candidate for inclusion in your .login
file
u

51

mail(pine)
PINE 4.10

MAIN MENU

Folder : INBOX No Messages

? HELP

- Get help using Pine

C COMPOSE MESSAGE

- Compose and send a message

- View messages in current folder

MESSAGE CHECK

L FOLDER LIST

- Select a folder to view

A ADDRESS BOOK

- Update address book

S SETUP

- Configure Pine Options

Q QUIT

- Leave the Pine program

? Help

P PrevCmd

O OTHER CMDS > [ListFldrs] N NextCmd

R RelNotes
K KBlock
52

26

7/30/2012

File System
q

Unix provides a hierarchical, or tree-like, file


system
Supports two main objects
l Files
l Directory files, or directories
u From Unixs view, there is no difference between the
two
u

From the users view, files are data that you want,
directories are containers of files
q In reality, files are simply a stream of bytes
q

Critical design philosophy of Unix


53

53

54

54

Tree Structure

27

7/30/2012

Files and Directories

55

55

File Names
Every file has a filename
q Current versions of Unix allow up to 255
characters, older versions limit to 14
q Almost any character can be used in a filename
q

Sticking with the following will save many problems


lComma ( , )
l Uppercase letters (A-Z)
lDash ( - )
l Lowercase letters (a-z)
lUnderscore ( _ )
l Numbers (0-9)

Period ( . ) can be used as any other character and


may denote special files
Exception is the root directory, which is always named /
l No other file can use this name

56

56

28

7/30/2012

Other File Name Rules


q

Other characters may be used by escaping them


with a \ character
u
u

Willie\*Weasil - displays as Willie*Weasil


Much confusion can result from this

File names are case sensitive


u

my_file, My_File, and MY_FILE are all unique names

57

57

File Access Controls


Unix provides file security with access levels and
permissions
q Access levels define who specific permissions
belong to
q

u
u
u

user (u)
group (g)
other (o)

Permissions define what the members of an


access level are allowed to do
execute (x)
u read (r)
u write (w)
u

58

58

29

7/30/2012

Access Controls Provide Security


Generally accepted that there are files users
cannot access or modify
q This provides security for:
q

System
u Work groups
u Individual users
u

59

59

Unix Has Many Filesystem Types


Use
Native

ifs

Digital
Unix
ufs

NFS

nfs

nfs

nfs

nfs

NFS

nfs

nfs

nfs

CD

cdrfs

cdfs

cdfs

HS

hsfs

hsfs

iso
9660

iso
9660

ufs

swap
DOS

pcfs

dos

msdos

proc

proc

proc

s5

hfs

sysv

Swap

AIX

HP
Unix
hfs

SCO
Unix
EAFS

Sun
OS
4.2

Solaris

IRIX

Linux

ufs

efs

ext2

DOS
/proc

procfs

other

advfs

vxfs

S51K

lofs

minux

60

60

30

7/30/2012

MaKe DIRectory - mkdir


Creates new directories
q Syntax: mkdir [ -p ] dirname
q

p - allows missing parent directories to be created as


needed

Requires write permission in the parent directory

61

61

ReMove file(s) - rm
Removes (deletes) files
q Syntax: rm [ - ] [ -fir] filename ...
q

u
u
u
u

- Treat following arguments a filenames, so you can


remove files starting with a minus sign
f - Force files to be removed without displaying
permissions, asking questions, or reporting errors
i - Interactive, ask before removing each file
r - Recursively delete the contents of a directory, its
subdirectories, and the directory itself

62

62

31

7/30/2012

ReMove DIRectory - rmdir


Removes empty directories
q Syntax: rmdir directory...
q An error will be reported if the directory is not
empty
q

63

63

LiSt directory - ls
List the contents of a directory
q Syntax: ls [-aAcCdfFgilLqrRstu1] filename
q

u
u
u
u
u
u

a - List all entries, without this, files beginning with a .


are not listed
c - Sort on time of last edit
l - List in long form, giving mode, number of links,
owner, size, and time of last modification
R -Recursively list subdirectories encountered
s - Give size of file in kilobytes
t - Sort by time modified, latest first

64

64

32

7/30/2012

Change Directory - cd
Change current working directory
q Syntax: cd [directory]
q

u
u

Without argument, changes to your login directory


Otherwise changes to directory

65

65

Absolute Pathnames
Every file has a pathname
q A pathname is built by tracing a path from the root
directory, through all intermediate directories, to
the file
q String all the filenames in the path together,
separating them with slashes ( / ) and proceeding
them with the root directory ( / )
q Example: /usr/src/cmd/date.c
q

66

66

33

7/30/2012

Path Names

67

67

Relative Pathnames
q

Every directory has two intrinsic directories


u
u

. (this directory)
.. (the parent directory)

These make using relative pathnames easy

68

68

34

7/30/2012

inode
q

inodes (or index nodes) contain information about


files
owner krf
group 300
type regular file
permissions rwxr-xr-x
last accessed Aug 23 1999 1:45 PM
last modified Jul 4 1999 9:17 AM
size 6030 bytes
number of links 2
disk addresses

inodes do not contain path or file name


information
69

69

LiNk - ln
Create a pseudonym for an existing file
q Syntax: ln [-fs] filename [linkname]
q

u
u

f - force a hard link to a directory, only available to


supersuser
s - create a symbolic (soft) link

Hard links are default, they create a pointer to the


file
q Symbolic, or soft links, create an indirect pointer
to the file via the pathname
q

Although only the superuser can create a hard link to a


directory, any user can create a soft link
u Soft links also work across file systems
u

70

70

35

7/30/2012

Why Hard and Soft Links?


q

Hard links all have equal status


u

When a file with hard links is deleted, the actual file


remains until all of the hard links have been deleted

Soft links can have the original file deleted while


soft links still remain
q Soft links can also be confusing when used to
change directories
q

Suppose you have a soft link called My_Dir that points


to your home directory, /home/krf and you cd My_Dir
u Then, if you execute pwd, it shows /home/krf!
u pwd shows the name of the linked-to directory rather
than the name of the link
u

71

71

Why Not Just cp?


q

ln creates a pseudonym for the given file


No new inode is created
u Only one copy of the actual file exists
u Modifications via either filename affect the file
u

cp creates a new copy of the given file


A new inode is created
u Twice as much disk space is used
u Modifications to the copy are not reflected in the
original
u

72

72

36

7/30/2012

touch
Update the access and modification times of a file
q Syntax: touch [-c] [-f] filename ...
q

u
u

-c - Do not create filename if it doesnt exist, default is


to create filename
-f - Attempt to force the update in spite of read/write
permissions associated with filename

Why would you want to do this?


u

This is particularly useful in software development

73

73

conCATenate file(s) - cat


Concatenate files to standard output
q Syntax: cat [ -benstuv ] [filename...]
q

b - number all lens except blanks


u e - display non-printing characters and $ at end-of-line
u n - number all lines
u s - substitute a single blank line for multiple adjacent
blank lines
u t - display non-printing and tab characters
u u - unbuffered
u v - display not printing characters
l ^X for Control-X
l M-X for non-ASCII characters with high bit set
u

74

74

37

7/30/2012

more
Page through a text file
q Syntax: more [-cdflsu] [-lines] [+linenumber]
[+/pattern] [filename...]
q

c - clear screen before displaying


u d - display error message rather than ringing bell if an
illegal command is used
u f - do not fold long lines
u l - do not treat formfeed characters as page breaks
u s - squeeze multiple adjacent blank lines into one
u u - suppress underlining escape sequence
u

75

75

head
Display the first few lines of a specified file
q Syntax: head [-n] [filename...]
q

-n - number of lines to display, default is 10


u filename... - list of filenames to display
u

When more than one filename is specified, the


start of each files listing displays
==>filename<==

76

76

38

7/30/2012

tail
Displays the last part of a file
q Syntax: tail +|-number [lbc] [f] [filename]
or: tail +|-number [l] [rf] [filename]
q

+number - begins copying at distance number from


beginning of file, if number isny given, defaults to 10
u -number - begins from end of file
u l - number is in units of lines
u b - units are blocks
u c - units are characters
u r - print in reverse order
u f - if input is not a pipe, do not terminate after end of file
has been copied but loop. This is useful to monitor a
file being written by another process
77
77
u

Word Count - wc
Display a count of lines, words, and characters
q Syntax: wc [ -lwc ] [ filename ... ]
q

l - count lines
u w - count words
u c - count characters
u

Example:
clyde%wc testfile
7

43 168

testfile

clyde%
78

78

39

7/30/2012

Metacharacters
q

q
q
q
q

Characters with special meaning to the shell


*, ?, [...],
* matches any grouping of zero or more
characters
? matches any single character
[...] allows matching a range of characters
u
u
u
u

[0-9] matches any digit


[A-Z] matches any capital letter
[a-d] matches a, b, c, or d
[aeiou] matches any vowel

79

79

history
Display the history list in the C or Korn shells
q Syntax: history [ -hr ] [ n ]
q

u
u

h - display history list without leading numbers


l used to produce files for input as a script
r - display the history list in reverse order with most
recent command first
n - number of previous commands to display
l if n is omitted, the entire history list is displayed
where the entire list length is determined by the
value of the shell variable history
Example:
set history = 40
80

80

40

7/30/2012

PaGe - pg
q

q
q
q
q

Page through a file


Unlike more, pg allows you to back up in the file
Syntax:/usr/5bin/pg
After each screen is displayed, it pauses, displays
a : prompt, and awaits a command
Perusal commands
u
u
u

Return or Enter - display next screen


(+|-)number - simulate scrolling forward or backwards
number of screens
. or CTRL-L - redisplay current screen

81

81

Searching with pg
q
q
q
q

[i]/pattern/ - search forward for the ith occurrence


(default i=1) of pattern
[i]^pattern^ or [i]?pattern? - search backwards for
the ith occurrence (default i = 1) of pattern
After searching, pg will normally display the line
containing pattern at the top of the screen
h displays summary of commands
q or Q exits

82

82

41

7/30/2012

CoPy file(s) -cp


q

Syntax: cp [ -ip ] filename1 filename2


cp -rR [ -op ] directory1 directory2
cp [ -iprR ] filename ... directory
i - interactive, prompt for confirmation whenever the
copy would overwrite an existing file
u p - preserve, duplicate not only the file contents but
also modification time and permission modes
u r or R - recursive, if any of the source files are
directories, copy the directory along with its files,
including any subdirectories and their files
u

83

83

Forms of cp
q

cp [ -ip ] filename1 filename2


u

cp -rR [ -op ] directory1 directory2


u

copies filename1 onto filename2

recursively copies directory1, along with its contents


and subdirectories, into directory2

cp [ -iprR ] filename ... directory


u

copies filename(s) into directory

cp refuses to copy a file on top of itself


q Beware of a recursive cp like this
q

clyde%cp -r /home/krf/src /home/krf/src/backup


u Why?
84

84

42

7/30/2012

MoVe file - mv
Move or rename files
q Syntax: mv [ - ] [ -fi ] filename1 filename2
mv [ - ] [ -fi ] directory1 directory2
mv [ - ] [ -fi ] filename ... directory
q

- - interpret all following arguments as filenames. This


allows filenames that begin with a minus sign
u f - force, overriding mode restrictions and the -i option.
Also suppress warning messages about modes that
would restrict overwriting
u i - interactive, displays the name of the file or directory
with a ? if the move would overwrite an existing file or
directory
u

85

85

cp Versus mv
mv simply changes the filename or absolute
pathname of the affected files, the files inodes
are not changed
q cp creates new files so new inodes are created,
unless the copy is overwriting an existing file
q

86

86

43

7/30/2012

Line Print - lp, cancel


lp arranges for the named files and associated
information (called a request) to be printed on a
line printer
q cancel lp requests
q Syntax: lp [-ddest] [-nnumber] [-ooption] [-ttitle]
filename
cancel [ids]
q lp displays an id that identifies the print request
q

cancel uses this id to cancel a specific print request

87

87

lpr
lpr sends a job to the printer via the print spooler
q Syntax: lpr [-Pprinter] [-#copies] [-Ttitle] [-h]
filename
q

-P - desired printer as listed in /etc/printcap


u -# - number of copies
u -T - title
u -h - suppress burst page
u filename - file to print
u

lpr has many more options and capabilities


u

See the man page for more info

88

88

44

7/30/2012

Line Printer Queue - lpq


Displays the queue of printer jobs
q Syntax: lpq [-Pprinter] [-l] [+[interval] [username...]
q

u
u
u

-P
printer queue to display, default is
defined by environmental variable
PRINTER, or system default printer if
PRINTER isnt set
-l
display queue information in long format
+ interval displays the queue every interval
seconds until queue is empty
username displays jobs for username(s)

Example of lpq output


Rank Owner
Job Files
active krf
143 test.txt
89

89

Line Print ReMove - lprm


Remove files from the specified print queue
q Syntax: lprm [-Pprinter] [-] [job# ]
q

-P specifies which printer queue


u remove all jobs owned by you
u job# remove specified job numbers, or ids
u

Example
lprm -Pps 144
removes job # 144 from ps queue
lrpm -Pversatec - removes all my pending jobs from
versatec queue
lprm 144
removes job # 144 from default
printer queue
90

90

45

7/30/2012

file
Attempts to determine the type of a file by
examining its contents
q Syntax: file [-ffile] [-L] filename
q

u
u
u

-ffile
get list of filenames to check from file
-L
if filename is a link, check the link
references rather than the link itself
filename... name of file(s) to check

file is good when you have no idea what a file is


u

listing an executable or binary file to see what is in it


can mess up your terminal display, and using an editor
is more work

91

91

CHange file MODe - chmod


Changes access permissions (mode) of a file
q Syntax: chmod [-R] mode filename
q

u
u
u

-R
recursively descend through directory
arguments, changing the mode for each
mode
new value for file access permissions
mode, either octal or symbolic
filename filename(s) or directory(s) name to
modify

92

file

92

46

7/30/2012

Access Permissions
Each file and directory has a set of access
permissions associated with it
q You can see them by using the long form of ls
(ls -l)
q Format is srwxrwxrwx
q

u
u
u
u

s is special, l for links, d for directories, t for sticky bit


r is read permission
w is write permission
x is execute permission

Three access levels exist, in order


u

User, Group, Other


93

93

Access Permissions
Only the owner (or superuser) can change the
access permissions of a file
q The execute bit must be set in order to cd into a
directory
q The shell variable umask sets the default access
permissions when you create a new file
q

94

94

47

7/30/2012

Octal Access Permissions


q

Octal notation consists of the numbers 0-7


u
u

In bit positions, the values of the bits are 4 2 1


To determine the octal value, multiply the bit position
value by its contents and add them up
l 101 = 1*4 + 0*2 + 1*1 = 5
l 110 = 1*4 + 1*2 + 0*1 = 6
To use octal access permissions, you do the same
thing for each level of permissions
l -rwxrw-r-=(0*0)
(1*4+1*2+1*1) (1*4+1*2+0*1) (1*4+0*2+0*1) =0764
l Leading zeros can be ignored

95

95

Symbolic Access Permissions


Symbolic access permissions have the form
[who] op permission [ op permission]
q Where who is
q

u
u
u
u

u
g
o
a

user
group
other
all

If who is omitted, all is assumed


q op is +, -, or = for add, remove, or explicitly set
q permission is r (read), w (write), or x (execute)
q

96

96

48

7/30/2012

Symbolic Permissions Examples


q
q
q

chmod o-w file


remove write permission
from other
chmod g+x file
add execute permission to
group
chmod u=wr file
explicitly make permissions
for user read and write only
(all other
bits for group are
reset when = is used)
chmod u=rwx g+rw o-w file
explicitly gives user all
permissions,
adds read and
write to group and
removes
write from other

97

97

Background Processes Using &


The simplest way to create a background
process is to invoke the command with a trailing &
q Example:
q

% sort addresses > mail_list &


[1] 21885
%
u
u
u

This will sort the file addresses in the background and


place the results in the file mail_list
21885 is the unique pid of this instance of the sort
process
[1] is the job number that is assigned by the current
shell
98
98

49

7/30/2012

wait
What if you initiate several background processes
and then you decide that you need to wait until
they have all finished before doing something?
q wait will make the shell wait on all the background
processes to complete before giving you another
prompt
q

If wait immediately returns a prompt, there are no


processes executing in the background

99

99

kill - signal a process


kill is somewhat strangely named
q Syntax: kill [-sig_no] pid
kill -l
(display list of signals)
q

u
u

-sig_no - signal number to send


pid - process id of process to receive signal

Default sig_no is 15, or request-processtermination


q kill -9 pid terminates the process with extreme
prejudice
q As usual, you can only kill your own processes
unless you are the superuser
q

100

100

50

You might also like