Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 1 · Basics · Lesson 09 · 40 minutes

Pack and unpack archives with tar

After this lesson you can
  • Pack a folder into an archive, list it and unpack it, also into another folder
  • Leave out folders, strip leading path parts and compress an archive
  • Check an archive before you extract it, and check a download with a checksum
Lab files of this lesson (8)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

A colleague wants the files of a project, and you have to send them as one file. Later another colleague sends you an archive from a server and says "please look at what is inside". You must pack your folder without the cache that nobody needs, and you must open the other archive without letting it write where it should not.

Recall

What does "find . -type f -mtime +30" list?

The regular files under the current folder whose content changed more than 30 days ago.

You delete the original name of a file that has a hard link and a symbolic link. Which link still works?

The hard link. Both names point to the same inode, so the data stays. The symbolic link holds the old path, and it is now broken.

The idea

An archive is one file that holds many files, with their names, folders, permissions and times. tar (tape archive) makes and reads archives. It does not make them smaller by itself. A compressor does that: gzip is common and fast, bzip2 is smaller and slower, and xz is smallest and slowest. You mark that step with one more letter.

Every tar command has one mode letter and the file name:

OptionMeaning
-cCreate an archive.
-tList the names in an archive.
-xExtract the files of an archive.
-f NAMEThe archive file. Give it last in a bundle of letters, as in -czf NAME.
-vVerbose: print each name as it works.
-z, -j, -JCompress with gzip, bzip2 or xz.
-C DIRChange to the folder DIR before the work.
tar -czf: three options in one word
tarthe program-ccreate an archive-zcompress it with gzip-f project.tar.gzthe archive fileprojectthe folder to packtarthe program-ccreate an archive-zcompress it with gzip-f project.tar.gzthe archive fileprojectthe folder to pack

An archive is data from outside. Look at it before you open it.

Pack, look, unpack
FolderArchiveListingNew foldertar -c packs the filestar -t shows every nametar -x unpacks, after you read itFolderArchiveListingNew folder123
  1. Folder → Archive: tar -c packs the files
  2. Archive → Listing: tar -t shows every name
  3. Listing → New folder: tar -x unpacks, after you read it

Worked example

1. Build a project folder

mkdir -p ~/krlab/linux-basics/tarlab && cd ~/krlab/linux-basics/tarlab
mkdir -p project/src project/docs project/cache
echo "Project readme" > project/readme.txt
printf '#!/bin/bash\necho main\n' > project/src/main.sh
printf '#!/bin/bash\necho util\n' > project/src/util.sh
echo "How to use it" > project/docs/guide.txt
echo "tmp one" > project/cache/tmp1.dat
echo "tmp two" > project/cache/tmp2.dat

2. Pack and list

-c creates, and -f project.tar names the file. The last word is the folder to pack. tar stores the folder name and everything below it. -t lists the names. The order of the names depends on the disk, so sort makes the list stable here. All names in this lesson are lower case, so sort gives the same order in every language setting.

tar -cf project.tar project
tar -tf project.tar | sort
project/
project/cache/
project/cache/tmp1.dat
project/cache/tmp2.dat
project/docs/
project/docs/guide.txt
project/readme.txt
project/src/
project/src/main.sh
project/src/util.sh

With -v the list also shows the owner, the size and the time of each member. Ask for one name.

tar -tvf project.tar project/readme.txt
-rw-rw-r-- you/you  15 2026-11-21 10:58 project/readme.txt
One member in a tar -tv list
-rw-rw-r--type and permissionsyou/youowner and group15size in bytes2026-11-21 10:58time of the last changeproject/readme.txtthe name in the archive-rw-rw-r--type and permissionsyou/youowner and group15size in bytes2026-11-21 10:58time of the last changeproject/readme.txtthe name in the archive

3. Compress

Add -z for gzip, or -J for xz. -j is bzip2, which was not installed on the build machine, so it is not tested here. The name of the file ends in .tar.gz or .tgz for gzip, .tar.xz for xz and .tar.bz2 for bzip2. The ending is only a habit: it does not change what tar does. When you read, tar finds the kind of compression by itself.

tar -czf project.tar.gz project
tar -cJf project.tar.xz project
tar -tf project.tar.gz | wc -l
tar -tf project.tar.xz | wc -l
10
10

4. Extract

-x extracts. Without -C, the files go into the folder where you are, so first make an empty folder and name it with -C. The members keep their folder project. To take one file, name it after the archive.

mkdir restore one
tar -xzf project.tar.gz -C restore
tar -xzf project.tar.gz -C one project/readme.txt
find restore one -type f | sort
one/project/readme.txt
restore/project/cache/tmp1.dat
restore/project/cache/tmp2.dat
restore/project/docs/guide.txt
restore/project/readme.txt
restore/project/src/main.sh
restore/project/src/util.sh

5. Leave a folder out

--exclude=PATTERN skips the names that fit the pattern. Put it before the folder name, because tar reads the options before the names to pack. Use it more than once to skip more than one thing.

tar -czf slim.tar.gz --exclude=cache project
tar -tf slim.tar.gz | sort
tar -czf slimmer.tar.gz --exclude=cache --exclude='*.sh' project
tar -tf slimmer.tar.gz | sort
project/
project/docs/
project/docs/guide.txt
project/readme.txt
project/src/
project/src/main.sh
project/src/util.sh
project/
project/docs/
project/docs/guide.txt
project/readme.txt
project/src/

The first list has no cache. The second also has no .sh file. Quote a pattern with *, so that the shell does not expand it.

6. Strip the leading folder

--strip-components=1 removes one leading part of each name while it extracts. Here the folder project is dropped, so that the files go straight into the target.

mkdir flat
tar -xzf project.tar.gz --strip-components=1 -C flat
find flat -type f | sort
flat/cache/tmp1.dat
flat/cache/tmp2.dat
flat/docs/guide.txt
flat/readme.txt
flat/src/main.sh
flat/src/util.sh

7. Look before you extract, and check the download

An archive can hold a name that leaves your target folder, or a link that points outside it. Make two such archives in the lab and read their listings. --transform changes the names while they are stored, here by putting ../ in front.

echo "note" > note.txt
tar -cf dots.tar --transform='s,^,../,' note.txt
tar -tf dots.tar
ln -s /etc/hostname pointer
tar -cf link.tar pointer
tar -tvf link.tar
tar: Removing leading `../' from member names
../note.txt
lrwxrwxrwx you/you   0 2026-11-21 10:59 pointer -> /etc/hostname

tar -cf makes the archive without a word. The warning comes from tar -tf, when it reads the list, and the name stays in the archive: ../note.txt.

The warning signs are a name that starts with /, a name that holds .., and a link (->) to a place that is not inside the folder. GNU tar here refuses to extract ../note.txt ("Member name contains '..'"), and removes a leading /. Other versions and other tools may not, so read the list first, and extract into an empty folder.

A checksum is a short fingerprint of a file. sha256sum prints it. If the sender gives you the fingerprint over another channel, you can prove that the download is the same file. -c checks a list of fingerprints.

sha256sum project.tar.gz > project.tar.gz.sha256
sha256sum -c project.tar.gz.sha256
echo "x" >> project.tar.gz
sha256sum -c project.tar.gz.sha256
project.tar.gz: OK
project.tar.gz: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match

The extra byte changed the fingerprint, so the check failed. The fingerprint of your own archive differs from the fingerprint of the same folder on another machine, because the archive holds times and owners.

Your turn

Run this block once. It builds the folder webapp with fixed content.

cd ~/krlab/linux-basics && rm -rf webapp webapp.tar.gz deploy
mkdir -p webapp/static webapp/logs webapp/tmp
echo "<h1>shop</h1>" > webapp/index.html
printf 'print("shop")\n' > webapp/app.py
echo "logo" > webapp/static/logo.txt
echo "body {}" > webapp/static/style.css
echo "GET /" > webapp/logs/access.log
echo "oops" > webapp/logs/error.log
echo "s1" > webapp/tmp/session1
echo "s2" > webapp/tmp/session2

There are no steps here. In ~/krlab/linux-basics:

  1. Pack webapp into webapp.tar.gz, and leave out the folders logs and tmp.
  2. List the archive and count its names.
  3. Make the folder deploy, and extract the archive into it, so that index.html is directly in deploy and not in deploy/webapp.
  4. Write the checksum of the archive into webapp.tar.gz.sha256, and check it.

Check yourself

What is wrong with "tar -czf slim.tgz project --exclude=cache"?

The option comes after the name to pack. GNU tar 1.35 ignores the option there, prints --exclude 'cache' has no effect and exits with an error. Put every option before the names: tar -czf slim.tgz --exclude=cache project.

You get "backup.tar.gz". How do you see what is inside without writing a file?

tar -tf backup.tar.gz lists the names. tar -tvf adds the owners, the sizes, the times and the targets of links.

Does a name that ends in .tar.gz prove that the file is a gzip archive?

No. The ending is only a habit. file backup.tar.gz reads the content and tells the truth, and tar -tf fails if the content is not an archive.

Why do you extract an archive from outside into an empty folder?

Then you see at once what it made, and it cannot replace a file that you care about. You can delete the whole folder when the content is bad.

Field notes

  • Read the list before you extract: names that start with / or hold .., links to other places, and files with the SUID bit are all warning signs. This is the weakness that security people call "path traversal" in archives. Do not extract as root.
  • Check a download with a checksum or, better, a signature. A checksum from the same site as the file only proves that the transfer worked. Lesson 6 of Part 3 shows signatures.
  • A normal user who extracts gets the permissions of the archive cut by the umask. With -p, tar keeps them as stored (a file stored as 666 stays 666). Only root can set the owner of the files back (not run here, it needs root).
  • To check an archive that was made by a script, compare its list with the list that you expect: tar -tf a.tar.gz | sort > list.txt.
  • Archives hide secrets as well as files. Before you send one, list it, and look for keys, .env files and shell histories.