
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: June 16, 2025
In Linux, knowing how to redirect the output of a program to a ZIP file is important. As a system administrator, it allows us to save a program’s output neatly and organized. Also, this approach enables us to compress and archive the output, making it easier to share and manage.
In this tutorial, we’ll explore how to redirect the output of a program to a ZIP file.
Before we proceed, let’s take a look at the concepts involved:
In the next section, let’s discuss some approaches that we can use to achieve our goal.
To demonstrate, we’ll use a simple program, ls, which simply lists the contents of the current directory.
Here, we redirect the program output to a temporary file and then compress it:
$ ls > output.txt
In this example, the ls command output redirects to the file output.txt. At this point, we can use the cat command to ensure that this temporary file contains the desired content:
$ cat output.txt
file1.txt
projects
...
Above, the command displays the contents of the output.txt.
Next, let’s compress the temporary file output.txt into a ZIP file:
$ zip -q output.zip output.txt
Above, zip compresses output.txt into an archive named output.zip.
In this next approach, we utilize piping to provide the zip command with a program’s output:
$ ls | zip -q output.zip -
So, let’s explore the above command:
The command above generates the current directory contents and then compresses them, creating output.zip.
This combination of the tee and zip commands allows us to simultaneously show the ls program’s output in the terminal while creating a ZIP file containing this output in the background. To clarify, the details on the ongoing ZIP file creation process are not visible:
$ ls | tee >(cat > ls_output.txt) && zip output.zip ls_output.txt && rm ls_output.txt
file1.txt
projects
...
Let’s break down the above command:
So, the command above creates the ZIP archive outzip.zip. Now, let’s use the unzip command to inspect the contents of this zip archive:
$ unzip -l output.zip
Archive: output.zip
Length Date Time Name
--------- ---------- ----- ----
1358 2023-03-18 04:00 file1.txt
0 2023-12-18 12:12 projects/
...
Let’s have a look at this command:
From this output, we can see that output.zip contains the output of the ls command.
In this article, we explored how to redirect the output of a program and also use the zip command to compress data. In detail, we discussed piping program output directly to the zip command, redirecting this output to a temporary file, and utilizing the tee command. These approaches are straightforward to implement. Therefore, we can choose either based on preference.