Bash Tutorial · Linux System Skills · Lesson 27 of 30
Bash Archives, Compression and Checksums
Create and extract tar.gz archives, compress files, and verify downloads with checksums.
tar
- Create:
tar -czf backup.tar.gz dir/(c=create, z=gzip, f=file) - List:
tar -tzf backup.tar.gz - Extract:
tar -xzf backup.tar.gz -C target/
mkdir -p site/css
echo "<h1>Home</h1>" > site/index.html
echo "body{}" > site/css/app.css
tar -czf site.tar.gz site
tar -tzf site.tar.gz | sort
mkdir restore && tar -xzf site.tar.gz -C restore
cat restore/site/index.html
Output
site/ site/css/ site/css/app.css site/index.html <h1>Home</h1>
Run this on your computer — see “Get Started” for setup.
Checksums
Verify that a download wasn't corrupted or tampered with by comparing its SHA-256 with the published value:
echo "release 1.4.0" > app-1.4.0.bin
sha256sum app-1.4.0.bin > SHA256SUMS
sha256sum -c SHA256SUMS
echo "tampered" >> app-1.4.0.bin
sha256sum -c SHA256SUMS 2>/dev/null || echo "checksum mismatch detected"
Output
app-1.4.0.bin: OK app-1.4.0.bin: FAILED checksum mismatch detected
Run this on your computer — see “Get Started” for setup.
Test yourself
Which command extracts archive.tar.gz?
tar -czf archive.tar.gztar -xzf archive.tar.gzunzip archive.tar.gzgzip archive.tar.gz
Show answer
Answer: tar -xzf archive.tar.gz