BSD DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


FreeBSD Basics

Read The Friendly Manpage! -- Part Two

10/11/2000

In last week's article, we were left hanging in the /usr/share/man directory. In today's article, we'll manipulate the files in this directory and learn something about formatted text, unformatted text, compressed data, and control characters. And, after traveling this circuitous route, we may even learn something interesting about manpages.

Let's do an ls of the directory where the manpages are stored:

cd /usr/share/man
ls -aF
./         cat4/     catn/      man4/     mann/
../        cat5/     ja/        man5/     whatis
cat1/      cat6/     man1/      man6/
cat1aout/  cat7/     man1aout/  man7/
cat2/      cat8/     man2/      man8/
cat3/      cat9/     man3/      man9/

Remember that there are subdirectories for each of the 9 sections of the manual. The subdirectories that begin with man contain unformatted data; the subdirectories that begin with cat contain pre-formatted data. In just a moment, we'll do an exercise that will show the difference between formatted and unformatted data.

But first, do an ls of the man1 and cat1 directories; I've snipped my output (indicated by <snip>) to just show the first 10 lines of each. You can ls more than one directory at a time; use -C to keep the multi-column output:

ls -C man1 cat1 |more
man1:
./              indent.1.gz       pkg_version.1.gz
../             indxbib.1.gz      pl2pm.1.gz
CC.1.gz         info.1.gz         pod2html.1.gz
Mail.1.gz       install-info.1.gz pod2man.1.gz
[.1.gz          install.1.gz      popd.1.gz
a2p.1.gz        intro.1.gz        pr.1.gz
addftinfo.1.gz  introduction.1.gz printenv.1.gz
addr2line.1.gz  ipcrm.1.gz        printf.1.gz
alias.1.gz      ipcs.1.gz         ps.1.gz
alloc.1.gz      ipftest.1.gz      psbb.1.gz
<snip>

cat1:
./              indent.1.gz       pkg_version.1.gz
../             indxbib.1.gz      pl2pm.1.gz
CC.1.gz         info.1.gz         pod2html.1.gz
Mail.1.gz       install-info.1.gz pod2man.1.gz
[.1.gz          install.1.gz      popd.1.gz
a2p.1.gz        intro.1.gz        pr.1.gz
addftinfo.1.gz  introduction.1.gz printenv.1.gz
addr2line.1.gz  ipcrm.1.gz        printf.1.gz
alias.1.gz      ipcs.1.gz         ps.1.gz
alloc.1.gz      ipftest.1.gz      psbb.1.gz
<snip>

Notice that every file has a .gz extension. This means that all of the manpages have been compressed to conserve disk space. This is a good thing, as the online manual is huge. The utility used to compress the files is called gzip.

When I first discovered gzip, I thought, "What a great way to conserve disk space"; at the time I had a 602 MB drive and disk space was an issue. I merrily became the superuser (mistake number one), went to the root directory (mistake number two), and told gzip to compress every file on my FreeBSD system while giving me stats on how much space I had saved by issuing the command gzip -rv (trust me, you don't want to try that one). After I had finished rendering that installation of FreeBSD useless, I learned a valuable lesson: Keep the files that came compressed with FreeBSD compressed, and keep the files that came uncompressed with FreeBSD uncompressed.

However, feel free to compress any files you have created in your home directory; compression can also be very useful when you want to e-mail a file to someone. Let's say I want to e-mail a friend a PDF file; PDF files are notoriously large, and my poor friend is still using his old 14.4 kbps modem. I can save him some time downloading that e-mail attachment if I do this first:

cd ~/pdf_files
ls -l framerel.pdf
-rwxr-xr-x 1 genisis  wheel 31840 Sep 26 16:01 framerel.pdf

gzip -v framerel.pdf
framerel.pdf:  32.9% -- replaced with framerel.pdf.gz

ls -l framerel*
-rwxr-xr-x 1 genisis wheel 21392 Sep 26 16:01 framerel.pdf.gz

Notice that the gzip utility was able to compress this file by about a third of its original size; it also replaced the original file with its compressed counterpart and added a .gz extension to the original name.

When my friend receives this file, he won't be able to do anything with it until he uncompresses it like so:

gunzip framerel.pdf

Note that my friend didn't have to specify the .gz extension as gunzip assumes the file it is unzipping will have a .gz extension and will complain if it doesn't.

This is also a good time to introduce the file utility; if anyone e-mails you an attachment or you happen to find a file on your FreeBSD system and don't know what type of data it contains, don't just send it to your screen using the cat or more commands. If it is not a text file, it may do nasty things to your screen. The file command will tell you what type of data is contained within the file like so:

file framerel.pdf.gz
framerel.pdf.gz: gzip compressed data, deflated, original filename, last modified: Tue Sep 26 16:01:34 2000, os: Unix
gunzip frame*
file framerel.pdf
framerel.pdf: PDF document, version 1.2

This is very useful information as I now know that the contents of this file will appear as random garbage characters unless I use a reader specifically designed to read pdf files. I was nice when I named this file with a pdf extension; I could have just as easily named it something less descriptive like open_me.

Let's compare these outputs to an executable file, say the ls command:

whereis -b ls
ls: /bin/ls

file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, stripped

And finally, let's compare it to a file I created using an editor and saved as myfile:

file myfile
myfile: ASCII text

Out of the four file commands, the last command was the only one that revealed ASCII text; therefore, myfile is the only file that is safe to send to the more or cat commands or to a text editor.

Pages: 1, 2

Next Pagearrow





Sponsored by: