tar Command in Linux: Compress and Extract .tar.gz Files

Aidin

Cardboard box illustration for a guide to the tar command in Linux

Hi everyone. Sooner or later you need to pack a folder into one file, to back it up, move it to another machine, or attach it somewhere. On Linux the standard tool for that is the tar command, and the file it produces with compression is the familiar .tar.gz. In this guide I use tar on Ubuntu 26.04 to back up the three lab folders from this series, look inside the archive, restore it to a different folder, and check that nothing changed.

You will also see two things that confuse people the first time: the message Removing leading / from member names, and the fact that a restored file can come back with a different owner. Both happened in this lab.

The folders I back up here are the ones created in the earlier guides, and the previous article on the find command listed them by name.

Distro  : Ubuntu 26.04.1 LTS (Desktop)
Backup  : permissions-lab, chmod-lab, chown-lab
Archive : labs-backup.tar.gz

Creating a .tar.gz Archive

Pack several folders into one file

Creating labs-backup.tar.gz from three folders with tar -czf
cd ~
tar -czf labs-backup.tar.gz permissions-lab chmod-lab chown-lab
ls -lh labs-backup.tar.gz

The options read as one word: c creates an archive, z compresses it with gzip, and f says the next word is the archive file name (number 1). After f you list everything that should go in, here three folders. The result is a single file of 616 bytes (number 2). The lab files are tiny, so the archive is too.

Note: The order matters for f. It must be the last letter of the group, directly before the file name. tar -cfz backup.tar.gz would create an archive literally called z.

Looking Inside Without Extracting

List the contents with -t

Listing the contents of a tar.gz archive with tar -tzf without extracting
tar -tzf labs-backup.tar.gz | head -n 12

Replacing c with t lists what is in the archive without unpacking anything (number 1). Every path is relative, starting with the folder names I gave tar. It is worth running this before every extraction, so you know exactly which files will appear and where.

The Leading Slash Message

Archive a folder by its full path

The Removing leading / from member names message when archiving /etc/apt
tar -czf apt-config.tar.gz /etc/apt
tar -tzf apt-config.tar.gz | head -n 4

Giving tar an absolute path makes it print tar: Removing leading / from member names (number 1). It is not an error. tar strips the first slash on purpose and stores the files as etc/apt/ instead of /etc/apt/ (number 2). That way, extracting the archive later creates an etc folder wherever you are, instead of silently overwriting the real /etc on the machine.

Extracting and Checking the Result

Restore into a different folder with -C

Extracting into a restore folder with -C and confirming it is identical with diff
mkdir -p restore
tar -xzf labs-backup.tar.gz -C restore
ls restore
diff -r chmod-lab restore/chmod-lab && echo identical

x extracts, and -C tells tar which folder to extract into (number 1). Without -C, the files land in the current folder and would mix with the originals. diff -r compares the original chmod-lab with the restored copy file by file. It prints nothing when they match, so the && echo identical part only runs if everything is the same, and it did (number 2).

Pull out a single file

Extracting a single file whose owner changes to the extracting user
tar -xzf labs-backup.tar.gz -C /tmp chown-lab/notes.txt
ls -l /tmp/chown-lab

Adding a path from the archive listing after the options extracts only that file (number 1). Look at the owner of the restored notes.txt: mamangdigital (number 2). In the original folder that file belonged to alice. tar saves the owner, but a normal user cannot create files owned by someone else, so they come back owned by whoever extracts them.

My experience: I only noticed the owner change because I had set it up deliberately in the chown lab. On a real backup of a shared folder, this is the reason to extract with sudo, which tells tar to restore the original owners as well.

gzip or xz

Compare the two compressions

Comparing the size of tar.gz and tar.xz archives
tar -cJf labs-backup.tar.xz permissions-lab chmod-lab chown-lab
ls -l labs-backup.tar.*

A capital J switches the compression to xz. On these tiny files the difference is only 616 against 604 bytes (number 1). On larger folders with lots of text, xz usually saves noticeably more space but takes longer to create. gzip is still the safer default when you are sending a file to someone else, because every system can open it.

Leaving Files Out

Skip files with –exclude

Excluding .log files from the archive with --exclude
tar -czf chown-nologs.tar.gz --exclude='*.log' chown-lab
tar -tzf chown-nologs.tar.gz

–exclude takes a pattern, here every file ending in .log (number 1). The listing confirms that the logs folder is still there but empty, and only notes.txt was packed (number 2). This is handy for leaving out caches, logs, or large build folders from a backup.

Common Problems and How to Fix Them

tar: Removing leading / from member names

Not an error. tar stores absolute paths as relative ones on purpose. Extract with -C into the folder where you want the files to appear.

gzip: stdin: not in gzip format

The file is not actually gzip compressed, for example a plain .tar or an .xz with the wrong name. Check it with the file command, then use the matching option: no z for .tar, J for .xz.

Files come back with the wrong owner

A normal user cannot restore other people’s ownership. Extract with sudo when the owners matter.

Extracted files mixed with existing ones

Extracting without -C writes into the current folder. Always list with -t first, and extract into an empty folder with -C.

That covers tar for everyday backups: create with -czf, look inside with -t, extract with -x and -C, take out single files, choose between gzip and xz, and exclude what you do not need. Next in the series is zip and unzip, which matter when the archive has to open on Windows too. If tar shows you a message I did not cover, paste it in the comments.

Leave a Comment