View
219
Download
0
Embed Size (px)
Citation preview
UNIX Utilities
Learning Objectives:1. To understand the some basic utilities of UNIX
File
2. To compare UNIX shell and popular shell
3. To learn Input/Output Redirection and the Pipe function
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 22
UNIX Utilities
Table of Content Getting Started with UNIX Basic UNIX File Utilities UNIX File Utilities – Example The UNIX Shell Popular Shells Utilities for Finding Info Utilities for Interacting with Other Users More Utilities Input/Output Redirection Pipes
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 33
Getting Started with UNIX
The machines in CS Lab2 are named csl2su1 .. csl2su40. csl2su3 means “CSLab2, Sun#3” The full machine name for csl2su4 is: csl2su4.cs.ust.hk You can access these computers with telnet from other
computers via the Internet:telnet csl2su4.cs.ust.hk
(from other UNIX computers or even Windows) You need to log in to a UNIX computer with a valid account and
password:UNIX(r) System V Release 4.0 (csl2su4)login: kwchiuPassword:
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 44
Basic UNIX File Utilities
ls list files in current directory cat display (concatenate) file more display one screen of file rm remove (delete) a file cp copy source file to target file mv rename or move a file lpr print a file man online UNIX help manual mpage print multiple pages on postscript printer
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 55
UNIX File Utilities - Example (1)
$ ls
letter1 secret
$ cat letter1
Ms. Lewinski:
It is getting late. Please order some pizza and stop
by my office. We’ll tidy up a few more things before
calling it a night.
Thanks!
Bill
$ cp letter1 letter2
$ ls
letter1 letter2 secret
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 66
UNIX File Utilities – Example (2)
$ mv letter1 letter3
$ ls -F
letter2 letter3 secret/
$ lpr -Pcll3 letter2
$ mpage -Pcll3 letter2
$ rm letter2
$ ls -F
letter3 secret/
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 77
UNIX File Utilities – Example (3)
$ man ls
Reformatting page. Wait... done
User Commands ls(1)
NAME
ls - list contents of directory
SYNOPSIS
/usr/bin/ls [ -aAbcCdfFgilLmnopqrRstux1 ] [ file... ]
/usr/xpg4/bin/ls [ -aAbcCdfFgilLmnopqrRstux1 ] [ file... ]
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 88
UNIX File Utilities – Example (4)
DESCRIPTION
For each file that is a directory, ls lists the contents of
the directory; for each file that is an ordinary file, ls
repeats its name and any other information requested. The
output is sorted alphabetically by default. When no argu-
ment is given, the current directory is listed. When
several arguments are given, the arguments are first sorted
appropriately, but file arguments appear before directories
and their contents.
There are three major listing formats. The default format
for output directed to a terminal is multi-column with
--More--(5%)
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 99
The UNIX Shell
The UNIX shell listens to what you type and executes commands at your request.
User command:lpr file UNIX
Shell
UNIX
Kernel
Command
Library
ls, lpr, mv, rm,
telnet, netscape,
...
result or status
Printers
Files
Memoryresults (on screen)
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1010
Popular Shells
sh Bourne shell (the original shell) csh C-shell (pronounced as “sea shell”) tcshLike csh with more functions (default for our
lab computers) bash “Bourne again” shell ksh Korn shell zsh Z-shell
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1111
Utilities for Finding Info (1)
who Who is logged on, where & when
$ whohorner pts/0 Jan 29 09:52 (csz096.cs.ust.hk)clinton pts/1 Jan 29 10:43 (csnt1.cs.ust.hk)
finger A bit more login information
$ fingerLogin Name TTY Idle When Wherehorner Andrew Horner pts/0 12 Fri 09:52 csz096.cs.ust.hkclinton Bill Clinton pts/1 121 Fri 10:43 csnt1.cs.ust.hk
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1212
Utilities for Finding Info (2)
write Send message to another user$ whoamihorner$ write clintonBill, you’ve been idle for a long time! What are you doing? [hit CTRL-D to end write message]$ ---------------------------------------------------------$ whoamiclinton Message from horner on csz096.cs.ust.hk [ Fri Jan 29 20:18:47
. Bill, you’ve been idle for a
long time! What are you doing? <EOT>$
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1313
Utilities for Interacting with Other Users: talk (1) talk chat for UNIX
$ whoamihorner$ talk clinton[Waiting for your party to respond][Connection established]Hi Bill, what’s up?
+-----------------------------------------------------+Hi! I’m a little busy right now. Is it okay if I call you back latter?
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1414
Utilities for Interacting with Other Users: talk (2)$ whoamiclinton$ Message from [email protected] at 20:41 ...talk: connection requested by [email protected]: respond with: talk [email protected]$ talk [email protected][Waiting for your party to respond][Connection established]Hi! I’m a little busy right now. Is it okay if I call you back latter?
+-----------------------------------------------------+Hi Bill, what’s up?
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1515
More Utilities (1)
echo date head tail grep sort uniq
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1616
More Utilities (2)
echoDisplay command line input to screen$ echo Hi, I am Bill, the President of the US!
Hi, I am Bill, the President of the US!
date Print the date and time$ date
Wed Feb 3 12:13:07 HKT 1999
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1717
More Utilities (3)
headDisplay first few lines of file$ head -2 letter3Ms. Lewinski:It is getting late. Please order some pizza and stop
tailDisplay last few lines of file$ tail -2 letter3Thanks!Bill
grep Find a pattern in a file$ grep ”some pizza” letter3It is getting late. Please order some pizza and stop
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1818
More Utilities (4)
sort Sort the lines in lexical order $ sort letter3BillIt is getting late. Please order some pizza and stopMs. Lewinski:Thanks!by my office. We'll tidy up a few more things beforecalling it a night.
$ sort -r letter3calling it a night.by my office. We'll tidy up a few more things beforeThanks!Ms. Lewinski:It is getting late. Please order some pizza and stopBill
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 1919
More Utilities (5)
uniqDisplay file with duplicate adjacent lines removed
$ cat namesBill ClintonBill GatesBill GatesBill ClintonMonica Lewinski
$ uniq namesBill ClintonBill GatesBill ClintonMonica Lewinski
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2020
Input/Output Redirection (1)
On UNIX, the standard input (stdin) is the keyboard; the standard output (stdout) is the display screen.
$ sort
waits for you to type in the data from the keyboard and displays the sorted data on the screen.
sort
keyboard
display
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2121
Input/Output Redirection (2)
Using the “>” character after a command to redirect output to a named file:
$ sort names > names.sort$ uniq names.sortBill ClintonBill GatesMonica Lewinski
This will create a file test:$ cat > testtype line 1type line 2<ctrl-d> $ cat testtype line 1type line 2
uniq
display
sort
names
names.sort
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2222
Input/Output Redirection (3)
Using the “>>” character after a command to redirect output to APPEND to a named file:
Typing to the end of a file
$ cat >> testtype line 3type line 4<ctrl-d>
$ cat testtype line 1type line 2type line 3type line 4
Append file1 to file2
$ cat file1 >> file2
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2323
Input/Output Redirection (4)
Using the “<” character after a command to redirect input from a named file:
$ uniq < names.sort
This is the same as:
$ uniq names.sort
Using input and output redirection together:
$ sort < names > names.sort
uniq
display
sort
names
names.sort
names.sort
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2424
Appending and Pattern Matching
We have seen input redirection (cat <file) and output redirection (cat >file). We can also append to a file using >>
$ date > file$ who >> file
Simple file pattern matching The * pattern matches any number of characters:
$ ls -l letter*
lists all files in the working directory that start with “letter” The ? pattern matches any single character:
$ ls -l letter?
lists all files in the working directory that start with “letter” followed by exactly one character.
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2525
Pipes (1)
The standard output of a program can be “piped” into the standard input of another program:
$ sort names | uniq
Bill Clinton
Bill Gates
Monica Lewinski
uniq
display
sort
names
COMP111COMP111Lecture 2 / Slide Lecture 2 / Slide 2626
Pipes (2)
Several pipes can be connected:$ sort names | uniq | grep "Bill"
Bill Clinton
Bill Gates
Pipes and I/O redirection can be used together:$ sort -r names | uniq >names.rev
$ cat names.rev
Monica Lewinski
Bill Gates
Bill Clinton