If you need to backup a massive folder containing 10,000 log files, or if you need to email a massive database to a colleague, you cannot move those files in their raw state. Moving 10,000 individual files across a network is incredibly slow, and uncompressed databases will rapidly consume your bandwidth.
You must combine all the files into a single archive, and then compress that archive to make the file size as small as possible. While Windows users rely on the .zip format, the Linux ecosystem relies entirely on the tar command to achieve this.
Understanding Tarballs (.tar.gz)
The tar command (which stands for Tape Archive, a relic from the 1970s) actually performs two separate actions simultaneously when combined with modern flags:
- Archiving: It takes thousands of loose files and bundles them together into one single block (a
.tarfile). This block is not compressed; it is just a container. - Compressing: It then takes that solid block and runs it through the gzip compression algorithm, crushing the file size down drastically.
The resulting file will end with the extension .tar.gz. In the Linux community, this final compressed file is affectionately referred to as a “tarball.”
The Essential Command: tar -czvf
To create a compressed tarball, you will use the tar command followed by four very specific flags. You must memorize these four letters: czvf.
- -c (Create): Tells the utility you want to create a brand new archive (not extract an old one).
- -z (Gzip): Tells the utility to immediately compress the archive using the gzip algorithm to save space.
- -v (Verbose): Forces the terminal to print out the name of every single file as it is being packed, so you can visually confirm the command is working and hasn’t frozen.
- -f (File): Tells the utility that the very next word you type will be the name of the new file.
How to Compress a Single Directory
The syntax requires you to type the command, then the name you want the new file to have, and finally the name of the folder you want to compress.
tar -czvf new_archive_name.tar.gz /path/to/folder/
For example, if you want to backup your entire web server folder (/var/www/html/) and save it as a file named website_backup.tar.gz, you would type:
tar -czvf website_backup.tar.gz /var/www/html/
The terminal will rapidly scroll through a list of every single HTML, CSS, and image file as it packs them into the archive. When the scrolling stops, your new tarball will be sitting in your current directory, ready to be safely moved or emailed.
How to Compress Multiple Specific Files
You do not have to compress an entire folder. If you only want to compress three specific log files, you simply list them one after another at the end of the command.
tar -czvf logs.tar.gz error.log access.log system.log
How to Extract a Tarball
When you eventually need to open the tarball and get your files back out, you use the exact same command, but you swap the -c (create) flag for the -x (extract) flag.
The new flag combination is xzvf.
tar -xzvf website_backup.tar.gz
The utility will rip open the archive, uncompress the data, and dump the entire /var/www/html/ folder perfectly intact into your current directory.