How to Gzip Multiple Input Streams in Ubuntu Linux
This article explains how to generate a single gzip compressed file using multiple input sources on Ubuntu. Since the standard gzip command processes files individually, combining multiple streams requires specific utilities like tar or cat. You will learn the exact commands to merge and compress data efficiently without creating separate archives for each input.
The gzip command alone cannot merge multiple inputs into
one file; it compresses each argument separately. To create a single
compressed file from multiple sources, you should use the
tar command with the -z flag or pipe
concatenated streams into gzip.
Using Tar for Multiple Files
The most common method to archive and compress multiple files or
streams into one gzip file is using tar. This creates a
.tar.gz file.
tar -czf archive.tar.gz file1.txt file2.txt-c: Create a new archive.-z: Compress the archive using gzip.-f: Specify the filename of the archive.
Using Cat for Raw Input Streams
If you need to combine output from commands or raw streams into a
single .gz file without the tar archive format, use
cat to merge the streams and pipe the result to
gzip.
cat stream1 stream2 | gzip > output.gzYou can also use process substitution to combine command outputs directly:
cat <(command1) <(command2) | gzip > output.gzVerifying the Compressed File
After creating the file, verify its integrity using the
-t flag with gzip or list the contents with tar.
gzip -t output.gz
tar -tzf archive.tar.gzThese methods ensure multiple data sources are consolidated into a single compressed file on your Ubuntu system.