Compress and Run Linux Binaries: The Ultimate gzexe Guide

Overview

When administering storage-constrained Linux systems or embedded appliances, conserving drive space without adding operational friction is a key objective. The gzexe utility provides an elegant solution: it compresses executables and shell scripts in place while embedding a self-extracting stub, allowing binaries to run normally without requiring manual unpacking beforehand. This hands-on walkthrough covers the full lifecycle of using gzexe—from initial compression and runtime validation to inspecting internal wrapper logic, cleaning up artifacts, and restoring original uncompressed files with the -d switch.

Setting Up a Test Workspace and Sample Executable

Begin by isolating our experiments inside a dedicated working directory. Running mkdir -p ~/gzexe_lab && cd ~/gzexe_lab initializes the directory structure and switches into it.

Creating and entering the gzexe experiment directory

Next, create a mock script that queries basic host diagnostics to serve as our target payload by executing cat << 'EOF' > test_script.sh.

Writing sample diagnostics shell script

Because newly authored shell scripts lack execution bits by default, assign execution rights via chmod +x test_script.sh.

Granting execution permissions to test script

To establish an uncompressed baseline for file dimensions and permissions, inspect the target using ls -lh test_script.sh.
The standard file metadata reveals an initial footprint of roughly 109 bytes alongside standard user execution permissions.

Checking initial uncompressed script size and permissions

Verify baseline behavior prior to applying any packaging routines by executing ./test_script.sh.
The output confirms standard terminal reporting of the active machine name, logged-in identity, and current memory distribution.

Executing baseline diagnostics script

Compressing Binaries and Validating Runtime Execution

Before proceeding, confirm the utility’s presence on your system. Running which gzexe || (apt-get update -qq && apt-get install -y gzip) validates the binary location or retrieves the underlying gzip package containing it on Debian/Ubuntu installations.

Verifying gzexe installation and resolving dependencies

Under the hood, gzexe shrinks targets using gzip-compatible algorithms while wrapping the payload in a miniature decompressor script.
⚙️ [Primary Parameter]

-d : Reverses the compression routine, returning packed executables back into their baseline formats.
To pack the newly authored diagnostic tool, run gzexe test_script.sh.

Executing gzexe against the target script

Examine the resulting file listing with ls -la test_script* to assess what transpired on disk.
Notice that test_script.sh has been converted into the packed wrapper, whereas the untouched original script has been safely safeguarded with a trailing tilde as test_script.sh~.

Reviewing generated files and backup artifacts

To reveal the structural transformation applied to our script, query its file header with file test_script.sh.
Rather than identifying as a plain text Bash file, the utility classifies the artifact as a self-decompressing POSIX shell script wrapper.

Inspecting file classification of the compressed wrapper

Validate seamless execution by launching the compressed script directly via ./test_script.sh.
The embedded decompressor seamlessly expands the payload into an ephemeral sandbox behind the scenes, emitting identical telemetry without requiring manual intervention.

Validating execution of the compressed executable

Investigating Wrapper Internals and Managing Backup Files

To observe how automatic extraction functions behind the curtain, view the top portion of the wrapper using head -n 15 test_script.sh.
The listing highlights how the utility orchestrates temporary directory allocation, signal trapping, and dynamic unpacking pipelines prior to payload execution.

Viewing wrapper extraction logic in script header

Contrasting the packaged binary with its pristine backup using diff -u test_script.sh~ test_script.sh || true exposes the extent of the modification.
The comparison clarifies the divergence between human-readable plaintext instructions and the newly generated extraction wrapper paired with raw compressed bytes.

Comparing original plaintext against compressed wrapper

Once operational stability has been affirmed and the safety copy is no longer required, eliminate the backup to reclaim disk space via rm -f test_script.sh~.

Purging intermediate backup file

Verify the current contents of the workspace by invoking ls -la test_script.sh.
The directory listing now reflects a clean workspace containing strictly the standalone compressed executable.

Verifying workspace status after backup cleanup

Restoring the Original Binary with gzexe -d

Whenever debugging, code modifications, or plain binaries are needed again, restore the packed executable to its native state with gzexe -d test_script.sh.

Decompressing the script with gzexe -d

Inspect directory contents after decompression using ls -la test_script*.
The utility automatically shifts the compressed version into the tilde-appended backup (test_script.sh~) while replacing the active filename with the fully restored original code.

Verifying restored files and secondary backup creation

Confirm that the restored binary has regained its pristine identity by running file test_script.sh.
The output verifies that the wrapper envelope is completely gone, reverting the asset back to a clean POSIX shell script text document.

Confirming file type reversion back to pure shell script

Inspect the uncompressed codebase directly using cat test_script.sh.
Every single line of the initial diagnostics script is intact, completely free of runtime decompression overhead or binary segments.

Inspecting pristine source contents of restored script

Perform a final sanity check on the uncompressed file by invoking ./test_script.sh.
The script executes without issues, printing identical host metrics just as it did upon initial authoring.

Final operational test of restored script

The gzexe command remains a dependable and pragmatic utility for reducing binary disk overhead in bare-metal deployments, minimal containers, and lightweight environments. Incorporating this approach into routine systems workflows gives administrators an easy path to reclaim drive space without sacrificing runtime convenience.