When you compile a large binary executable on a Linux system, it can often consume a significant amount of disk space. If you need to distribute this binary across a low-bandwidth network to hundreds of IoT edge devices, or if you simply want to save space on an embedded system with limited storage, you need to compress the file. However, if you use standard gzip, the end user will have to manually decompress the file before they can run it. To bypass this friction, you can use the Linux gzexe command to create a self-extracting, compressed executable.
How the gzexe Command Works
The gzexe utility is a simple shell script wrapper that comes bundled with standard gzip implementations. When you run it against a compiled binary, it compresses the original file and seamlessly wraps it in a tiny, automated decompression script.
The resulting file replaces the original binary on your disk. It retains the exact same filename and executable permissions. When a user attempts to run this new file, the wrapper script secretly intercepts the command, extracts the payload into a temporary directory (usually /tmp), executes the binary transparently, and then cleans up the temporary files once the program finishes running. To the end user, it appears as though they are running a completely normal, uncompressed application.
Creating a Self-Extracting Executable
Using the command is incredibly straightforward. First, ensure you have a standard, working executable binary (e.g., a compiled C program named my_app).
- Open your Linux terminal.
- Run the following command against your binary:
gzexe my_app - The terminal will output the compression ratio, indicating exactly how much space was saved:
my_app: 48.2%
If you run ls -l in your directory, you will notice that my_app is now significantly smaller. You will also notice a new file named my_app~. The gzexe utility always creates this backup file (denoted by the tilde) containing your original, uncompressed binary, just in case the compression corrupts the executable. Once you verify the compressed version runs correctly by typing ./my_app, you can safely delete the my_app~ backup file.
How to Decompress the Executable
If you ever need to reverse the process and permanently strip the self-extracting wrapper off the binary (perhaps to debug it with gdb or run objdump against it), you can instruct gzexe to decompress it.
Simply use the -d (decompress) flag:
gzexe -d my_app
This will instantly unpack the binary back to its original, full-size, uncompressed state on your disk.