Commands.page Logo

Compress Directory and Verify SHA256 Checksum on Ubuntu

This article provides a concise guide on archiving a folder and ensuring its integrity using SHA256 hashing within the Ubuntu terminal. You will learn how to create a compressed tarball, generate a checksum file, and verify the archive later to confirm that no data corruption has occurred during storage or transmission.

Compress the Directory

To begin, open your terminal and navigate to the parent folder containing the directory you wish to compress. Use the tar command to create a gzip-compressed archive. Replace my_folder with your actual directory name and archive.tar.gz with your desired archive filename.

tar -czvf archive.tar.gz my_folder

Generate the SHA256 Checksum

Once the archive is created, generate a SHA256 hash value. This acts as a digital fingerprint for the file. Run the following command in the same directory where the archive is located.

sha256sum archive.tar.gz > archive.tar.gz.sha256

This command calculates the hash and saves it to a new file named archive.tar.gz.sha256. You can view the content of this file using cat archive.tar.gz.sha256.

Validate the Checksum

To verify the integrity of the archive at a later time or on a different system, ensure both the archive and the checksum file are in the same directory. Use the -c flag with the sha256sum command to check the file against the stored hash.

sha256sum -c archive.tar.gz.sha256

If the file is intact, the terminal will return archive.tar.gz: OK. If the file has been altered or corrupted, you will receive a FAILED warning.