Why pack files into one archive?
An archive is one file that holds many files and folders, with their names, folder structure and permissions. One file is easier to copy to a server, attach to an email, keep as a dated backup or download from a host. Copying a single 3 MB archive is also much faster than copying three hundred small files one by one, because each separate copy has its own overhead.
Most archives are compressed as well, so they take less space. Text such as HTML, CSS and menus shrinks a lot. Photos barely shrink, because JPEG and PNG are already compressed; don’t expect a folder of photos to get much smaller.
Two formats cover nearly everything you’ll meet. .tar.gz is the usual one on Linux servers and in software downloads. .zip is the one Windows and Macs open with a double-click.
How do you make a .tar.gz?
The command is tar with a few letters after it. For packing, the letters are almost always -czf: c to create, z to compress with gzip, and f to say the archive’s file name comes next. After the name, list what goes in. Naming a folder takes everything inside it.
Put f last in the group. Whatever follows f becomes the archive’s name, so tar -cfz photos.tar.gz photos on a real system makes an archive called z and then tries to pack a file called photos.tar.gz. The practice terminal is more forgiving about this order than real tar, so build the -czf habit now.
Add v for verbose (-czvf) and tar names each item as it goes in. Leave the z out and you get a plain .tar that isn’t compressed. Name the file to match what it is, because the name is the only clue other people get. The photos here are stand-ins with no real pixels, so this archive is tiny; a real one would be close to the 729K the photos take.
tar -czf photos.tar.gz photos
ls -lh
total 36
drwxr-xr-x 1 you you 4.0K Mar 22 09:15 menus
drwxr-xr-x 1 you you 4.0K Mar 22 09:11 photos
-rw-r--r-- 1 you you 516 Mar 22 09:19 photos.tar.gzHow do you look inside without unpacking?
Swap c for t, for table of contents: tar -tzf photos.tar.gz. It prints every path in the archive and changes nothing. Do this before unpacking an archive someone else made, and after making one yourself, to be sure the files you expected went in.
Read the first part of each path. Here everything starts with photos/, which means unpacking will create a photos folder. An archive whose paths have no folder at the front will scatter its files into whatever folder you’re in, and one with paths starting / or ../ is trying to write outside it. GNU tar strips those for you by default, but treat them as a warning about where the archive came from.
tar -tzf photos.tar.gz
photos/
photos/cinnamon-buns.jpg
photos/loaves.jpg
photos/shopfront.jpgHow do you unpack, and where do the files go?
Swap the letter for x, extract: tar -xzf photos.tar.gz. By default tar unpacks into the folder you’re in and replaces files with the same name without asking. Unpacking a backup of photos inside the bakery folder overwrites the current photos with the archived ones.
-C changes that. tar -xzf photos.tar.gz -C ~/restore-test unpacks into ~/restore-test. The folder must already exist, so make it with mkdir first; tar won’t create it and says “Cannot open” instead. Unpacking a test copy somewhere empty is the safe way to check a backup really restores.
On a Mac, tar is the BSD version and these letters work the same. Modern tar on both systems can detect gzip when extracting, so tar -xf often works without z, but keeping it costs nothing and works everywhere.
mkdir ~/restore-test
tar -xzf photos.tar.gz -C ~/restore-test
ls -R ~/restore-test

-C ~/restore-test builds the copy somewhere empty, so the real photos are never overwritten.How do zip and unzip work?
zip puts the archive name first too, then what goes in, and needs -r for folders: zip -r menus.zip menus. It prints a line for each item as it adds it. Without -r, zip adds the folder’s name and nothing inside it, and prints adding: menus/ (stored 0%) as the only hint.
unzip -l menus.zip lists the contents without unpacking. unzip menus.zip unpacks into the current folder, and unzip menus.zip -d somewhere unpacks into another folder, creating it if needed. Real unzip asks before replacing an existing file; the practice terminal unpacks without asking.
On a real system unzip -l prints a table with sizes and dates. The practice terminal lists only the paths, which is the part you need for checking.
zip -r menus.zip menus
adding: menus/ (deflated 64%)
adding: menus/allergens.txt (deflated 64%)
adding: menus/spring.txt (deflated 64%)
adding: menus/winter.txt (deflated 64%)
unzip -l menus.zipWhen should you use tar and when zip?
Use .tar.gz for anything going to or coming from a Linux server: site backups, deploys, logs. tar keeps file permissions and is installed on every server, while zip sometimes isn’t. It is also what you’ll download most software source in.
Use .zip when a person will open it on Windows or a Mac, such as the accountant getting the menus or a printer getting photos. Both systems open zip files without extra software. Windows 11 can also open .tar.gz in File Explorer, and Windows 10 has a tar command, though many people don’t know that.
Whichever you choose, add the date to backup names, like photos-2026-03-14.tar.gz. Names sort by date in ls, and you can tell at a glance which one to restore.
How do you check an archive is good?
List it with tar -tzf or unzip -l and read the paths. Then compare the size with ls -lh: on a real system, a backup of a folder full of photos that comes out at a few hundred bytes is missing them. The practice photos are stand-ins, so rely on the list here. For anything that matters, unpack a copy into an empty folder and open a few files.
A backup that has never been restored is a guess. The ten-second test unpack is what turns it into something you can rely on when the server fails.
What do AI tools get wrong with archives?
They often mix the letter order, as in tar -cfz, or use -xvf on a .zip file. tar doesn’t read zip archives on Linux; use unzip for those. Check which letter comes right before the archive name and what the file ending says.
They also suggest unpacking straight into a live folder, such as tar -xzf backup.tar.gz -C /var/www/html, without saying it will replace every file with the same name. List first, unpack a test copy, and only then unpack over real files. On Macs, AI answers sometimes add --exclude options in the wrong position; with BSD tar, put options before the file names.
Field notes
- An archive holds many files and folders inside a single file.
tar -czf name.tar.gz foldercreates a compressed archive.- Use
tto list the contents andxto unpack, with-Cto pick a folder. zip -rpacks a folder andunzipopens it again.
Easy to get wrong
The letter f takes the next word as the archive name. With f in the middle, real tar names the archive z.
Without -r zip adds the folder’s name and none of its files.
The folder given to -C must already exist. tar won’t make it.
A .zip is a different format. GNU tar can’t read it; unzip can.
Questions people ask
What’s the difference between .tar, .gz and .tar.gz?
A .tar bundles files together without compressing them. gzip compresses one file into .gz. A .tar.gz (also written .tgz) is a bundle that was then compressed, which is what tar -czf makes in one step.
Can I add one file to an existing .tar.gz?
Not directly. A compressed tar has to be rebuilt. Unpack it, add the file, and pack it again, or make a new archive. zip can add to an existing .zip by running it again with the same name.
Does an archive keep file permissions?
tar does, which is why it suits servers: a script that was executable stays executable. zip keeps them on Linux and Mac but Windows ignores them.
How big can an archive be?
Far bigger than you’ll need for a website. The practical limits are disk space and how long the copy takes, so archive just the folder you need.