unix introduction
DESCRIPTION
Unix IntroTRANSCRIPT
7/30/2012
1
1
UNIX
Dr. Emmanuel Shubhakar Pilli
Graphic Era University
2
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
q 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
u “Three people could overload it.”
7/30/2012
2
3
The Roots of Unix
q Ken Thompson had been using Multics to run
Space Travel but it cost $70 a run
q Thompson found a cast-off PDP-7 with the intent
to write a file system
q This file system became the roots of Unix
q Unix was originally called UNICS, a pun on
Multics (Uniplexed Information and Computing
Service)
q Unix struggled along on an obsolete, under-
powered machine until the PDP-11 was released
4
Lessons Learned From Multics
q Tree-structured file system
q Separate, identifiable program to do command
interpretation (the shell)
q Structure files with no structure except as a
sequence of bytes, in most cases with no
interpretation by the OS
q Text files as simple sequences of characters
separated by new-lines
q 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
7/30/2012
3
5
The Birth of Unix
q 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--we‟d
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
6
Thompson, Ritchie with PDP-11
7/30/2012
4
7
Unix Philosophy
q 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
u Write programs that handle text streams, because that
is a universal interface
q Don‟t hesitate to build new programs to do a job
8
The Unix Explosion
q February 1973, the Third Edition of the UNIX
PROGRAMMER‟S 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 ACM‟s Turing Award for
their work
7/30/2012
5
9
The Law
q January, 1949 DOJ filed an antitrust complaint
against Western Electric and AT&T
q In January, 1956, a “consent decree” was entered
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],”
u “from engaging ... in any business not of a character or
type engaged in by Western Electric or its subsidiaries”
u AT&T was enjoined “from engaging in any business
other than the furnishing of common carrier
communications services”
10
Results of Consent Decree
q 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
u “No business but phones and telegrams”
q Thompson‟s presentation at the ACM SOSP put
them in a quandary
u Suddenly requests started flowing in for Unix licenses
u Computers weren‟t “phones and telegrams” thus putting
them in danger of antagonizing the Justice Department
7/30/2012
6
11
AT&T Unix Policy
q 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:
no advertising
no support
no bug fixes
payment in advance
12
The Growth of Unix
q May 1974 - “nearly a dozen”
q Spring „75 - “three dozen”
q June „75 - “plus 20 new installations”
q October „75 - 60 installations
q March „76 - 80 installations
q September „76 - 138 installations• Canada 13
• Great Britain 10
• Australia 4
• Israel 3
• The Netherlands 3
• Austria 1
• Belgium 1
• Germany 1
• Venezuela 1
• United States 101
7/30/2012
7
13
Berkeley Unix
q 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:
u Unix Pascal System
u Ex Text Editor
14
BSD Unix and the Internet
q 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 DARPA‟s request, along with the
Berkeley Fast File System
q Unix emerged from this as the machine that
fueled the original internet (ARPANET)
7/30/2012
8
15
Dueling Unixes
q Up until „78, Unix meant AT&T‟s operating system
q After this, there were two principle flavors of Unix
u BSD
u System V
q 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
16
Why Unix Instead of Other OSs?
q Portability
u Written in C, only a small portion is in assembly
u Runs on micros, minis, main frames, and super
computers
q Ran on (relatively) inexpensive hardware
q Designed to be adaptable to changing user
environments
q It was inexpensive
q Source was available
q Permits access to its operating features
u Wonderful programmer‟s environment
7/30/2012
9
17
Challenges of Unix
q Powerful system with many tools and capabilities
u Takes considerable effort and time to master
q Most commands are short, terse, and non-
descriptive
q Relatively unforgiving of user mistakes
18
7/30/2012
10
19
20
Major Personalities
q Ken Thompson
q Dennis Ritchie
q Joe Ossanna
q Rudd Canaday
q Robert Fabrey
q Steve Johnson
q Stephen Bourne
q Doug McIlroy
q Kirk McKusick
q Armando Stettner
q Heinz Lycklama
q Bill Joy
q Co-Inventor of Unix
q Co-Inventor of Unix
q Author of troff
q Original file system collaborator
q Obtained DARPA funding for BSD
q Author of lint and yacc
q Author of the Bourne shell, sh
q Originator of pipe concept
q Wrote Berkeley Fast File System
q Got DEC to acknowledge Unix
q Chaired first Unix standards group
q Author of csh, ex, vi, termcap, and
much of BSD
7/30/2012
11
21
Ken Thompson and Dennis Ritchie
22
What‟s An Operating System?
q 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
u the hardware
u the kernel, the hardware interface unique to a platform
u the shell and other system utilities
u the application programs
7/30/2012
12
23
The System View of Unix
24
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
q 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
7/30/2012
13
25
Unix Shells
q A shell is the user interface or set of programs
user to interact with Unix and process commands
q Common shells are:
u Bourne
u C
u Korn
q When a shell is executed, it
u inherits environmental variables set by the previous
shell
u stops the previous shell
u becomes the current shell
26
Bourne Shell (sh)
q 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
u Approximately 20 times faster than C shell because it is
simpler and doesn‟t carry as much baggage
7/30/2012
14
27
C Shell (csh)
q 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
u alias
u history
u job control
u filename completion
28
Korn Shell (ksh)
q 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
u report formatting capabilities
u built-in arithmetic
u data types
7/30/2012
15
29
Superuser
q A user with essentially all privileges
u Often referred to as root privileges
q 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
30
Password Security
q Unfortunately, password security is a necessity
q Good passwords have several characteristics
u 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
q It is also good practice to avoid:
u Family names
u Birthdates
u Pet‟s names
u Any other personal data
7/30/2012
16
31
Logging In and Out
q login: username
q password: your_password
u 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%
q To logout
u clyde% exit
u or clyde% logout
32
To connect via internet
q telnet your.favorite.computer
q or telnet 127.0.0.0
q Your favorite computer may respond with
something like:
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%
7/30/2012
17
33
What happens when you login?
q Unix runs the login program
u If it exists, .login script is executed
q Then the shell specified in /etc/passwd is
executed and user is placed in his HOME
directory
u If it exists, .cshrc (if csh is your default shell) is
executed
q So what‟s the difference? Why two initialization
files?
u .login is executed only at login
u .cshrc is executed every time a new shell is spawned
34
exit or logout
q exit terminates your current shell
u if it is also your login shell, exit will exit and logout
q logout terminates a login shell
7/30/2012
18
35
Process Concepts
q Early or simple (DOS) computer systems allowed
only one program to be executed at a time
q Modern computer systems like Unix are multi-
tasking, allowing multiple programs to be loaded
and executed concurrently
q This requires more control of programs, leading to
the notion of a process
q A process is a program in some stage of
execution
q A modern computer system is a collection of
processes
u Operating system processes and User processes
36
Unix Processes
q Every command executed on Unix is a process
q Unix processes are hierarchical
u Parent and child processes
q Every process is automagically assigned three
standard files
u input
u output
u error
7/30/2012
19
37
Process Blocks
q Each process has a process block that includes
u a process identifier
u the process state
u the value of the process‟s program counter
u other information specific to the process such as
l memory limits
l files in use
l processor time used
l ....
38
Command Components
q Commands consist of:
u Command name
u Options, or flags
u Arguments
q Arguments are the “things” that the command will
operate on
q Options modify the behavior of the command
7/30/2012
20
39
Command Notation
q command name
q [ ] optional arguments or options
u can be nested
l [ arg1 [ arg2 ] ]
l [...]
q | is an OR condition
u arg1 | arg2
40
who
q Who is on the system and info about their login
u User_name, terminal_name, login_time
q Syntax: who [am i]
q Example:
clyde% who
root console Jul 22 09:36
krf ttyp0 Aug 7 10:32 (dal-tx2-33.ix.ne)
q am i option lists requestor info only
q Related command: whoami
u Lists requestor‟s user_name only
7/30/2012
21
41
CALendar - cal
q Prints a calendar for the specified year
u Default is current year
q Syntax: cal [ [ month ] year ]
u month - number from 1 to 12
u year - number from 1 to 9999
q Note: September 1752 is odd, 11 days were
skipped to make up for lack of prior leap year
adjustments
q Examples:
clyde% cal
clyde% cal 9 1752
42
date
q Display or set the date
q Syntax:date [-u] [-a [-] sss.fff] [yymmddhhmm [.ss]]
u 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
q If the argument begins with a +, the output of date
is under user control
q Examples:
clyde% date
clyde% date -u
clyde% date +%T
7/30/2012
22
43
finger
q Displays information about users
q Syntax: finger [options] user_name
q By default, finger displays user_name‟s:
u login name
u full name
u terminal name
u idle time
u login time
u location
u first line of .plan file
44
q 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:
u Read mail: mail
u Send mail: mail -s “subject” recipient
u or: mail -s “subject” recipient < file_name
7/30/2012
23
45
MANual pages - man
q Read online documentation or find reference
pages by keyword
q Syntax: man [[section] title]
or: man [[-k keyword] | [-f filename]]
q Examples:
clyde% man ps
clyde% man -k compile
clyde% man -f /var/spool/mail
clyde% man link
clyde% man 2 link
clyde% man 8 link
46
whois
q Internet „white pages‟
u Searches for a TCP/IP directory entry
u Used to find people or domain owners/contacts
q Syntax: who [-h host] identifier
u 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
q Examples:
clyde% whois -h rs.internic.net Earthlink.com
clyde% whois -h rs.internic.net „Krol, Ed*‟
7/30/2012
24
47
show ProcesSes - ps
q 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:
u a - include processes that are not owned by you
u x - show processes that don‟t have a controlling
terminal
u r - show only running processes
q Example:
clyde% ps -a
48
talk Example
clyde% talk krf
Message from Talk_Daemon@clyde at 17:39 ...
talk: connection requested by [email protected].
talk: respond with: talk [email protected]
q Respond as shown and a split screen will be
displayed with your input in one half and your talk-
mate‟s output in the other
q To end the session, type Control-c
7/30/2012
25
49
MESsaGe - mesg
q Permit or deny messages on your terminal
q Syntax: mesg [n] [y]
u default reports current state without changing it
u n - forbids messages to be sent to you from talk or write
u y - reinstates permission
50
Print Working Directory - pwd
q Displays the pathname of the current working
directory
q Syntax: pwd
q Example:
clyde% pwd
/home/clyde/krf
clyde%
7/30/2012
26
51
Set TTY - stty
q Display and set terminal options
q Syntax: stty [-ag] [option] ...
u a - Report all option settings
u g - Report current settings in a format that can be used
as an argument to another stty command\
q Most common user use is to remap control keys,
such as erase
u To set the erase command to be a backspace:
clyde% stty erase ^h
u Key mapping good candidate for inclusion in your .login
file
52
mail(pine)
PINE 4.10 MAIN MENU Folder : INBOX No Messages
? HELP - Get help using Pine
C COMPOSE MESSAGE - Compose and send a message
I MESSAGE CHECK - View messages in current folder
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 R RelNotes
O OTHER CMDS > [ListFldrs] N NextCmd K KBlock
7/30/2012
27
5353
File System
q Unix provides a hierarchical, or tree-like, file
system
u Supports two main objects
l Files
l Directory files, or directories
u From Unix‟s view, there is no difference between the
two
q From the user‟s view, files are data that you want,
directories are containers of files
q In reality, files are simply a stream of bytes
u Critical design philosophy of Unix
5454
Tree Structure
7/30/2012
28
5555
Files and Directories
5656
File Names
q 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
u Sticking with the following will save many problems
l Uppercase letters (A-Z)
l Lowercase letters (a-z)
l Numbers (0-9)
u Period ( . ) can be used as any other character and
may denote special files
u Exception is the root directory, which is always named /
l No other file can use this name
lComma ( , )
lDash ( - )
lUnderscore ( _ )
7/30/2012
29
5757
Other File Name Rules
q Other characters may be used by “escaping” them
with a “\” character
u Willie\*Weasil - displays as Willie*Weasil
u Much confusion can result from this
q File names are case sensitive
u my_file, My_File, and MY_FILE are all unique names
5858
File Access Controls
q Unix provides file security with access levels and
permissions
q Access levels define who specific permissions
belong to
u user (u)
u group (g)
u other (o)
q Permissions define what the members of an
access level are allowed to do
u execute (x)
u read (r)
u write (w)
7/30/2012
30
5959
Access Controls Provide Security
q Generally accepted that there are files users
cannot access or modify
q This provides security for:
u System
u Work groups
u Individual users
6060
Unix Has Many Filesystem Types
Use AIX DigitalUnix
HPUnix
SCOUnix
SunOS
Solaris IRIX Linux
Native ifs ufs hfs EAFS 4.2 ufs efs ext2
NFS nfs nfs nfs nfs NFS nfs nfs nfs
CD cdrfs cdfs cdfs HS hsfs hsfs iso9660
iso9660
Swap ufs swap
DOS DOS pcfs dos msdos
/proc procfs proc proc proc
other advfs vxfs S51K s5 hfs sysv
lofs minux
7/30/2012
31
6161
MaKe DIRectory - mkdir
q Creates new directories
q Syntax: mkdir [ -p ] dirname
u p - allows “missing” parent directories to be created as
needed
q Requires write permission in the parent directory
6262
ReMove file(s) - rm
q Removes (deletes) files
q Syntax: rm [ - ] [ -fir] filename ...
u - Treat following arguments a filenames, so you can
remove files starting with a minus sign
u f - Force files to be removed without displaying
permissions, asking questions, or reporting errors
u i - Interactive, ask before removing each file
u r - Recursively delete the contents of a directory, its
subdirectories, and the directory itself
7/30/2012
32
6363
ReMove DIRectory - rmdir
q Removes empty directories
q Syntax: rmdir directory...
q An error will be reported if the directory is not
empty
6464
LiSt directory - ls
q List the contents of a directory
q Syntax: ls [-aAcCdfFgilLqrRstu1] filename
u a - List all entries, without this, files beginning with a .
are not listed
u c - Sort on time of last edit
u l - List in “long” form, giving mode, number of links,
owner, size, and time of last modification
u R -Recursively list subdirectories encountered
u s - Give size of file in kilobytes
u t - Sort by time modified, latest first
7/30/2012
33
6565
Change Directory - cd
q Change current working directory
q Syntax: cd [directory]
u Without argument, changes to your login directory
u Otherwise changes to directory
6666
Absolute Pathnames
q 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
7/30/2012
34
6767
Path Names
6868
Relative Pathnames
q Every directory has two intrinsic directories
u . (this directory)
u .. (the parent directory)
q These make using relative pathnames easy
7/30/2012
35
6969
inode
q inodes (or index nodes) contain information about
files
q inodes do not contain path or file name
information
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
7070
LiNk - ln
q Create a pseudonym for an existing file
q Syntax: ln [-fs] filename [linkname]
u f - force a hard link to a directory, only available to
supersuser
u s - create a symbolic (soft) link
q 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
u 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
7/30/2012
36
7171
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
q 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
u 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
7272
Why Not Just cp?
q ln creates a pseudonym for the given file
u No new inode is created
u Only one copy of the actual file exists
u Modifications via either filename affect the file
q cp creates a new copy of the given file
u A new inode is created
u Twice as much disk space is used
u Modifications to the copy are not reflected in the
original
7/30/2012
37
7373
touch
q Update the access and modification times of a file
q Syntax: touch [-c] [-f] filename ...
u -c - Do not create filename if it doesn‟t exist, default is
to create filename
u -f - Attempt to force the update in spite of read/write
permissions associated with filename
q Why would you want to do this?
u This is particularly useful in software development
7474
conCATenate file(s) - cat
q Concatenate files to standard output
q Syntax: cat [ -benstuv ] [filename...]
u 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
7/30/2012
38
7575
more
q Page through a text file
q Syntax: more [-cdflsu] [-lines] [+linenumber]
[+/pattern] [filename...]
u 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
7676
head
q Display the first few lines of a specified file
q Syntax: head [-n] [filename...]
u -n - number of lines to display, default is 10
u filename... - list of filenames to display
q When more than one filename is specified, the
start of each files listing displays
==>filename<==
7/30/2012
39
7777
tail
q Displays the last part of a file
q Syntax: tail +|-number [lbc] [f] [filename]
or: tail +|-number [l] [rf] [filename]
u +number - begins copying at distance number from
beginning of file, if number isn‟y 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
7878
Word Count - wc
q Display a count of lines, words, and characters
q Syntax: wc [ -lwc ] [ filename ... ]
u l - count lines
u w - count words
u c - count characters
q Example:
clyde%wc testfile
7 43 168 testfile
clyde%
7/30/2012
40
7979
Metacharacters
q Characters with special meaning to the shell
q *, ?, [...],
q * matches any grouping of zero or more
characters
q ? matches any single character
q [...] allows matching a range of characters
u [0-9] matches any digit
u [A-Z] matches any capital letter
u [a-d] matches a, b, c, or d
u [aeiou] matches any vowel
8080
history
q Display the history list in the C or Korn shells
q Syntax: history [ -hr ] [ n ]
u h - display history list without leading numbers
l used to produce files for input as a script
u r - display the history list in reverse order with most
recent command first
u 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
7/30/2012
41
8181
PaGe - pg
q Page through a file
q Unlike more, pg allows you to back up in the file
q Syntax:/usr/5bin/pg
q After each screen is displayed, it pauses, displays
a : prompt, and awaits a command
q Perusal commands
u Return or Enter - display next screen
u (+|-)number - simulate scrolling forward or backwards
number of screens
u . or CTRL-L - redisplay current screen
8282
Searching with pg
q [i]/pattern/ - search forward for the ith occurrence
(default i=1) of pattern
q [i]^pattern^ or [i]?pattern? - search backwards for
the ith occurrence (default i = 1) of pattern
q After searching, pg will normally display the line
containing pattern at the top of the screen
q h displays summary of commands
q q or Q exits
7/30/2012
42
8383
CoPy file(s) -cp
q Syntax: cp [ -ip ] filename1 filename2
cp -rR [ -op ] directory1 directory2
cp [ -iprR ] filename ... directory
u 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
8484
Forms of cp
q cp [ -ip ] filename1 filename2
u copies filename1 onto filename2
q cp -rR [ -op ] directory1 directory2
u recursively copies directory1, along with its contents
and subdirectories, into directory2
q cp [ -iprR ] filename ... directory
u copies filename(s) into directory
q cp refuses to copy a file on top of itself
q Beware of a recursive cp like this
clyde%cp -r /home/krf/src /home/krf/src/backup
u Why?
7/30/2012
43
8585
MoVe file - mv
q Move or rename files
q Syntax: mv [ - ] [ -fi ] filename1 filename2
mv [ - ] [ -fi ] directory1 directory2
mv [ - ] [ -fi ] filename ... directory
u - - 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
8686
cp Versus mv
q mv simply changes the filename or absolute
pathname of the affected files, the file‟s inodes
are not changed
q cp creates new files so new inodes are created,
unless the copy is overwriting an existing file
7/30/2012
44
8787
Line Print - lp, cancel
q 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
u cancel uses this id to cancel a specific print request
8888
lpr
q lpr sends a job to the printer via the print spooler
q Syntax: lpr [-Pprinter] [-#copies] [-Ttitle] [-h]
filename
u -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
q lpr has many more options and capabilities
u See the man page for more info
7/30/2012
45
8989
Line Printer Queue - lpq
q Displays the queue of printer jobs
q Syntax: lpq [-Pprinter] [-l] [+[interval] [username...]
u -P printer queue to display, default is
defined by environmental variable
PRINTER, or system default printer if
PRINTER isn‟t set
u -l display queue information in long format
u + interval displays the queue every interval
seconds until queue is empty
u username… displays jobs for username(s)
q Example of lpq output
Rank Owner Job Files
active krf 143 test.txt
9090
Line Print ReMove - lprm
q Remove files from the specified print queue
q Syntax: lprm [-Pprinter] [-] [job# …]
u -P specifies which printer queue
u - remove all jobs owned by you
u job#… remove specified job numbers, or ids
q 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
7/30/2012
46
9191
file
q Attempts to determine the type of a file by
examining it‟s contents
q Syntax: file [-ffile] [-L] filename…
u -ffile get list of filenames to check from file
u -L if filename is a link, check the link
references rather than the link itself
u filename... name of file(s) to check
q 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
9292
CHange file MODe - chmod
q Changes access permissions (mode) of a file
q Syntax: chmod [-R] mode filename…
u -R recursively descend through directory
arguments, changing the mode for each file
u mode new value for file access permissions
mode, either octal or symbolic
u filename… filename(s) or directory(s) name to
modify
7/30/2012
47
9393
Access Permissions
q 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
u s is special, l for links, d for directories, t for sticky bit
u r is read permission
u w is write permission
u x is execute permission
q Three access levels exist, in order
u User, Group, Other
9494
Access Permissions
q 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
7/30/2012
48
9595
Octal Access Permissions
q Octal notation consists of the numbers 0-7
u In bit positions, the values of the bits are 4 2 1
u 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
u 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
9696
Symbolic Access Permissions
q Symbolic access permissions have the form
[who] op permission [ op permission] …
q Where who is
u u user
u g group
u o other
u a all
q 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)
7/30/2012
49
9797
Symbolic Permissions Examples
q chmod o-w file remove write permission
from other
q chmod g+x file add execute permission to
group
q chmod u=wr file explicitly make permissions
for user read and write only (all other
bits for group are reset when = is used)
q chmod u=rwx g+rw o-w file
explicitly gives user all permissions,
adds read and write to group and
removes write from other
9898
Background Processes Using &
q The simplest way to create a background
process is to invoke the command with a trailing &
q Example:
% sort addresses > mail_list &
[1] 21885
%
u This will sort the file addresses in the background and
place the results in the file mail_list
u 21885 is the unique pid of this instance of the sort
process
u [1] is the job number that is assigned by the current
shell
7/30/2012
50
9999
wait
q 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
u If wait immediately returns a prompt, there are no
processes executing in the background
100100
kill - signal a process
q kill is somewhat strangely named
q Syntax: kill [-sig_no] pid
kill -l (display list of signals)
u -sig_no - signal number to send
u pid - process id of process to receive signal
q Default sig_no is 15, or request-process-
termination
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