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.
The route
Jump straight to the step you need, or tick off Done means at the end.
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
- Already-suffixed names are skipped. The command skips names already ending in
.gzor.tgz, along with names ending in.zand.taZ. - A rename requires real gzip content. For another name to be renamed, it must be a regular file whose contents are recognised as a deflate gzip stream.
- The new name is just the old name plus .gz. So
reportcan becomereport.gz, a plain text file callednotesis not renamed, and a gzip file calledbundle.tgzis left as it is. - Nothing is decompressed or recompressed. The command does not inspect every file in the directory, only the ones you name.
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
- Expecting zforce to compress. It is about naming, not compression. It will not turn plain input into a gzip file; use a deliberate
gzipcommand for that. - Trusting a suffix as proof of contents. A file called
data.gzcan still be corrupt or not be gzip data at all. Conversely, a suffixless file can be a valid gzip stream. Usegzip -tto test the data, andzforceonly when restoring the conventional name is useful.
Done means
- You checked the installed zforce and gzip version.
- You reviewed the exact file names before running it.
- Recognised suffixless gzip files now end in
.gz. - Existing .gz and .tgz names were left alone.
- The command's exit status and any partial failures were reviewed.
- gzip -t passes for each repaired archive.