Repair Truncated gzip Names Safely with zforce

A file transfer strips the .gz suffix off a batch of archives, and zforce puts it back without you renaming each one by hand. The command renames files in place, so you will also learn how to preview the affected names and verify the result, while leaving ordinary files and names such as .tgz alone.

Allow about ten minutes for a small directory. The examples use gzip 1.12, installed here as Ubuntu package gzip 1.12-1ubuntu3.2. The exact package revision can differ on your machine, so check it before relying on the details in a script.

You need a shell, a writable directory containing the files, and enough permission to rename them. Most personal archives can be handled without sudo. Do not grant elevated privileges just to silence a permissions error: first check the directory owner and permissions.

1. Check the installed command

This is a read-only checkpoint. It confirms which executable will run and records the local package version:

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

On a non-Debian system, the final package query may not exist. The important check is that zforce belongs to the gzip installation you intend to use. This guide describes the installed GNU gzip 1.12 script, not an invented compatibility promise for every release.

2. Inspect the candidate directory

Move into the directory that contains the truncated names. Use a real path in place of /path/to/archive; do not use a broad path such as your home directory until you have inspected its contents.

$ cd /path/to/archive
$ find . -maxdepth 1 -type f -printf '%f\n' | sort

zforce accepts one or more file names. It does not scan subdirectories recursively. Its useful target is a file whose gzip data is present but whose name has lost the suffix, often after a file transfer.

Checkpoint: Write down the exact names you expect to change. If the list contains files that must not be renamed, stop and narrow the command arguments rather than passing a directory or a large glob.

3. Understand what will and will not change

There is no dry-run option in the installed command. Before running it, preview your explicit arguments with the shell:

$ printf '%s\n' report notes bundle.tgz

Shell globbing can make this less obvious. If you use a pattern, expand it first and review the result. An unmatched pattern may remain literal, and a pattern such as * can include more names than you intended.

4. Rename the files

Warning: Running zforce changes directory entries. It uses mv to replace each recognised file with its suffixed name. That is reversible only if the destination name is still available and you know the original name. Do not run it on an archive directory while another process is moving or creating files.

For a small, reviewed set, pass the names directly:

$ zforce report notes bundle.tgz
report -- replaced with report.gz

The output is deliberately selective. A successful rename prints the old and new names. Skipped files normally produce no line at all, so silence for notes and bundle.tgz is expected. The command's exit status is the stronger check:

$ printf 'exit status: %s\n' "$?"
exit status: 0

That status means every requested operand was processed without a reported error. It does not mean that every operand was renamed.

5. Handle a partial failure

If a name does not refer to a regular file, zforce prints an error and continues with the other operands. For example:

$ zforce report missing-file
report -- replaced with report.gz
zforce: missing-file not a file
$ printf 'exit status: %s\n' "$?"
exit status: 1

An exit status of 1 means you must review the directory. Some earlier operands may already have been renamed. Do not simply repeat the whole command from shell history: the successfully changed name now ends in .gz, and a later run will skip it, while the original missing operand may still be wrong.

To recover from an accidental rename, first confirm that the new name is the file you intended. Then rename it back, provided the original name is free:

$ mv -- report.gz report

Recovery: This undo command changes the name only; it does not repair damaged gzip data. If the destination name already exists, stop rather than overwriting it. Compare the files and choose a separate recovery directory.

6. Verify the repaired archive

Check that the new path exists and that gzip can read the compressed stream:

$ test -f report.gz
$ gzip -t report.gz
$ printf 'archive check: %s\n' "$?"
archive check: 0

gzip -t reads the archive and returns success without extracting it. This is the right verification for a renamed file: the operation should have changed only its name, but the check confirms that the contents are still a valid gzip stream.

You can inspect the archive metadata without extracting it:

$ gzip -lv report.gz
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla  ...

The date, sizes and CRC will depend on the file. Do not treat a particular line of metadata as a fixed test fixture. The useful result is that gzip lists the file and the earlier integrity check returned zero.

Common traps

Done means