tar Command Guide for Packing, Compressing, and Unpacking Archives
tar is the Linux workhorse for bundling many files into one archive and (optionally) compressing it — almost every .tar.gz you ever unpack will go through this command.
What is tar, and Why Use It?
tar stands for tape archive — it was written in the 1980s for writing backups to tape drives, and the name stuck. Today it does the same job on disk: it collects many files and directories, records their permissions, ownership, and directory structure, and writes the whole thing into a single file. By itself that file is just an archive; tar can also pipe the result through a compressor (gzip, xz, bzip2) so the archive shrinks on the way out.
Why Learn tar?
- It is the default Linux archive format — software you download from source, distro packages, and container images all arrive as
.taror.tar.gz. - One command covers the whole job — create, list, extract, add, remove:
tardoes all of it with a small set of flags. - It preserves metadata — permissions, symlinks, and empty directories survive the round trip, which
cp -rdoes not reliably do for every case. - It drives the compressors —
tar -z(gzip),-J(xz),-j(bzip2) call the tools in this section for you, so you rarely run them separately.
If you learn one archive command, learn
tar. The three-letter patterntar -czf(create + gzip + file) appears in more tutorials, READMEs, and install scripts than anything else on this page.
1. Create a Compressed Archive (The Everyday Form)
The command you will type most often. The flags are read left to right:
tar -czf backup.tar.gz myproject/
-c— create a new archive.-z— filter it through zip/gzip compression (.gz).-f backup.tar.gz— file: the name of the archive to write. The filename comes after the flag.myproject/— what to pack: a directory, a file, or several of them.
A plain archive with no compression (bigger file, instant to create):
tar -cf backup.tar myproject/
Compression options at a glance:
| Flag | Compressor | Result | Typical ratio |
|---|---|---|---|
| (none) | — | .tar | none — plain bundle |
-z | gzip | .tar.gz | ~3–5× smaller; fast |
-J | xz | .tar.xz | smallest; slower |
-j | bzip2 | .tar.bz2 | between gzip and xz; older |
Check the result:
ls -lh backup.tar.gz
2. Extract an Archive
Reverse of create — note the flag change from c (create) to x (extract):
tar -xzf backup.tar.gz
This unpacks everything into the current directory. To choose where it lands, add -C (change directory):
tar -xzf backup.tar.gz -C ~/restored/
The directory must already exist — tar will not create it for you.
The flag order never changes: x for extract, z/J/j for the compressor, f for the filename.
3. List the Contents Without Extracting
Sometimes you just want to peek inside before unpacking:
tar -tzf backup.tar.gz
-t— tlist (list the table of contents).
Output is one path per line:
myproject/
myproject/README.md
myproject/src/
myproject/src/main.c
Pipe it through grep to find one file across an archive you did not unpack:
tar -tzf backup.tar.gz | grep main
4. See What Is Inside With Details
Add -v (verbose) to list with sizes, dates, and permissions — or to watch the work being done while creating:
tar -tzvf backup.tar.gz
tar -czvf backup.tar.gz myproject/
Verbose output while creating looks like this:
myproject/
myproject/README.md
myproject/src/
myproject/src/main.c
5. Extract Only Some Files
You do not have to unpack everything. Name the paths you want:
tar -xzf backup.tar.gz myproject/src/main.c
Or exclude paths while extracting:
tar -xzf backup.tar.gz --exclude='*.log'
Excluding at creation time works the same way:
tar -czf backup.tar.gz --exclude='.git' --exclude='node_modules' myproject/
That last one is a classic: packing a project directory without the .git history or dependency folders makes tarballs dramatically smaller.
6. Add or Remove Files in an Existing Archive
Archives are not immutable — you can update them:
# Append a new file to an existing (uncompressed or gzip) archive
tar -rf backup.tar newfile.txt
# Remove a path from an archive
tar --delete -f backup.tar myproject/old.txt
Caution: with
-r(append) or--delete, the archive must be plain (.tar) — you cannot append to a.tar.gz. If you are compressing, rebuild the archive from scratch instead; it is one command and always correct.
7. Preserve What Matters (Permissions and Ownership)
tar records file metadata by default. When extracting as a normal user you may want it preserved explicitly:
sudo tar -xpvzf backup.tar.gz -C /opt/
-p— preserve permissions (extract as the recorded owner).-v— verbose, so you can see what is happening.
Running with sudo when extracting to system locations is normal — this is how many software packages install themselves:
# A common real-world pattern: unpack a source release into /opt
sudo tar -xzf mysoftware-1.0.tar.gz -C /opt/
Common Pitfalls
- Putting the filename before the flags —
tar backup.tar.gz -czffails;tar -czf backup.tar.gz myproject/works. The archive name belongs to-fand comes right after it. - Forgetting
-f— without it,tarwrites to standard output (raw bytes) instead of a file, and you get a wall of garbage in the terminal. - Extracting into the wrong place —
tarunpacks into the current directory unless you give-C. Runpwdfirst if you are not sure where you are. - Mismatched compressor flag —
-zon a.tar.xzfails with “not in gzip format”. Match the flag to the extension:-z↔.gz,-J↔.xz,-j↔.bz2. - Extracting an archive whose paths start with
/— some archives contain absolute paths and would overwrite system files. List first with-t, and extract into a scratch directory if you are unsure. - Expecting
-rto work on compressed archives — appending only works on plain.tarfiles; rebuild instead.
Related Pages
| Page | Relationship |
|---|---|
| gzip | The compressor -z calls; also compresses single files on its own |
| zip | The cross-platform alternative archive format |
| xz | The -J compressor; best ratio for .tar.xz |
| bzip2 | The -j compressor; older .tar.bz2 files |
tar Cheatsheet
| Command | What it does |
|---|---|
tar -czf backup.tar.gz dir/ | Create a gzip-compressed archive |
tar -cf backup.tar dir/ | Create a plain (uncompressed) archive |
tar -xzf backup.tar.gz | Extract a .tar.gz into the current directory |
tar -xzf backup.tar.gz -C /path/ | Extract into a specific directory |
tar -tzf backup.tar.gz | List the contents without extracting |
tar -tzvf backup.tar.gz | List with sizes, dates, and permissions |
tar -czvf archive.tar.gz dir/ | Create with verbose progress output |
tar -xzf archive.tar.gz --exclude='*.log' | Extract everything except matching files |
tar --delete -f archive.tar path | Remove one path from an archive |
tar -rf archive.tar newfile | Append a file to a plain archive |
ls -lh archive.tar.gz | Check the archive size after creation |