Ubuntu Command to Compress and Verify Directory
This article provides a direct solution for compressing a directory and verifying the archive integrity on the Ubuntu operating system. It outlines the specific terminal commands required to create a compressed backup and confirm its validity immediately after the process completes.
The standard utility for this task in Ubuntu is tar.
While there is no single flag within tar that compresses
and cryptographically verifies a gzip archive in one step, you can
achieve this by chaining two commands together using the logical AND
operator (&&). This ensures the verification step
only runs if the compression succeeds.
To compress a directory named my_folder into an archive
called backup.tar.gz and verify it immediately, run the
following command in the terminal:
tar -czf backup.tar.gz my_folder && tar -tzf backup.tar.gz > /dev/null
In this command, the -czf flags create a gzip-compressed
archive. The && operator proceeds to the next
command only if the first one finishes without errors. The second part,
tar -tzf, lists the contents of the archive. If the archive
is corrupted, this listing will fail, alerting you to the issue
immediately. Redirecting the output to /dev/null keeps the
terminal clean while still performing the integrity check.
Alternatively, if you have the zip utility installed,
you can use a single command flag to test the archive integrity after
creation. The command is:
zip -r -T backup.zip my_folder
The -r flag recurses into the directory, and the
-T flag tests the integrity of the archive after it is
created. However, tar is generally preferred on Ubuntu for
preserving file permissions and system attributes during
compression.