tar, zip and unzip: archives
An archive puts many files and folders into one file, usually compressed, for sending or for safe keeping. On Linux and the Mac, two formats cover nearly every case: .tar.gz, the native one, and .zip, which every system can open.
tar: making an archive
asha@pc:~/demo$ tar -czf photos.tar.gz photos
asha@pc:~/demo$ ls -l photos.tar.gz
-rw-r--r-- 1 asha asha 218 Nov 12 08:44 photos.tar.gzThe letters after the dash are separate options.
| Letter | Meaning |
|---|---|
c | create an archive |
x | extract from an archive |
t | list the contents |
z | compress with gzip |
f | the next word is the archive's file name |
v | verbose: name each file as it is handled |
So -czf reads "create, gzip, file". The archive name comes straight after f, and then the things to put in it. The originals are left untouched.
Several files and folders can go in one archive.
asha@pc:~/demo$ tar -czvf backup.tar.gz notes.txt marks.csv photos
notes.txt
marks.csv
photos/
photos/beach.jpg
photos/hills.jpgLooking inside without unpacking
Always list an archive before extracting one you did not make.
asha@pc:~/demo$ tar -tzf backup.tar.gz
notes.txt
marks.csv
photos/
photos/beach.jpg
photos/hills.jpgExtracting
asha@pc:~/demo$ mkdir restore
asha@pc:~/demo$ tar -xzf backup.tar.gz -C restore
asha@pc:~/demo$ ls -R restore
restore:
marks.csv
notes.txt
photos
restore/photos:
beach.jpg
hills.jpg-C names the folder to unpack into. Without it, files are unpacked into the current folder.
To take out a single item, name it after the archive: tar -xzf backup.tar.gz notes.txt.
Danger. Extracting overwrites existing files of the same name without asking. An archive whose paths do not begin with a single folder scatters its files into wherever you are. List the contents first, and unpack into a new, empty folder with -C.
Which compression?
| Ending | Compression | tar letter |
|---|---|---|
.tar | none, just bundled | (none) |
.tar.gz or .tgz | gzip: quick, the usual choice | z |
.tar.bz2 | bzip2: smaller, slower | j |
.tar.xz | xz: smallest, slowest | J |
Modern tar works out the compression by itself when extracting, so tar -xf archive.tar.xz is enough for any of them.
Remembering the letters
Three patterns cover almost everything:
- Create:
tar -czf name.tar.gz things - List:
tar -tzf name.tar.gz - Extract:
tar -xzf name.tar.gz
Only the first letter changes: c, t, x.
zip and unzip
Use .zip when the archive is going to someone on Windows, or when you are not sure what they use.
zip -r photos.zip photos
unzip -l photos.zip
unzip photos.zip -d restorezip -rincludes the contents of folders. Without-r, a folder is added empty.unzip -llists the contents.unzip -dnames the folder to unpack into.
zip -e asks for a password and encrypts the archive. The standard zip encryption is weak, so do not depend on it for anything sensitive.
zip and unzip are present on the Mac and on most Linux systems. Where they are missing, the package manager installs them.
A dated backup
Putting the date in the name keeps backups apart and in order.
tar -czf "documents-$(date +%Y-%m-%d).tar.gz" ~/DocumentsThe part in $( ) runs the date command and inserts its result, giving a name such as documents-2026-11-12.tar.gz.
Leaving things out
--exclude skips files that match a pattern. Put it before the folder name.
tar -czf project.tar.gz --exclude="*.log" --exclude="node_modules" projectgzip on its own
gzip file compresses a single file and replaces it with file.gz. gunzip file.gz reverses it. gzip -k keeps the original. zcat and zless read a compressed text file without unpacking it.
Checking an archive
A backup that cannot be read is no backup. tar -tzf archive.tar.gz > /dev/null reads the whole archive and reports any damage. For archives that matter, list them after making them, and now and then try restoring one into a spare folder.
Common mistakes
- Putting
fanywhere but last in the group of letters. The word afterfis taken as the file name, so-cfzmakes an archive calledz. - Forgetting
-rwithzip. - Extracting into the wrong folder.
- Making the archive inside the folder being archived, so that it tries to include itself.
On Windows 10 and 11, tar is built in and works the same way in the Command Prompt.
Examples shown with output were run on 12 November 2026 in a practice folder: Windows commands in cmd.exe on Windows 11, Linux commands with the GNU tools of Git Bash. The folder path and user name were replaced with made-up ones. Commands marked "not run here" are shown as you would type them.
This is an independent learning site. It is not connected with Microsoft, Apple or any Linux distribution. Windows, macOS and other names belong to their owners. Try commands first in a practice folder, never on files you cannot afford to lose.