Other Utilities


Other Utilities

This section introduces you to a few more utilities from the GNU software packages shown in Table 8-1. The list is not exhaustive; I include only a few selected utilities. Following are the utilities summarized in the following sections:

  • GNU bc: Arbitrary precision calculator

  • gzip: Utility for compressing and expanding files

  • patch: Utility for applying changes to a text file

GNU bc

GNU bc enables you to enter arbitrary precision numbers and to perform various calculations with these numbers. GNU bc implements the arbitrary precision-calculation capability the POSIX P1003.2/D11 draft standard specifies.

GNU bc is installed as part of the base operating system during the Red Hat Linux installation on the companion CD-ROMs.

If you have GNU bc installed, you should be able to run it with the following command:

bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

After displaying the banner, bc waits for your input (there is no prompt), then you can enter numbers and expressions by using syntax similar to that of the C programming language.

Numbers are the basic elements in bc. A number is treated as an arbitrary precision number with an integral and fractional part. You can enter numbers and evaluate expressions just as you write expressions in C, as the following example shows:

1.000000000000000033 + 0.1
1.100000000000000033

As soon as you enter an expression, bc evaluates the it and displays the result.

You can also define variables and use them in expressions, as follows:

cost=119.95
tax_rate=0.05
total=(1+tax_rate)*cost
total
125.94

If you check this result with your calculator, you’ll notice that bc truncates the result of multiplication (it does the same with division). If you want, you can retain more significant digits by setting the scale variable, as follows:

scale=10
total=(1+tax_rate)*cost
total
125.9475

In this case, the result has more significant digits.

The bc utility supports an entire programming language with a C-style syntax. You can write loops and conditional statements and even define new functions with the define keyword. The following example shows how you might define a factorial function:

define factorial (n) {
  if(n <= 1) return (1);
  return (factorial(n-1)*n);
}

As you can see, the factorial function is recursive; it calls itself. Entering the lines as shown in the example is important. In particular, bc needs the open curly brace ({) on the first line because that brace tells bc to continue reading input until it comes to a closing brace (}).

After you define the factorial function, you can use it just as you might call a C function. Following are some examples:

factorial(3)
6
factorial(4)
24
factorial(10)
3628800
factorial(40)
815915283247897734345611269596115894272000000000
factorial(50)
30414093201713378043612608166064768844377641568960512000000000000
factorial(100)
933262154439441526816992388562667004907159682643816214685929638952175\
999932299156089414639761565182862536979208272237582511852109168640000\
00000000000000000000

As the example shows, you can use bc to represent as large a number as you want; no limit exists. When necessary, bc displays the result on multiple lines, with each continuation line ending in a backslash (\) followed by a new line.

To quit bc, type the following command:

quit

To learn more about bc, consult its man pages by using the man bc command. After you learn about new features of bc, try them interactively.

gzip

The gzip program is the GNU zip compression utility used to compress all Linux software distributions. When you run gzip with a filename as an argument, it reduces the file’s size by using the Lempel-Ziv (LZ77) compression algorithm and stores the result in a file with the same name, but with an additional .gz extension. Thus, if you compress a file with the gzip files.tar command, the result is a compressed file with the name files.tar.gz.

The same gzip program can decompress any file gzip has compressed. To decompress the file files.tar.gz, for example, you can simply type the following:

gzip -d files.tar

Notice that you do not have to type the .gz extension explicitly; gzip automatically appends a .gz extension when it is looking for the compressed file. After decompressing, the utility creates a new file with the name files.tar.

Instead of gzip -d, you can use the gunzip command to decompress a file:

gunzip files.tar

This command also looks for a file named files.tar.gz and decompresses the file, if found.

By default, gzip stores the original filename and timestamp in the compressed file. You can decompress a file with the -N option to restore the original filename and timestamp.

Following is the basic syntax of the gzip command:

gzip [-cdfhlLnNrtvV19] [-S .xxx] [file ...]

The options have the following meanings:

  • -c writes output to standard output.

  • -d decompresses the file.

  • -f forces compression or decompression, even if the file has multiple links or the corresponding file already exists.

  • -h displays a help screen and quits.

  • -l lists files sizes before and after compression.

  • -L displays the gzip license and quits.

  • -n stops gzip from storing the original filename in the compressed file.

  • -N restores the original filename during decompression.

  • -q suppresses all warnings.

  • -r causes gzip to traverse the directory structure recursively and operate on all files.

  • -S .xxx causes gzip to use the suffix .xxx instead of .gz.

  • -t tests the integrity of a compressed file.

  • -v displays the name and percentage of compression for each file.

  • -V displays the version number and the compiler options used to build that version of gzip.

  • -1 uses the fastest compression method (even though compression may not be as much).

  • -9 uses the slowest compression method (provides the most compression).

  • file ... specifies one or more filenames (the files to be compressed or decompressed).

The gzip utility is installed automatically when you install Red Hat Linux from the companion CD-ROMs.

patch

The GNU patch utility is designed to apply patches, or corrections, to files. The basic idea behind patch is that when you want to distribute changes in a file, you run the standard UNIX diff command and generate a diff file that indicates how the file should be changed, then you distribute that diff file to everyone who has the original file. The recipient runs the patch utility with the diff file as input and patch makes the changes in the original files.

Cross Ref 

The patch utility is installed automatically when you install Linux from the companion CD-ROMs. Chapter 21 discusses how to use patch to apply changes to the Linux kernel sources when you upgrade the kernel from one version to the next.

You can learn more about patch through a simple example. Assume that you have a file named original.txt that contains the following text:

Version: 1.0

Revision history:
  10/26/2002: Original file (NB)

This text file is used as an example to illustrate how to use diff and patch to update a file.

Suppose that you already distributed the original.txt file to several users. (Pretend that the file is the source file of a computer program.) After a while, you make some changes in this file. The new file, named revised.txt, looks like the following:

Version: 1.1

Revision history:
  10/26/2002: Original file (NB)
   4/19/2003: Added a new line (LB)

Now, you want to provide these changes to your users so they can use the new file. Your first task is to create a diff file that captures the changes you have made.

To create the diff file, run diff with the -u option, and specify the two filenames as arguments—the original file, followed by the revised one. Thus, the following command creates the diff file for the current example:

diff -u original.txt revised.txt > patch-1.1

This command creates the file patch-1.1, which is what you distribute to your users who are currently using the file original.txt. This diff file is also referred to as the patch file.

When a user receives the patch file, all he or she needs to do is put the patch file in the same directory where the file original.txt resides and type the following command:

patch < patch-1.1
patching file 'original.txt'

The patch command applies the changes directly to the file original.txt. After patch finishes, you’ll find that the content of the original.txt file matches that of revised.txt. This example should give you a good idea of how to use the patch utility to update text files.