Tuesday, February 05, 2008

Linux : Compresses or decompresses files

bzip2 [options] [file-list]

bunzip2 [options] [file-list]

bzcat [options] [file-list]

bzip2recover [file]


The bzip2 utility compresses files; bunzip2 restores files compressed with bzip2; bzcat displays files compressed with bzip2.

Arguments

The file-list is a list of one or more files (no directories) that are to be compressed or decompressed. If file-list is empty or if the special option is present, bzip2 reads from standard input. The ––stdout option causes bzip2 to write to standard output.

Options

Accepts the common options described on page 587.

––stdout

–c

Writes the results of compression or decompression to standard output.

––decompress

–d

Decompresses a file compressed with bzip2. This option with bzip2 is equivalent to the bunzip2 command.

––fast or ––best

n

Sets the block size when compressing a file. The n is a digit from 1 to 9, where 1 (––fast) generates a block size of 100 kilobytes and 9 (––best) generates a block size of 900 kilobytes. The default level is 9. The options ––fast and ––best are provided for compatibility with gzip and do not necessarily yield the fastest or best compression.

––force

–f

Forces compression even if a file already exists, has multiple links, or comes directly from a terminal. The option has a similar effect with bunzip2.

––keep

–k

Does not delete input files while compressing or decompressing them.

––quiet

–q

Suppresses warning messages; does display critical messages.

––test

–t

Verifies the integrity of a compressed file. Displays nothing if the file is OK.

––verbose

–v

For each file being compressed displays the name of the file, the compression ratio, the percentage of space saved, and the sizes of the decompressed and compressed files.


Discussion

The bzip2 and bunzip2 utilities work similarly to gzip and gunzip; see the discussion of gzip (page 689) for more information. Normally bzip2 does not overwrite a file; you must use ––force to overwrite a file during compression or decompression.

Notes

The bzip2 home page is sources.redhat.com/bzip2.

The bzip2 utility does a better job of compressing files than gzip.

Use the ––bzip2 modifier with tar (page 788) to compress archive files with bzip2.

bzcat file-list

Works like cat except that it uses bunzip2 to decompress file-list as it copies files to standard output.

bzip2recover

Attempts to recover a damaged file that was compressed with bzip2.

Examples

In the following example, bzip2 compresses a file and gives the resulting file the same name with a .bz2 filename extension. The –v option displays statistics about the compression.

$ ls -l

total 728

-rw-r--r-- 1 sam sam 737414 Feb 20 19:05 bigfile

$ bzip2 -v bigfile

bigfile: 3.926:1, 2.037 bits/byte, 74.53% saved, 737414 in, 187806 out

$ ls -l

total 188

-rw-r--r-- 1 sam sam 187806 Feb 20 19:05 bigfile.bz2


Next touch creates a file with the same name as the original file; bunzip2 refuses to overwrite the file in the process of decompressing bigfile.bz2. The ––force option enables bunzip2 to overwrite the file.

$ touch bigfile

$ bunzip2 bigfile.bz2

bunzip2: Output file bigfile already exists.

$ bunzip2 --force bigfile.bz2

$ ls -l

total 728

-rw-r--r-- 1 sam sam 737414 Feb 20 19:05 bigfile

No comments: