<<

. 11
( 69 .)



>>

fdesc 1 1 0 100% /dev
<volfs> 512 512 0 100% /.vol
automount -fstab [314] 0 0 0 00% /Network/Servers
automount -static [314] 0 0 0 100% /automount

The last five file systems in the above list correspond to internal
systems used by the Unix operating system. You can safely ignore
them and only look at the first listing. According to this listing, the
above computer has a single disk that is 69 percent full.




¤ Type the name of a ¤ Type a directory or
DETERMINE TOTAL LIST DISK SPACE IN KILOBYTES
DIRECTORY SIZE directory or file and press filename and press Return.
„ Type du -k and a space.
„ Type du -s and a space. Return.
– The shell displays the size
– The shell displays the total of files in kilobytes.
size of the directory in
blocks.


39
UNIX FOR MAC



VIEW TEXT FILES
The other type of file that the cat command can display

Y
ou can display the content of text files with the cat
is a binary file. Binary files contain special characters that
command. The name cat is short for concatenate,
you cannot print, because the computer codes for these
which means to join several things together into one.
characters do not correspond to any printable symbols. The
However, the cat command has additional functions
executable files that programmers create from compiled
beyond concatenation, one of which is to display files
applications are often binary files, as are proprietary data
on-screen. You can display any file by typing this command:
formats such as Microsoft Word documents.
cat filename
If you try to use the cat command to display a binary file,
the best that you can expect is gibberish in your Terminal
The screen displays the full contents of the file. If the file is
window. The worst result is that it resets the display format
longer than one screen, your Terminal window scrolls to
of your Terminal window, meaning that your prompt and
display the whole file.
any letters you type also appear as gibberish.
The two types of files that you can display using the cat
To display the contents of a binary file safely, you can use
command are text and binary files. A text file consists of
the -v option with the cat command. This command
ordinary letters, numbers, spaces, and punctuation,
displays non-printable characters by showing a caret (^)
collectively known as plain text. If you are using a word-
before control characters, and M- before Meta characters.
processing program and you save the file as text, you are
creating a text file.

VIEW TEXT FILES




– The shell displays the
¤ Type the name of a text
VIEW TEXT FILES VIEW BINARY FILES
file and press Return. contents of the file, with
„ Type cat and a space. „ Type cat -v and a space. non-printing characters
– The shell displays the ¤ Type the name of a binary shown.
contents of the file. file and press Return.
– This is an example of a
Meta-Control-T character.


40
4
WORK WITH TEXT



CREATE A SIMPLE TEXT FILE
make simple text files. Output redirection means that you

Y
ou can create a text file by using the cat command
save the results of a command to a file instead of displaying
and redirecting the output to a file. If you type cat
them on-screen. To redirect the output of the cat
and press Return, the cat command appears to do
command to a file, you can type the following:
nothing, but your prompt does not reappear. When you
type something else and press Return, you just see whatever
cat > filename
you typed echo back at you. This means that the cat
command is letting you enter content. You are building a This creates a new file called filename. For better
virtual file that only exists for the execution of the compatibility with the Mac OS X Finder, you may want to
command. As long as you continue to type, you are adding name a text file with the extension .txt. This allows you to
content to that virtual file. double-click the icon for the file in Finder and open it in the
TextEdit application.
To indicate that you are finished entering text into the cat
command, you must indicate the end of your virtual file. You The contents of the new file are whatever you type after
do this by pressing Return and then pressing Control + D. pressing Return. The file can contain multiple lines; just
press Return at the end of each line. When you are done,
By itself, the ability to type text and see it reappear
you can press Control + D to create the file.
on-screen is not particularly useful. However, you can
combine the cat command with output redirection to


CREATE A SIMPLE TEXT FILE




„ Type cat and a space. ‹ Type the name of the file › Type the text you want Á Press Control + D.
you want to create, and press the file to contain.
– The cat command creates
¤ Type a greater-than Return.
ˇ Press Return.
symbol and a space. a file containing the text you
typed.




41
UNIX FOR MAC



VIEW TEXT FILES AS PAGES
If you try to use the less command on a binary file with

Y
ou can display long files one screen at a time using
unprintable characters, the less command warns you and
the less command. If you try to view a long file with
asks if you want to continue. You can type y, for yes, and press
the cat command, it quickly scrolls your Terminal
Return, or simply press Return to cancel the less command.
window, and you must use the scroll bars to go back. The
less command is an example of a pager command, a
You have already used the less command if you have used
program that displays files one page at a time.
the man command; when you type man, you read a manual
page by viewing it as you would with the less command.
The less command displays a screen at a time, and waits
for you to finish reading that screen before continuing.
The name of the less command makes sense when you
When you are ready to move ahead in the file, you can
know the history of pager commands in Unix. The original
press the Spacebar.
command for displaying a page at a time was named more,
because, while waiting for you to read the page, it printed
You can also search ahead in the file for a specific text
--More-- on the bottom line of the Terminal window. The
pattern by typing a forward slash (/) and then the text you
less command was named as a natural successor to the
want to find. This moves you ahead in the file to the next
more command, because it could do more than the more
place that text appears.
command could do.



VIEW TEXT FILES AS PAGES




– The less command – The shell displays the next
‹ Press the Spacebar.
VIEW A FILE, PAGE BY PAGE
displays the first page of the page of text.
„ Type less and a space. content of the file.
¤ Type the name of a file
and press Return.




42
4
WORK WITH TEXT


In addition to pressing the Spacebar to page through a file using
the less command, or the man command, you can also use other
keystrokes to move through the file. For a full listing of keystrokes
in less, type man less or type h while using the less command.

KEYSTROKE MEANING
Return Go forward one line.
Down arrow Go forward one line.
Up arrow Go back one line.
Go back one page.
b
Search backwards in the file.
?pattern
Go to the first line of the file.
g
Go to the last line of the file.
G
Open the file in a Unix text editor.
v
Read the less command help.
h
Quit immediately.
q




– The cursor appears in – The less command skips
SEARCH AHEAD FOR
A CERTAIN WORD the lower-left corner of the ahead to that word or phrase
› Type a forward slash. Terminal window, beside the and highlights it.
forward slash.
ˇ Type a word or phrase
and press Return.



43
UNIX FOR MAC



VIEW PORTIONS OF A TEXT FILE
head -linenumber filename

Y
ou can display a portion of a text file by using the
tail -linenumber filename
head and tail commands. To understand how the
head and tail commands work, it is useful to For example, type head -3 /usr/share/dict/words to see the
visualize a text file as if it were a fish being chopped into first three lines of the file /usr/share/dict/words. Several
head, tail, and body. The head command displays the Unix programs use this text file as a source of English words,
portion of the fish containing its head, and the tail so it makes a good example for text commands.
command shows the portion containing the tail of the fish.
You can give additional options that indicate where along Typing tail -3 /usr/share/dict/words shows you the last three
the body of the fish you want to cut. lines of this file. You can also tell the tail command to
start a certain number of lines in from the beginning of the
Without any options, the head command shows the first file by including a plus symbol:
ten lines of a specified file and the tail command shows
the last ten lines. If the file has ten or fewer lines, the whole tail +linenumber filename
file displays.
If you want to view the tail of the words file starting at line
You can change the number of lines that display in the 234,567 ” it is a large file ” you can type tail +234567
following way: /usr/share/dict/words.




VIEW PORTIONS OF A TEXT FILE




¤ Type a filename and press ‹ Type head and a space. ˇ Type the filename and
VIEW THE BEGINNING
OF A TEXT FILE Return. press Return.
› Type a dash, the number

<<

. 11
( 69 .)



>>