Donation?

Harley Hahn
Home Page

Send a Message
to Harley


A Personal Note
from Harley Hahn

Unix Book
Home Page

SEARCH

List of Chapters

Table of Contents

List of Figures

Chapters...
   1   2   3
   4   5   6
   7   8   9
  10  11  12
  13  14  15
  16  17  18
  19  20  21
  22  23  24
  25  26

Glossary

Appendixes...
  A  B  C
  D  E  F
  G  H

Command
Summary...

• Alphabetical
• By category

Unix-Linux
Timeline

Internet
Resources

Errors and
Corrections

Endorsements


INSTRUCTOR
AND STUDENT
MATERIAL...

Home Page
& Overview

Exercises
& Answers

The Unix Model
Curriculum &
Course Outlines

PowerPoint Files
for Teachers

Exercises and Answers for Chapter 21...

Displaying Files

Review Question #1:

Which programs do you use to display:

• A text file one screenful at a time
• An entire text file all at once
• The first part of a text file
• The last part of a text file
• A binary file

Answer

Use the following program to display...

less a text file one screenful at a time
cat an entire text file all at once
head the first part of a text file
tail the last part of a text file
hexdump a binary file
od a binary file

Review Question #2:

As you are using less to display a file, which commands do you use to perform the following actions?

• Go forward one screenful
• Go backward one screenful
• Go to the first line
• Go to the last line
• Search forward
• Search backward
• Display help
• Quit the program

Answer

<Space> go forward one screenful
b go backward one screenful
g go to the first line
G go to the last line
/ search forward
? search backward
h display help
q quit the program

Review Question #3:

You can use less to display more than one file, for example:

less file1 file2 file3 file4 file5

As you are reading, which commands do you use to perform the following actions?

• Change to the next file
• Change to the previous file
• Change to the first file
• Delete the current file from the list

Which commands do you use to search forward and search backward within all the files?

Answer

:n change to the next file
:p change to the previous file
:x change to the first file
:d delete the current file from the list
/* search forward within all the files
?* search backward within all the files

Review Question #4:

What command would you use to watch the end of a growing file?

Answer

tail -f

Review Question #5:

When you display a binary file, what is canonical format? How do you display a file in canonical format using hexdump? Using od?

Answer

When displaying or printing binary data, canonical format consists of 16 bytes per line such that:

• To the left of each line is the offset in hexadecimal

• In the middle are the actual bytes, also in hexadecimal

• On the right are the ASCII equivalents

To display a binary file in canonical format use either:

hexdump -C
od -Ax -tx1z

Applying Your Knowledge #1:

Check the value of your PAGER environment variable. If it is not set to less, do so now. Display the man page (Chapter 9) for the less program itself. Perform the following operations:

• Display help information
• Page down to the end of help, one screenful at a time
• Take a moment to read each screen
• Quit help

• Search forward for "help"
• Search again
• Search again
• Search backward

• Go to the end of the man page
• Go backward one screenful
• Go to line 100
• Display the current line number
• Go to the line 20 percent of the way through the man page
• Display the current line number
• Go to the beginning of the man page

• Quit

Answer

To check the value of your PAGER environment variable:

echo $PAGER

To set PAGER to less for the Bourne Shell family (Bash, Korn Shell):

export PAGER=less

To set PAGER to less for the C- Shell family (C-Shell, Tcsh):

setenv PAGER less

To display the man page for less:

man less

To perform the following operations:

h display help information
<Space> page down to the end of help, one screenful at a time
q quit help
   
/help search forward for "help"
n search again
n search again
N search backward
   
G go to the end of the man page
b go backward one screenful
100g go to line 100
= display the current line number
20p go to the line 20 percent of the way through the man page
= display the current line number
g go to the beginning of the man page
   
q quit

Applying Your Knowledge #2:

You want to use less to display the contents of the file list, which contains many lines of text. Along with the text, you also want to see line numbers. However, there are no line numbers within the text, and you do not want to change the original file in any way.

How would you do this using nl and less?

How would you do it using only less?

Is there any advantage to using nl?

Answer

Using nl and less:

nl list | less

Using only less:

less -N list

The advantage to nl is that you can use options to control the numbering. You cannot do this with less -N.

Applying Your Knowledge #3:

Convert the following binary (base 2) number to octal (base 8), hexadecimal (base 16), and decimal (base 10):

1111101000100101

You must show your work.

Answer

base  2 = 1111101000100101

base  8 = 1 111 101 000 100 101 = 175045

base 16 = 1111 1010 0010 0101 = FA25

base 10 = (1*8^5) + (7*8^4) + (5*8^3) + (0*8^2) + (4*8^1) + (5*8*0)
        = (1*32768) + (7*4096) + (5*512) +(0*64) + (4*8) + (5*1)
        = 32768 + 28672 + 2560 + 0 + 32 + 5
        = 64037

Applying Your Knowledge #4:

Use the strings command (Chapter 19) to look for character strings within the binary file /bin/ls.

Then select one string and use hexdump or od to find the exact position of that string in the file.

Answer

To look for character strings within /bin/ls:

strings /bin/ls

For Further Thought #1:

You want to use less to search through five different files, looking for a particular sequence of words. The obvious solution is to use less as follows:

less file1 file2 file3 file4 file5

However, this requires you to keep track of and manipulate five different files. Instead you might first combine the files into one large file:

cat file1 file2 file3 file4 file5 | less

Will this make your job easier or harder? Why?

Answer

Combining the files will make your job harder, because when you find what you want, you won't know which file it was in.

The easiest way to do the job is to start less with five different files, and use the /* command to search all the files for the particular sequence of words you want to find.

For Further Thought #2:

In the text, we discussed four numbers systems: decimal (base 10), binary (base 2), octal (base 8) and hexadecimal (base 16). In principle, any positive whole number can be used as a base.

Consider base 12, also called DUODECIMAL. In base 12 we use the digits 0 through 9. For the extra two digits, we use A and B. Thus, in base 12, we count as follows: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, 10, 11, 12, and so on. The number 22 in base 10 equals 1A in base 12.

What important advantages does base 12 have over base 10? Hint: How many factors does 12 have compared to 10? How does this simplify calculations?

Would our culture be better off if we used base 12 instead of base 10?

In spite of its advantages, we don't use base 12 with computers. Most of the time we use base 16, which is much more complicated. Why is this?

By the way, if you have ever read the Lord of the Rings books by J.R.R. Tolkein, you will be interested to know that the Elvish languages use a duodecimal number system.

Answer

The number 12 has 6 factors (1, 2, 3, 4, 6, 12). The number 10 has only 4 factors (1, 2, 5, 10).

Because base 12 has more factors, calculations are easier, especially division. Also, more values can be expressed as simple fractions. For example, there are 12 items in a "dozen". Consider the following:

  1 = 1/12 of a dozen
  2 = 1/6 of a dozen
  3 = 1/4 of a dozen
  4 = 1/3 of a dozen
12 = a whole dozen

Now, let's make up a unit called "ten" that contains 10 items. Consider:

  1 = 1/10 of a ten
  2 = 1/5 of a ten
  5 = 1/2 of a ten
10 = a whole ten

(Take a moment to think about this.)

Our culture would be better off using base 12 than base 10, as calculations, especially those using division and fractions, would be easier. Note that there are:

12 items in a dozen
12 dozen in gross
12 inches in a foot
12 pence in a shilling (old British system)
12 hours on the face of the clock
12 troy ounces in a troy pound

The troy ounce and troy pound, were medieval measurements, widely used at one time. The name "troy" comes from the city of Troyes, France, an important mediaeval trading center. Today, troy units are used only to measure precious metals, gemstones and gun powder. A troy ounce is defined as 480 (4x12) grains. A troy pound is defined as 12 troy ounces.

The ancient Romans used a fraction system based on 12. Within this system, the word uncia was used to denote a type of coin and a unit of length. This word became the ancestor of both the English words "ounce" and "inch".

We use base 16 with computers even though it is more complex than either base 10 or 12. This is because (as explained in the text) base 16 can be thought of as a compact way to express base 2, and base 2 represents the off/on system fundamental to computer data storage.

Jump to top of page