unix utility 常用的工具程式
DESCRIPTION
Unix Utility 常用的工具程式. Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology. Outline. System(Process)-related utilities (chap 7) Text File utilities (chap 8). Display Date and Time. - PowerPoint PPT PresentationTRANSCRIPT
1
Unix Utility常用的工具程式
Instructors: Fu-Chiung Cheng
(鄭福炯 )Associate Professor
Computer Science & EngineeringTatung Institute of Technology
2
Outline
• System(Process)-related utilities (chap 7)• Text File utilities (chap 8)
3
Display Date and Time
• date command show current time and date.
$ dateMon May 4 09:01:03 EST 1999$ date ‘+%a %d:%m:19%y’ Mon 04:05:1999$ date ‘+It is exactly %H:%M and %S seconds.’It is exacly 09:01 and 03 seconds.
• format: A. %H: hour(two digits) B. %M: minute (two digits) C. %S: second (two digits)
4
Date command
• other format: C. %a: day(Sun, Mon, Tue, Wed, Thu, Fri, Sat) D. %w: day(0..6) 0 is Sunday E. %j: Julia day (001..366) F. %m: month (two digits) G. %d: date (two digits) H. %y: year (two digits) I. %T: time in HH:MM:SS (24 hour format) format J. %n: new line• supre user can change date. >date mmddhhnn[yy] mm:month dd:date hh:hour nn:minute yy:year
5
List logged-in Users
• who command displays information about the current status of the system• whoami: list user‘s information
$ who john tty01 May 5 09:01dan tty17 May 5 09:02$ who -uH …$ whoami…
6
List users‘ information
• finger command lists the information of users in the system: A. Name and where (office): from /etc/password B. when: login time C. idle time:
$ finger Login Name TTY Idle When Officejohn John Cheng 01 Mon 09:01 cs522dan Dan Quale 17 2d Sat 19:02 EE20
7
List users‘ information
• finger userName@remoteHost (or IP) A. show detail information of userName@remoteHost B. .plan file .project (unix predefined files) C. try to use editor to add some information to .plan and/or .project
$ finger [email protected]...
8
Change Login Password
• passwd command: change your logic password
$ passwdOld password:New Password:Retype Nnew password:$
9
Change Login Password
• Good Practice: change your password frequentlyA. length of password > 5 charB. users may change their own passwordsC. super user can change the password of any user
• super user can have more option• -d delete password -a list all users
10
Processes
• A process is a program that is being executed.• Invoke two emacs programs and one vi program
• Processes are kept in kernel’s process table. • Process-related commands: ps, kill, nice, time, nohup
$ emacs &$ vi &$ emacs &
11
List Processes
• ps command: list your processes• the options of ps may different platforms (use man ps)• each process has a process identification number (pid)• some options: -e all processes -f all information -p pid -t tty -u uid
$ psPID TTY TIME COMD26344 02 0:14 -sh29313 02 0:01 -ps$ ps -p 2150
12
Kill Processes
• You may use Ctrl-C to kill a forground process• to kill a background process: use kill command• how to make a background process: $textedit &• how to make a background process forgrond: $fg• How to use kill? A. use ps to find
out the pid . B. kill pid or
kill -9 pid
$ psPID TTY TIME COMD26344 02 0:14 -emacs29313 02 0:01 -ps$ kill -9 26344$
13
Run Programs while logged off
• nohup (no hangup) command: continue to execute a process after logout.
$ nohup nroffbook &29673Sending output to ‘nohup.out’
14
Run processes at lower priority
• nice: run processes with lower priority.• Examples: A. >nice program & B. >nice -5 program C. >nice --5 program (superuser only)• niceness value: 1-19 (higher value => lower priority).
$ nice nroffbook &29673$ nice nohup nroffbook &29678Sending output to ‘nohup.out’
15
Time Prcesses
• time command: measure running time of a process. A. wall time:total elapsed time B. user time: execution time (I/O) C. CPU time: system time
$ time nroffbook338.8 213.7 26.5$
16
Display Message
• echo command displays message to console.
• echo “What is the beginning and end of eternal life?”
$ echo ‘just display’just display$ echo just displayjust display$ echo $PATH:/bin:/usr/bin:/usr/local/bin:/home/kc/bin $
17
Communication with other users
• write, talk, ntalk: communication with other user• Examples: A. write userName B. talk userName@host• write: communication within the same host• talk, ntalk: communication through network• mesg -n: do not want to talk to anyone
$ talk userA@IP$ talk userB@IPWaiting for connect...
18
Set/Display terminal information
• tty, stty: set or display terminal information• Examples: A. tty (the terminal information may be used in write command) B. stty C. stty erase \^h kill \^u intr \^c (keyboard setting)
$ tty /dev/pts002$ ls -l /dev/pts002
$ stty speed 9600 baud;rows=25; columns=40…$ stty erase \^h
19
Chap 8Text File Untilities
20
Type Files
• cat command concatenates files to standard output.• Examples: A. cat file B. cat file1 file2 file3 > files C. cat file[123] >files D. cat file? > files E. cat file* > files F. cat /dev/null > empty G. cat > stdout• note that cat file3 file1 > file31 but cat file[31] > file13
$ cat /etc/motd system will be downall weekend forequipment installation$ cat /etc/greetingwelcome to Unix$ cat /etc/greeting \ /etc/motd
21
Format Files
• pr command: format one or more files• Examples A. >pr -h head -l15 file B. >pr -3 -l12 file C. >pr -3 -a -l12 file D. >pr -n • try: A. ls > temp1 B. pr -h cheng -l15 temp1
$ cat verse Birds can flyCats are funDogs sniffFish can swinI like monkey$ pr -3 -l12 verse...
22
fmt command
• fmt: fill and join text, producing lines of roughly the same length• Examples: A. fmt -w 65 filename
23
Print Files
• • lp: send file to printer (lpr)• Examples: A. lp -n 5 (5 copies) -m (send mail) report B. ls -l /bin /usr | pr | postprint |lp -d ps
24
Browse Text File
• • pg: display files on a terminal, one page at a time.• Examples: A. pg file1 file2 B. pg -p ‘Page%d :’ file• Prompt: A. Enter: next page -1Enter: previous page B. q or Q: quit pg C. h for help
25
Count Lines, words and Characters
• • wc command: print the counts of character, words, lines• Examples: A. wc file B. wc -w file C. who | wc -l D. wc file*
$ wc chapt? 408 2007 12093 chapt1 684 7921 32313 chapt21071 11040 45818 chapt3 509 7210 29289 chapt4 606 7680 30910 chapt53278 35838 150532 total
26
Compare files
• • diff: report the lines that differ between file1 and file2• Examples: A. diff file1 file2 < file1... > file2... B. diff -w file1 file2
$ diff arlinote arlinote2 5c5< ice rink.--->ice skating rink.10all>Susan - 586-1234
27
Compare files
• • diff3: report the lines that differ between three files• $diff3 file1 file2 file3 ==== all three files differs ====1 file 1 different ====2 file 2 different ====3 file 3 different
28
Sorting File• sort: sort the lines of the files in alphabetical order• Examples: A. wc -l *|sort -r B. sort +2n -t: /etc/passwd +2 skip two fields n sort in arithmetic order C. build telnos as shown in textbook try >sort telnos >sort +1 telnos >sort telnos > telnos.temp D. sort +0.1 -0.2 /etc/passwd
$ cat telnos kc 362-4993gmk 245-3209arm 333-3903$ sort telnosarm 333-3903gmk 245-3209kc 362-4993$ sort +1 telnosgmk 245-3209arm 333-3903kc 362-4993
29
Search Text Patens in Files
• • grep, egrep, fgrep: search one or more files for lines that match a regular expression.• Exit status: 0: if any lines match 1: no lines match 2. Error • Examples: A. grep userName /etc/passwd B. grep “any string to be searched” /usr/include/* C. grep “any string to be searched” /usr/include/*/* D. grep -i string files
30
Search Text Patens in Files
• • fgrep: fast grep (does not support regular expression.)• egrep: support different regular expression (see manpage)
31
Rearrange Columns of Files
• • cut: select a list of columns or fields form one or more files -d delimiter (default is tab) -f fields to be kept -c fields to be cut• Examples: A. cut -d: -f1,5 /ect/passwd B. who |cut -d” “ -f1 C. cut -c4 file|paste - file
$ cat telnos kc 362-4993gmk 245-3209arm 333-3903$ cut -f1 telnoskcgmkarm $
32
Rearrange Columns of Files• paste: merge corresponding lines of one or more files into a vertical columns, separated by a tab.
$ cat telnos kc 362-4993gmk 245-3209arm 333-3903$ cut -f1 telnos >names$ cut -f2 telnos >nums$ paste nums names >newtelnos362-4993 kc245-3209 gmk333-3903 arm $
33
Rearrange Columns of Files
• paste options: -d delimiter (default is tab) - replace a filename with the standard input• Examples: A. paste f1 f2 f3 > file123 B. who | paste -d -
34
Translate Characters
• • tr: copy standard input to standard output, and perform substitution.• Examples: A. cat file | tr ‘[A-Z]’ ‘[a-z]’ B. tr ‘ ‘ ‘\012’ < file
$ cat numbs 1,300.5 900.351,333,545.80$ tr ., ,. <numbs 1.300,5 900,351.333.545,80
35
Find Spelling Error
• • spell: compare the words of files with system dictionary• Examples: A. spell file file2 >jargon
36
Encode Files
• • crypt: encrypt a file to prevent unauthorized access. • Examples: A. crypt encryptKey <infile > outfile
$ cat numbs 1,300.5 900.351,333,545.80$ crypt xyzzy321 <numbs >numbs.cry
37
Duplicate File
• tee: read standard input and diverts it both to the standard output and to one or more named files.• Example: $spell chapt5 | tee errwords
38
Print the first/last few lines of a files
• head: print the fist few line• tail: print the last few line• Examples: A. grep “pid_t” /usr/include/* |head -20 B. grep “pid_t” /usr/include/* |tail -20
$ echo “one\ntwo\nthree\nfour\nfive” > num $ head -3 num $tail -3r numone fivetwo fourthree three