20
CS 497C – Introduction to UNIX Lecture 7: General- Purpose Utilities Chin-Chih Chang [email protected]

CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang [email protected]

Embed Size (px)

Citation preview

Page 1: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

CS 497C – Introduction to UNIXLecture 7: General-Purpose

Utilities

Chin-Chih [email protected]

Page 2: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

script: Record Your Session

• If you want to keep track of all your activities, you can invoke the script command after you log in:

$ scriptScript started, file is typescript$• All your keystrokes that you enter get

recorded in the file typescript.

Page 3: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

script: Record Your Session• You can view this file with the cat

command.• Script overwrites any previous typescript

file.• To append activities to existing file

typescript, use: script -a• To log activities to a different log file, use: script logfile

Page 4: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

clear and tput: Clear Your Screen• There are two commands available in most

UNIX systems to clear your screen – clear and tput.

• The clear command is used without arguments:

clear• The tput command is used with the clear

argument: tput clear

Page 5: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

clear and tput: Clear Your Screen• tput can als position the cursor at a specific

location or be used to highlight text. tput cup 10 20• You can boldface your text by using the

smso and rmso arguments. smso starts highlighting the text and rmso ends highlighting.

tput smso echo Come to the Web tput rmso

Page 6: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

uname: Know Your Machine’s Name

• If your machine is connected to the Internet, then your machine name forms part of your machine’s domain name (a series of words separated by dots).

• Use –n option to show the machine name in a network:

$ uname –nsisko• Use –r option to show the operating

system’s version.

Page 7: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

date: Display The System Date

• The UNIX system maintains an internal clock meant to run perpetually. When the system is shut down, a battery backup keeps the clock ticking.

• The clock actually stores the number of seconds elapsed since January 1, 1970.

• You can display the current date with the date command.

Page 8: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

date: Display The System Date

• The date command can display specific format by a + symbol followed by the % operator and a single character describing the format:– d – the day of the month (1 to 31)– m – month in the number format (1 to 12)– h – month name– y – the last two digits of the year– H, M, and S – the hour, minute and second,

respectively.

Page 9: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

date: Display The System Date• You can combine them in the format where

the + symbol precedes a string which specifies the requested format within a pair of “ symbols.

• To show the month name, date, and, you can use the following command:

$ date +”%h %d %y”Sep 3 01• To change the system date, you need the

administrator permission.

Page 10: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

cal: The Calender• cal is a handy tool that you can invoke

anytime to see the calendar of any specific month or a complete year.

• To show December, use:$ cal dec• To show the year of 2002, use:$ cal 2002 | more• The | symbol connects two commands (in a pipeline) where more takes input from the cal command.

Page 11: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

calendar: A useful Reminder Mechanism

• The calendar command provides a useful reminder mechanism for a user. The command searches a file named calendar in the current directory

$ cat calendar

Sep 3 Labor Day

Sep 5 Howework Due

Page 12: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

calendar: A useful Reminder Mechanism

• You can use date and calendar in a sequence to show those lines containing today’s and tomorrow’s dates.

$ date

Mon Sep 3 12:30:14 CDT 2001

$ calendar

Sep 3 Labor Day

Page 13: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

bc: The Calculator• Two types of calculators are available in

UNIX – a graphical object (the xcalc command) and the character-based bc command.

• bc belongs to a family of commands (called filters) that expect input from the keyboard when used without any argument.

• Key in the following arithmetic expression:$ bc12 + 517

Page 14: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

bc: The Calculator

• bc added the two numbers and showed the output in the next time.

• To quit bc, you have to use [Ctrl-d] that marks the end of input.

• You can also ask bc to perform calculations together:

12*12; 2^32

144

4294967296

Page 15: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

bc: The Calculator

• By default, bc performs truncated division, and you have to set scale to the number of digits of precision before you perform any division:

9/5

1

scale = 2

17/7

2.42

Page 16: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

bc: The Calculator

• bc is quite useful in converting numbers from one base to another.

• An example to convert a binary number to a decimal one is shown as follows:

ibase=2

11001010

202

Page 17: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

bc: The Calculator• The reverse is possible: obase = 2 14 1110• You can convert from one base to the

another: obase = 16 14 E

Page 18: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

bc: The Calculator• bc can be used with variables: x = 3; y = 4; z = 5 p = x + y + z p 12• bc is a pseudo-programming language

featuring arrays, functions, conditionals (if) and loops (for and while). It also comes with a library for performing scientific calculations.

Page 19: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

More General Purpose Commands

• Copy, rename and delete files with cp, mv and rm.

• View and print a file with cat, more, and lp.

• Compress a file with compress and gzip.

• Find out disk space utilization with df and du.

Page 20: CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang chang@cs.twsu.edu

More General Purpose Commands

• Extract the two ends of a file with head and tail.

• Cut and past a file with cut and paste.

• Find differences between two files with comm, cmp, and diff.

• Sort a file’s contents with sort.