ShareYourGroupShareYourGroupCommands

Linux and Mac commands

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

Linux or Mac terminal
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.gz

The letters after the dash are separate options.

LetterMeaning
ccreate an archive
xextract from an archive
tlist the contents
zcompress with gzip
fthe next word is the archive's file name
vverbose: 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.

Linux or Mac terminal
asha@pc:~/demo$ tar -czvf backup.tar.gz notes.txt marks.csv photos
notes.txt
marks.csv
photos/
photos/beach.jpg
photos/hills.jpg

Looking inside without unpacking

Always list an archive before extracting one you did not make.

Linux or Mac terminal
asha@pc:~/demo$ tar -tzf backup.tar.gz
notes.txt
marks.csv
photos/
photos/beach.jpg
photos/hills.jpg

Extracting

Linux or Mac terminal
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?

EndingCompressiontar letter
.tarnone, just bundled(none)
.tar.gz or .tgzgzip: quick, the usual choicez
.tar.bz2bzip2: smaller, slowerj
.tar.xzxz: smallest, slowestJ

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:

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.

Linux or Mac terminal not run here
zip -r photos.zip photos
unzip -l photos.zip
unzip photos.zip -d restore

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.

Linux or Mac terminal not run here
tar -czf "documents-$(date +%Y-%m-%d).tar.gz" ~/Documents

The 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.

Linux or Mac terminal not run here
tar -czf project.tar.gz --exclude="*.log" --exclude="node_modules" project

gzip 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

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.