Download presentation
Presentation is loading. Please wait.
1
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
2
COMP111 Lecture 2 / Slide 2 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
3
COMP111 Lecture 2 / Slide 3 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: kwchiu Password:
4
COMP111 Lecture 2 / Slide 4 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
5
COMP111 Lecture 2 / Slide 5 UNIX File Utilities - Example (1) $ ls letter1secret $ 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 letter1letter2secret
6
COMP111 Lecture 2 / Slide 6 UNIX File Utilities – Example (2) $ mv letter1 letter3 $ ls -F letter2letter3secret/ $ lpr -Pcll3 letter2 $ mpage -Pcll3 letter2 $ rm letter2 $ ls -F letter3secret/
7
COMP111 Lecture 2 / Slide 7 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... ]
8
COMP111 Lecture 2 / Slide 8 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%)
9
COMP111 Lecture 2 / Slide 9 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 Memory results (on screen)
10
COMP111 Lecture 2 / Slide 10 Popular Shells sh Bourne shell (the original shell) csh C-shell (pronounced as “sea shell”) tcsh Like csh with more functions (default for our lab computers) bash “Bourne again” shell ksh Korn shell zsh Z-shell
11
COMP111 Lecture 2 / Slide 11 Utilities for Finding Info (1) who Who is logged on, where & when $ who horner 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 $ finger Login Name TTY Idle When Where horner Andrew Horner pts/0 12 Fri 09:52 csz096.cs.ust.hk clinton Bill Clinton pts/1 121 Fri 10:43 csnt1.cs.ust.hk
12
COMP111 Lecture 2 / Slide 12 Utilities for Finding Info (2) write Send message to another user $ whoami horner $ write clinton Bill, you’ve been idle for a long time! What are you doing? [hit CTRL-D to end write message] $ --------------------------------------------------------- $ whoami clinton 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? $
13
COMP111 Lecture 2 / Slide 13 Utilities for Interacting with Other Users: talk (1) talk chat for UNIX $ whoami horner $ 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?
14
COMP111 Lecture 2 / Slide 14 Utilities for Interacting with Other Users: talk (2) $ whoami clinton $ Message from Talk_Daemon@csz096.cs.ust.hk at 20:41... talk: connection requested by horner@csz096.cs.ust.hk. talk: respond with: talk horner@csz096.cs.ust.hk $ talk horner@csz096.cs.ust.hk [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?
15
COMP111 Lecture 2 / Slide 15 More Utilities (1) echo date head tail grep sort uniq
16
COMP111 Lecture 2 / Slide 16 More Utilities (2) echo Display 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
17
COMP111 Lecture 2 / Slide 17 More Utilities (3) head Display first few lines of file $ head -2 letter3 Ms. Lewinski: It is getting late. Please order some pizza and stop tail Display last few lines of file $ tail -2 letter3 Thanks! Bill grep Find a pattern in a file $ grep ”some pizza” letter3 It is getting late. Please order some pizza and stop
18
COMP111 Lecture 2 / Slide 18 More Utilities (4) sort Sort the lines in lexical order $ sort letter3 Bill It is getting late. Please order some pizza and stop Ms. Lewinski: Thanks! by my office. We'll tidy up a few more things before calling it a night. $ sort -r letter3 calling it a night. by my office. We'll tidy up a few more things before Thanks! Ms. Lewinski: It is getting late. Please order some pizza and stop Bill
19
COMP111 Lecture 2 / Slide 19 More Utilities (5) uniq Display file with duplicate adjacent lines removed $ cat names Bill Clinton Bill Gates Bill Clinton Monica Lewinski $ uniq names Bill Clinton Bill Gates Bill Clinton Monica Lewinski
20
COMP111 Lecture 2 / Slide 20 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
21
COMP111 Lecture 2 / Slide 21 Input/Output Redirection (2) Using the “>” character after a command to redirect output to a named file: $ sort names > names.sort $ uniq names.sort Bill Clinton Bill Gates Monica Lewinski This will create a file test: $ cat > test type line 1 type line 2 $ cat test type line 1 type line 2 uniq display sort names names.sort
22
COMP111 Lecture 2 / Slide 22 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 >> test type line 3 type line 4 $ cat test type line 1 type line 2 type line 3 type line 4 Append file1 to file2 $ cat file1 >> file2
23
COMP111 Lecture 2 / Slide 23 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.sort uniq display sort names names.sort
24
COMP111 Lecture 2 / Slide 24 Appending and Pattern Matching We have seen input 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.
25
COMP111 Lecture 2 / Slide 25 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
26
COMP111 Lecture 2 / Slide 26 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.