Recompress Legacy .Z Files with znew Without Losing the Source

znew converts a legacy Unix .Z file into a leaner gzip stream, tests the result, and tidies up after itself. Read the options before you run it on anything you care about, because the default behaviour deletes your source file once the new one checks out. The installed command is from gzip 1.12, package version 1.12-1ubuntu3.2. Allow about ten minutes, plus the time needed to read or copy the files you are converting.

Important: a successful conversion normally removes the original .Z file. Work on a disposable copy first, or use a backup outside the directory being converted. sudo is not normally needed. Use it only if the files or destination are genuinely inaccessible to your account.

1. Check the installed command

This is a read-only check, but worth doing anyway: the short option list has stayed the same for years, and you want to know that before you trust it on a real file:

$ command -v znew
/usr/bin/znew
$ znew --version
znew (gzip) 1.12
...
$ dpkg-query -W -f='${Package} ${Version}\n' gzip
gzip 1.12-1ubuntu3.2

The short option list in the local manual is -ftv9PK. The installed script also accepts --help and --version. This guide uses the local implementation, so a different gzip release may have slightly different diagnostics.

2. Put a test copy in a working directory

Choose an actual legacy file, then copy it before allowing znew to change anything. Replace the placeholder path with your file:

$ mkdir -p /tmp/znew-work
$ cp --preserve=all -- /path/to/ARCHIVE.Z /tmp/znew-work/
$ ls -l /tmp/znew-work/ARCHIVE.Z
-rw-r--r-- 1 you you ... /tmp/znew-work/ARCHIVE.Z

The operand may be written as /tmp/znew-work/ARCHIVE.Z or as /tmp/znew-work/ARCHIVE. In both cases, znew looks for the name with a .Z suffix and creates the matching .gz name. Keep the suffix spelling exact: ARCHIVE.Z and archive.Z are different files on a normal Linux filesystem.

Checkpoint: Stop if the copied file is not present or is not readable. Do not run the conversion against a guessed path.

3. Convert and test the copy

Run -t and -v together. The new gzip file is tested before the old .Z file is removed, while -v prints the file name and compression statistics:

$ znew -tv /tmp/znew-work/ARCHIVE.Z
/tmp/znew-work/ARCHIVE: 42.1% -- replaced with /tmp/znew-work/ARCHIVE.gz
$ printf 'exit status: %s\n' "$?"
exit status: 0
$ ls -l /tmp/znew-work
total ...
-rw-r--r-- 1 you you ... ARCHIVE.gz

The percentage and file sizes depend on the data, so treat the output above as its shape rather than a promised number. A status of zero means this invocation completed successfully. Check the gzip stream independently as a useful second checkpoint:

$ gzip -t -- /tmp/znew-work/ARCHIVE.gz
$ printf 'gzip test status: %s\n' "$?"
gzip test status: 0

At this point the copy has been converted and the copied .Z file has gone. The original at /path/to/ARCHIVE.Z is still available because step 2 used a separate working copy. Once you have checked the result, move or copy the .gz file into its final location using your normal change-control process.

4. Preserve the source when disk blocks make it worthwhile

Use -K when you want znew to keep the .Z file if it occupies fewer disk blocks than the new gzip file:

$ znew -vK /tmp/znew-work/ARCHIVE.Z
/tmp/znew-work/ARCHIVE.Z smaller than /tmp/znew-work/ARCHIVE.gz -- unchanged
$ ls -l /tmp/znew-work/ARCHIVE.Z /tmp/znew-work/ARCHIVE.gz

-K implies -t. It does not promise to keep every source file. If the gzip result uses fewer blocks, the source is removed after the tested conversion. The comparison is about filesystem blocks, not simply the byte count shown by ls. If keeping the source is a hard requirement, retain your separate backup instead of relying on -K.

This is the main distraction trap: -t means test before deletion, not test and keep. There is no dry-run option in the local znew(1) interface.

5. Handle an existing gzip file explicitly

If ARCHIVE.gz already exists, do not assume it is a harmless duplicate. Without -f, conversion will not force replacement. Inspect it first:

$ ls -l /tmp/znew-work/ARCHIVE.Z /tmp/znew-work/ARCHIVE.gz
$ gzip -t -- /tmp/znew-work/ARCHIVE.gz
$ printf 'existing gzip status: %s\n' "$?"
existing gzip status: 0

Only after deciding that replacement is correct should you use -f:

$ znew -ftv /tmp/znew-work/ARCHIVE.Z
/tmp/znew-work/ARCHIVE: ... -- replaced with /tmp/znew-work/ARCHIVE.gz

Warning: -f permits replacement of the existing gzip output, and a successful run still removes the .Z input. If the old gzip may be needed, copy it first:

$ cp --preserve=all -- /tmp/znew-work/ARCHIVE.gz /tmp/znew-work/ARCHIVE.gz.backup

Do not delete that backup until you have compared the new file with the data you expect. Removing it is irreversible unless another backup exists.

6. Choose compression level and disk usage deliberately

The default compression level is used unless you pass -9. That option asks gzip for the slowest method and optimal compression, which can take more CPU time. It does not change the file format or restore the .Z source:

$ znew -9tv /tmp/znew-work/ARCHIVE.Z

Use -P when free disk space is tight. It pipes decompression directly into gzip rather than first creating an uncompressed intermediate file. The conversion is still a replacement operation, so keep a backup and check the destination filesystem before starting a large batch.

$ df -h -- /tmp/znew-work
$ znew -Ptv /tmp/znew-work/ARCHIVE.Z

The local manual records two portability limits for -P: timestamps may not be preserved if touch lacks -r, and permissions may not be preserved if chmod lacks --reference. The installed Linux tools support those options, but check metadata with stat when it matters.

7. Recover from a failed conversion

Keep the original backup until the gzip file passes gzip -t and any application-level check you require. With -t or -K, a failed decompression or integrity test is designed to leave the source unchanged. A failure during conversion can still leave an incomplete or uncompressed temporary result, so inspect the directory rather than guessing:

$ ls -la /tmp/znew-work
$ file -- /tmp/znew-work/ARCHIVE.Z /tmp/znew-work/ARCHIVE.gz
$ gzip -t -- /tmp/znew-work/ARCHIVE.gz

If the source has disappeared and the new file is unusable, restore the backup with an explicit copy, not a blind overwrite of an unknown path:

$ cp --preserve=all -- /path/to/ARCHIVE.Z /tmp/znew-work/ARCHIVE.Z

For a batch, pass several names only after testing one representative file. znew reports a missing .Z file and continues to the other operands, then returns a non-zero status if any conversion failed. Check that status in scripts and review every resulting directory.

Done means