compress Command
Purpose
Compresses data.
Syntax
compress [ -c ] [ -C ] [ -d ] [ -F ] [ -f ] [ -n ] [ -q ] [ -v ] [ -V ] [ -b Bits ] [ File ... ]
Description
The compress command compresses data, by using adaptive Lempel-Ziv coding to reduce the size of files. Each original file that is specified by the File parameter is replaced when possible by a compressed file with a .Z appended to its name. If the invoking process has appropriate privileges, the compressed file retains the same ownership, modes, and modification time of the original file. If the path of the file that is specified is more than 1023 bytes, the command does not work. If no files are specified, the standard input is compressed to the standard output. If compression does not reduce the size of a file, a message is written to standard error and the original file is not replaced.
The amount of compression depends on the size of the input, the number of bits per code specified by the Bits variable, and the distribution of common substrings. Typically, source code or English text is reduced by 50 to 60%. The compression of the compress command is more compact and takes less time to compute than the compression achieved by Huffman coding (as used in the pack command) or adaptive Huffman coding.
Flags
Item | Description |
---|---|
-b Bits | Specifies the maximum number of bits to use to replace common substrings in the file. The value of the Bits variable must be in the range from 9 bits through 16 bits. The default value of the Bits variable is 16 bits. When compressing data, the algorithm first uses all of the 9-bit codes (257 through 512) to replace as many substrings as possible. Then it uses all 10-bit codes, and so on, continuing until the limit that is specified by the -b flag is reached. |
-c | Writes to standard output. No files are changed. |
-C | Produces output compatible with the Berkeley Software Distribution (BSD) Revision 2.0. |
-d | Causes the compress command to function exactly like the uncompress command. |
-f or -F | Forces compression. The -f and -F flags are
interchangeable. Overwrites the File.Z file if it already
exists. After the value of the Bits variable is attained, the compress command periodically checks the compression ratio. If it is increasing, the compress command continues to use the existing code dictionary. However, if the compression ratio decreases, the compress command discards the table of substrings and rebuilds it. Rebuilding the table allows the algorithm to adapt to the next block of the file. When the .Z file already existss, if the -f flag is not given, and the process is not running in the background, it prompts to verify whether to overwrite the existing .Z file. |
-n | Omits the compressed file header from the compressed file. Note: If this option is used, the
-n flag must also be used when using the uncompress command
to decompress the file.
|
-q | Suppresses the display of compression statistics that are generated by the -v flag. If several -v and -q flags are on the same command line, the last one specified controls the display of the statistics. |
-v | Writes the percentage of compression. |
-V | Writes the current version and compile options to standard error. |
Parameters
Item | Description |
---|---|
File | Specifies the file to compress. |
Return Values
If an error occurs, the exit status is 1. If the compress command exits without compressing a file, it exits with a status of 2. Otherwise, the compress command exits with a status of 0.
The compress command detects an error and exits with a status of 1 if any of the following events occur:
- An input file is not a regular file.
- An input file name is too long to append the .Z extension.
- An input file cannot be read or an output file cannot be written.
Exit Status
Item | Description |
---|---|
0 | Successful completion. |
1 | An error occurred. |
2 | One or more files were not compressed because they might have increased in size (and the -f flag was not specified). |
>2 | An error occurred. |
Security
Example
To compress the foo file and write the percentage of compression to standard error, enter the following command:
compress -v foo
The foo file is compressed and renamed foo.Z.