The tar command archives and extracts files on Ubuntu 26.04 LTS and Ubuntu 26.10. Each example below includes a short explanation so you can compress backups and unpack releases without guesswork.
Before You Start
Ubuntu version
GNU tar ships by default on Ubuntu 26.04 and 26.10.
Terminal
Open with Ctrl + Alt + T. Create a practice folder before packing real home data.
Compression
Use -z for gzip, -j for bzip2 and -J for xz. gzip is the everyday default.
KEY TAKEAWAYS
01Archive then compress
tar gathers files; flags such as -z add gzip in one step.
02Extract safely
List contents with -t before extracting into the wrong directory.
03Cheat sheet included
Download the free tar cheat sheet in PDF or ebook formats.
Common form: tar [options] archive paths. Always pass -f before the archive name.
tar –help | head
tar --help | head
Common tar examples
1. Create an archive
Pack a folder into an uncompressed .tar file.
tar -cvf archive.tar Documents/
tar -cvf archive.tar Documents/
2. Extract an archive
Unpack into the current directory.
tar -xvf archive.tar
tar -xvf archive.tar
3. Create a gzip archive
Most common backup format on Ubuntu.
tar -czvf archive.tar.gz Documents/
tar -czvf archive.tar.gz Documents/
Sample output: tar -czvf creating a gzip archive (generated for this guide).
4. Extract a gzip archive
Unpack a .tar.gz or .tgz file.
tar -xzvf archive.tar.gz
tar -xzvf archive.tar.gz
5. Create a bzip2 archive
Better ratios than gzip on some text trees.
tar -cjvf archive.tar.bz2 Documents/
tar -cjvf archive.tar.bz2 Documents/
6. Extract a bzip2 archive
Unpack .tar.bz2 files.
tar -xjvf archive.tar.bz2
tar -xjvf archive.tar.bz2
7. Create an xz archive
Strong compression for release tarballs.
tar -cJvf archive.tar.xz Documents/
tar -cJvf archive.tar.xz Documents/
8. Extract an xz archive
Unpack .tar.xz files.
tar -xJvf archive.tar.xz
tar -xJvf archive.tar.xz
9. Extract to a directory
Send files into a chosen folder with -C.
mkdir -p /tmp/restore && tar -xzvf archive.tar.gz -C /tmp/re
mkdir -p /tmp/restore && tar -xzvf archive.tar.gz -C /tmp/restore
10. List archive contents
Inspect before extracting.
tar -tzvf archive.tar.gz | head
tar -tzvf archive.tar.gz | head
11. Exclude patterns
Skip cache directories while backing up.
tar -czvf project.tar.gz –exclude=node_modules –exclude=.g
tar -czvf project.tar.gz --exclude=node_modules --exclude=.git myapp/
12. Backup home excluding cache
Practical Ubuntu laptop backup starter.
tar -czvf home-backup.tar.gz –exclude=.cache –exclude=.loc
tar -czvf home-backup.tar.gz --exclude=.cache --exclude=.local/share/Trash -C ~ .
Useful tar options
Option
Meaning
-c
Create archive
-x
Extract archive
-t
List contents
-v
Verbose
-f
Archive file name
-z
gzip
-j
bzip2
-J
xz
-C dir
Change to directory
–exclude
Skip matching paths
Tips and best practices
Prefer relative paths when creating archives so extraction does not try to write under / accidentally. Verify with tar -t before a large restore. For ongoing sync instead of point-in-time archives, use rsync from the advanced commands guide.