Seminar on UNIX
Enquiry : Mr.Jason Chan (mwchan@phy.cuhk.edu.hk,
Rm348)
Topics
Introduction to UNIX
Objectives
- Describe the UNIX operating system
- Log into the system and execute basic UNIX commands
- Use the correct syntax for entering shell commands
The UNIX Operating System
- A multi-user multi-process time-sharing operating system
- Wide spectrum of computers
- Offer a common execution environment across the computers
UNIX Original Philosophy
- Portability;
- Unification of file, device and interprocess communication;
- Ability of creating asynchronous multiple processes;
- Hierarchial file system;
- Algoriths were selected for simplicity, not for speed or sophistication;
- Replaceable and programmable shell command interpreter;
- Ability to build complex programs from simpler programs;
- Power of UNIX comes from the relations between the programs rather
than the programs themselves.
Reasons for its Success
- Open system
- Portability
- Functionality
- Government acceptance
- Interoperability
- Scalability
- Standards
Obstacles
- UNIX variants
- Lack of business software
- Development complexity
- Security issues
A Brief History
- UNIX was developed at Bell Labs. in the late 1960’s
- Version 7 was distributed in 1978
- Major development streams
- AT&T : System V
- U.C. Berkeley : BSD 4.x
- Sun Microsystems Inc. SunOS, Solaris
- Microsoft : Xenix
Unix Structure
- Low Level : hardware (physical)
- kernel
- - File system
- - Process controller
- Shell
- High Level : user (in a shell) or user application
Kernel
- Shell commands and application programs initiate system calls to the
kernel
- Written in C, some assembler
- Schedules multiple users
- Interfaces to I/O devices
- Manages the files on disk
What is a Shell ?
The Shell - User Interface to UNIX
- e.g. cal, cat, cp, mv
- Command Interpreter (Interpret input for OS and handle any output from
the OS)
- Programming Language(Combine sequences of commands to create new programs
called shell scripts)
- The most popular shells are Bourne Shell (bsh), C Shell (csh) and Korn
Shell (ksh)
Logging in and out
- Login
- Logout
- < ctrl > d or exit or logout
- $ is the UNIX command prompt
- Command prompt for root is #
Command Format
Command Line
$ command option(s) argument(s)
$ ls -l /u
The - always indicates the next character is an option, or flag, for
the command
There are exceptions to this command format
Command Format (cont.)
Separation
right wrong
$ ls -l /usr $ ls -l/usr
$ ls -s /bin $ ls-s /bin
Order
right wrong
$ cat -s file $ cat file -s
$ ls -a $ -a ls
(There are exceptions !)
Command Format (cont.)
Multiple Options
right wrong
$ ls -l -d -t $ ls -l-d-t
$ ls -ldt $ ls -l d t
Multiple Arguments
right wrong
$ cat file1 file2 $ cat file1file2
$ cat file1 file2 $ cat file1, file2
The cal Command
cal month year
Examples
- Display a calendar for October 1994 at your work station
- Display a calendar for the year 1994
The cat Command
- concatenate or display files
- cat (options) filename
Examples
- Display the file file1 at the work station
- Concatenate several files
The date Command
Example : Display current date and time,
Super user can change the system date
The echo Command
- Write arguments to standard output
- New line is \n
Example : Write message to standard output
The lp Command
- Send print job to default system printer
Example : Print the file file1 on system printer
The lprm Command
- Remove jobs from the system printer spooling queue
Example : Remove job number 8 from the default printer queue
The lpstat Command
- Display line printer status information
Examples
- Display the status for all print queues
- Display the status of the default queue
The mail Command (Sending Mail)
- Standard utility on any Unix machine
- Send message to user nhychi on local system
- Send message to user nhychi on other system
- Send file to another user on local system
- Contents of your personal mailbox
The mail Command (Receiving Mail)
- The default message ‘You have New Mail’ will be shown if there is new
mail
Some useful commands at the ‘&’ prompt
- d - delete messages
- s - append messages to a file
- m - forward messages
- q - exit mail and leave messages in the queue
- t - top of list
- h - display header information again
- ? - display option in mail
The man Command
- Display Manual pages online
- One method to obtain command documentation
Examples
- Obtain information about the ls command
- Display one line synopsis of each manual section whose listing in the
table of contents contains stat
The passwd Command
- Changing password for ‘nhychi’
Example
> passwd [press Enter]
> nhychi’s Old password:
> nhychi’s New password:
> Enter the new password again:
Password will not be echoed on the screen
The wc Command
- Count the no. of lines, words and characters in a file
Options
Examples
- Display line, word, & character counts of a file
- Display only character and word counts
The whatis Command
- Describe what function a command performs
Example : whatis ls will produce the following output:
ls(1) -Display the contents of a directory
The who Command
- Identify the users currently logged in
Examples
- Display information about who is using the local system node
- Display your user name
Exercise 1
- Display the system’s date
- Count the number of lines in the /etc/motd file
- Display the whole calendar for the year 2000
- Display the whole calendar for the year 1752
- How many users are there in the system ?
- Display the contents of the /etc/motd file
Using Files
Objectives
- Describe different file types
- Describe the UNIX file system structure
- Use both full and relative pathnames
- Create, copy, display, move and delete files
File
- A collection of data located on some storage device(s)
- It is known to the system simply as a stream of characters
- Collections of files are stored in directories
File Type
Ordinary
- Text or code data
- No particular internal format
Directory
- A table of contents for a collection of related files
- Contains name and inode number (inode is where other information about
the file is)
Special Files
- Represent hardware or logical devices
- Found in directory called /dev
File Name in UNIX
- No embedded blanks
- Should not begin with ‘+’ or ‘-’ sign
- Case sensitive - MYFILE vs myfile
- .file hidden from normal ls command
- No shell metacharacters
Some Naming Conventions
|
filename.c
|
C Source code
|
|
filename.h
|
‘include’ file (header data) for C
|
|
filename.f
|
FORTRAN source code
|
|
filename.p
|
PASCAL source code
|
|
filename.s
|
Assembler source code
|
|
filename.o
|
Object code
|
|
.
|
Current directory
|
|
..
|
Parent directory
|
Pathnames
Change Directory
Examples
- Change to your home directory
- Change to other directory
- Go down one level of the directory tree
- Go up one level of the directory tree cd ..
Create Directory
- Command : mkdir directory
Example : Create the subdirectory data in /a1/cc/nhychi
mkdir /a1/cc/nhychi/data full path name
or
mkdir data relative path name
Print Working Directory
- Command : pwd
- The full path name of current working directory
Remove Directory
- Command : rmdir directory
Example : Empty and remove a directory
- rm mydir/* mydir/.*
- rmdir mydir
Copy Files
- Command : cp source target
- ‘source’ can be a file or a list of files
- If ‘source’ is a list of files, then ‘target’ must be a directory
- ‘target’ can be a file or a directory
- Copies will have the same filenames as the originals
- For each file in ‘source’ you will have two files
- You become the owner of the new file
Examples
- Make another copy of a file in the current directory
- Copy to another directory
- Copy a directory, its files and its subdirectories to another directory
This copies the directory test, its files subdirectories etc to the
directory prod
Display Files
- Commands : cat filename, head filename, more filename,
pg filename, tail filename
- If the file is longer than a screen, it will scroll until the bottom
of the file is reached ‘more’ displays one screen of information at a time
- If ‘pg’ fills the screen, it will wait, prompting you to press the
< Enter > key
List Files
Examples
- List all files in the current directory
- Display detail information
- List the files in order of modification time
Output Format
(1) (2) (3) (4) (5) (6) (7)
drwx------ 2 cc staff 104 Oct 20 09:00 bin
-rwxrwxr 1 cc staff 542 Oct 18 10:45 prog.sas
field 1 : file or directory, permission bits
field 2 : link count
field 3 : user name of file owner
field 4 : group name for group permissions
field 5 : character count
field 6 : date the file was last written to disk
field 7 : name of file or directory
Move/Rename Files
- Command : mv source target
- ‘source’ can be a file or a list of files
- If ‘source’ is a list of files, then ‘target’ must be a directory
- ‘target’ can be a file or a directory
- all attributes remain the same
- the only things that change are filename and/or location
Examples
- Rename the file chapter.1 to chap.1
- Move a file chap.1 to subdirectory book
- Use the mv command with pattern-matching
This moves all files in the directory book into the current directory
Remove Files
- Command : rm file1 file2 ...
Examples
- Delete a file
- Delete files one by one
Enter y to delete the file, or press Enter to keep it.
- Delete a directory tree
This recursively removes the contents of all subdirectories of book,
then removes book itself
File Permissions
Objectives
- Use octal and symbolic formats to change file permissions
- Use permissions to protect files and directories
- Determine if a file can be accessed by the owner, groups or others
Output Format
(1) (2) (3) (4) (5) (6) (7)
drwx------ 2 cc staff 104 Oct 20 09:00 bin
-rwxrwxr-- 1 cc staff 542 Oct 18 10:45 prog.sas
field 1 : file or directory, permission bits
field 2 : link count
field 3 : user name of file owner
field 4 : group name for group permissions
field 5 : character count
field 6 : date the file was last written to disk
field 7 : name of file or directory
File Protection/Permission
r = read; w = write; x = execute
rwx rwx rwx
user group others
Ordinary file
r ==> can look at contents of the file
w ==> can change or delete contents of the file
x ==> can use the filename as a command
Directory
r ==> can list (ls,li) files in the directory
w ==> can create/remove files in the directory (x required !)
x ==> can be ‘in’ the directory (e.g. cd to it)
For directory, x permission is required to access any of the files
and/or subdirectories within it
For directory, x permission is necessary for w permission to be effective
If you cannot get into the directory(x) then you cannot create or remove
files within it(w)
Change Mode - Permissions
- Command : chmod mode filename
- Specify changes relative to the existing permission on a file or directory
by adding or deleting
- permissions use ‘ls -l’ to list the current permissions
Symbolic Format
Examples
- Add write permission for group and other
- Give read, write execute to owner but denies group and other the permission
to access
Specify the final permissions, not relative changes, as with the Symbolic
Format
Octal Format - specify permissions as 3 digits
Example : Allow the owner read, write and execute, read and execute
to group and no permission to other
- What is rwxrr-xr-x in octal ?
- Change mode to rwxr--r-- in symbolic
- Repeat change in Octal
Function/Permissions Required
|
Command
|
Source Directory
|
Source File
|
Target Directory
|
|
cd
|
x
|
n/a
|
n/a
|
|
ls
|
r
|
n/a
|
n/a
|
|
mkdir
|
x,w
|
n/a
|
n/a
|
|
rmdir
|
x,w
|
n/a
|
n/a
|
|
cat,pg,more
|
x
|
r
|
n/a
|
|
mv
|
x,w
|
none
|
x,w
|
|
cp
|
x
|
r
|
x,w
|
|
touch
|
x(w)
|
n/a
|
none
|
|
rm
|
x,w
|
none
|
n/a
|
The ‘vi’ Editor
Objectives
- Identify different modes within vi
- Create, save and exit a file
- Add text to a file
- Delete text from a file
- Invoke the search and replace function
- Exit with or without saving a file
vi
- Full screen editor (visual editor)
- Widely use
- Create and modify text only
- No formatting capability
- Two modes of operation
- full screen command mode
- full screen text editing mode
- Search and replace capability for pattern matching
Cursor Movement
From command mode
|
h
|
left, one space
|
|
j
|
down, one line
|
|
k
|
up, one line
|
|
l
|
right, one space
|
|
^
|
beginning of line
|
|
$
|
end of line
|
|
1G
|
first line
|
|
G
|
last line
|
|
< ctrl > f
|
forward one screen
|
|
< ctrl > b
|
backward one screen
|
|
< ctrl> d
|
down half screen
|
|
< ctrl > u
|
up half screen
|
From command or text mode
<esc> get to command mode
Create a File
- Command : vi filename
- if the file to be edited exists, a copy of the file is put into a buffer
- if the file does not exist, an empty buffer is opened for this edit
session
- When vi is invoked the default mode is command mode
- Use cursor keys to get to the location of first revision
Insert and Delete
a - append or add text
- change the editor to text input mode
- ‘a’ will not be displayed
d$ - delete from cursor to end of line
dd - delete the entire line
ndd - delete n lines
dw - delete to the end of the word
i - insert text before cursor
o - open a new line after cursor
O - open a new line before cursor
p - put item last deleted before current line
P - put item last deleted after current line
rx - replace current character with x
x - delete 1 character at the cursor position
Save / Exit a File
< esc > - change the editor to command
mode
:q! - exit without saving
:w - write the editing buffer to the file
:wq - write and exit vi
ZZ - save & quit
String Search
/the/ - put the cursor on the first
occurrence of the string ‘the’
?the?- similar to /the/ but in opposite
direction
n - search next occurrence of the string
N - similar to ‘n’ but in opposite direction
Global Search and Replace
Example :Replace every ‘ if ‘ with ‘ if and only if ‘
- the : moves the cursor to the ex line editor command line
- first g indicates global search
- the s stands for substitute
- the // direct the editor to use the search string in the preceding
command and replaces it with the following string
- the last g stands for global
Undo
:u - allow command to
appear on screen
u - command will not
appear anywhere on
the screen
. - repeat last command
Environment Customization
- Editor defined variables only
- To see current settings from command mode, type
- To set toggle variables on or off
.exrc File
- Set the vi environment on each vi session
- Create a .exrc file in home directory
- .exrc file is read for every vi session
Crash Recovery
- After the system is restored, type
- List of files that have been saved
Processes
Objectives
- Define a UNIX process
- Monitor and stop processors
- Control process priorities
- Describe the primary system processors
What is a Process ?
- Process is a task
- Process is a program that is running
- Process is the execution of a program within its own environment
Process Characteristics
- Multi-Tasking
- many processes can execute at the same time
- many occurrences of the same program can be executed by different processes
at the same time
Self-Contained
- execute independently in their own environment
- communicate with other processes and with the rest of the world via
system calls
Monitor Processes
- The ps command writes process status to standard output
Examples
- List the processes that you have started : ps
- Give full information about the processes you are running : ps
-f
- Display all process information : ps -efl
- List processes owned by specific users :
Control Processes
- Background processes are invoked by putting an ‘&’ at the end of
the command line
Example : sas regression.sas&
- The ‘nohup’ command runs a command without hangups and quits
Example : nohup sas regression.sas&
- Use ‘nohup’ command to keep running a program in the background,
even through you log off the system
- The ‘nice’ command informs the system that a process is not
urgent and that a lower scheduling priority is appropriate
Example : nice sas regression.sas&
- The higher the nice value, the lower the priority
- Nice value 1 being the highest priority
- The kill command terminate a process
- For sure kill, type
Introduction to the Shell
Objectives
- Use redirection and pipes
- Use command grouping
What is a Shell ?
The shell-User Interface to UNIX command interpreter programming language
Redirection - Standard Input
- Redirecting input from a file : <
- Command < filename
Example : mail nhychi < letter.1
Redirection - Standard Output
- Redirect output to a file : >
- Command > filename
Example : ls -lR > list.file
- Redirect and append output to a file : >>
- Command >> filename
Example : ls -lR >> list.file
Redirection - Standard Error
- Redirecting error output to a file : 2 >
- Command 2 > filename
Example : cat file1 file2 2 > error.file
Line Continuation
the ‘/’ is used to continue a command on a separate line
Command Grouping
Commands can be entered on one line, separated by ‘;’
Pipes
- The standard output of each command becomes the standard input of the
next
- command | command
Example : ls | wc -w is similar to
Pipes and filters
- Filters are commands that can read from standard in and write to standard
out
- command | filter | command
Example : ps -e | grep cc | wc -l
- Filter can read its input from standard input and write its output
to standard output
- Filter can be used as an intermediate command between pipes