380 likes | 459 Views
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.
E N D
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 • date command show current time and date. $ date Mon 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)
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
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:01 dan tty17 May 5 09:02 $ who -uH … $ whoami …
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 Office john John Cheng 01 Mon 09:01 cs522 dan Dan Quale 17 2d Sat 19:02 EE20
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 cheng@140.129.20.247 ...
Change Login Password • passwd command: change your logic password $ passwd Old password: New Password: Retype Nnew password: $
Change Login Password • Good Practice: change your password frequently • A. length of password > 5 char • B. users may change their own passwords • C. super user can change the password of any user • super user can have more option • -d delete password -a list all users
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 &
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 $ ps PID TTY TIME COMD 26344 02 0:14 -sh 29313 02 0:01 -ps $ ps -p 2150
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 $ ps PID TTY TIME COMD 26344 02 0:14 -emacs 29313 02 0:01 -ps $ kill -9 26344 $
Run Programs while logged off • nohup (no hangup) command: continue to execute a • process after logout. $ nohup nroffbook & 29673 Sending output to ‘nohup.out’
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 & 29678 Sending output to ‘nohup.out’
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 nroffbook 338.8 213.7 26.5 $
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 display just display $ echo $PATH :/bin:/usr/bin:/usr/local/bin:/home/kc/bin $
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 userB@IP Waiting for connect... $ talk userA@IP
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) $ stty speed 9600 baud; rows=25; columns=40 … $ stty erase \^h $ tty /dev/pts002 $ ls -l /dev/pts002
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 down all weekend for equipment installation $ cat /etc/greeting welcome to Unix $ cat /etc/greeting \ /etc/motd
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 fly Cats are fun Dogs sniff Fish can swin I like monkey $ pr -3 -l12 verse ...
fmt command • fmt: fill and join text, producing lines of roughly the same • length • Examples: • A. fmt -w 65 filename
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
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
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 chapt2 1071 11040 45818 chapt3 509 7210 29289 chapt4 606 7680 30910 chapt5 3278 35838 150532 total
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
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
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-4993 gmk 245-3209 arm 333-3903 $ sort telnos arm 333-3903 gmk 245-3209 kc 362-4993 $ sort +1 telnos gmk 245-3209 arm 333-3903 kc 362-4993
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
Search Text Patens in Files • fgrep: fast grep (does not support regular expression.) • egrep: support different regular expression (see manpage)
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-4993 gmk 245-3209 arm 333-3903 $ cut -f1 telnos kc gmk arm $
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-4993 gmk 245-3209 arm 333-3903 $ cut -f1 telnos >names $ cut -f2 telnos >nums $ paste nums names >newtelnos 362-4993 kc 245-3209 gmk 333-3903 arm $
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 -
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.35 1,333,545.80 $ tr ., ,. <numbs 1.300,5 900,35 1.333.545,80
Find Spelling Error • spell: compare the words of files with system dictionary • Examples: • A. spell file file2 >jargon
Encode Files • crypt: encrypt a file to prevent unauthorized access. • Examples: • A. crypt encryptKey <infile > outfile $ cat numbs 1,300.5 900.35 1,333,545.80 $ crypt xyzzy321 <numbs >numbs.cry
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
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 num one five two four three three