Ravindra BagaleCourses & study guides

6. Linux Advanced Commands

6.5 Archives and Compression: tar, gzip, zip

tar -czvf site-backup.tar.gz /var/www/html      # create gzip archive (c=create z=gzip v=verbose f=file)
tar -tzvf site-backup.tar.gz                    # list contents
mkdir -p /tmp/restore
tar -xzvf site-backup.tar.gz -C /tmp/restore    # extract into a folder
tar -cJvf logs.tar.xz /var/log/nginx            # xz compression (smaller, slower)
gzip big.log            # -> big.log.gz        gunzip big.log.gz
zip -r site.zip mysite/                         # install package 'zip' if missing
unzip site.zip -d /var/www/html/                # install package 'unzip' if missing

Why this matters for security

Backups are your last defence against ransomware and mistakes – but a site-backup.tar.gz left inside the web root can be downloaded by anyone who guesses the name. Directory brute-forcing tools (Gobuster, Part 10) look for exactly such files.

Ravindra Bagale's Tip

Before running tar -xzvf, many students don't check what's inside with -tzvf, and files get scattered in the wrong place. List the contents first, then extract with -C and a target folder. And never keep backups in the web root.

Practice task

Archive ~/labs into labs.tar.gz, list its contents, and extract it into /tmp/restore.