Linux commands: tar
By Flavio
Published · Updated
Tagged: CLI
Learn the Linux tar command: create an archive with tar -cf, make a .tar.gz with -czf, extract with tar -xf, list contents, extract one file, exclude folders and strip the top directory.
The tar command is used to create an archive, grouping multiple files in a single file.
Its name comes from the past and means tape archive. Back when archives were stored on tapes.
This command creates an archive named archive.tar with the content of file1 and file2:
tar -cf archive.tar file1 file2
The
coption stands for create. Thefoption sets the archive file name: the file tar writes to, or reads from when you extract.
You can pass folders too. tar adds everything inside them:
tar -cf project.tar project
To extract files from an archive in the current folder, use:
tar -xf archive.tar
the
xoption stands for extract
and to extract them to a specific directory, use:
tar -xf archive.tar -C directory
The directory must already exist. Create it first with mkdir directory.
You can also just list the files contained in an archive with t:

It’s a good habit to run tar -tf on an archive you downloaded before you extract it. You see if everything is inside a folder, or if it will spill dozens of files into your current directory.
Add v (verbose) to see each file as tar works on it, or to get a long listing with sizes and permissions:
tar -tvf archive.tar
Compressed archives: .tar.gz, .tar.bz2, .tar.xz
tar is often used to create a compressed archive, gzipping the archive.
This is done using the z option:
tar -czf archive.tar.gz file1 file2
This is just like creating a tar archive, and then running gzip on it. You’ll also see the .tgz extension: it’s the same thing.
Other compression formats have their own letter:
| Option | Format | Usual extension |
|---|---|---|
z | gzip | .tar.gz, .tgz |
j | bzip2 | .tar.bz2 |
J | xz | .tar.xz |
--zstd | zstd | .tar.zst |
Or let tar pick the format from the file name with a (auto-compress):
tar -caf archive.tar.xz project
To unarchive a gzipped archive, you can use gunzip, or gzip -d, and then unarchive it, but tar -xf will recognize it’s a gzipped archive, and do it for you:
tar -xf archive.tar.gz
The same works for .tar.bz2, .tar.xz and the other formats. You only need -z and friends to create an archive.
The exception is reading from a pipe with GNU tar, the one on Linux. There it can’t detect the format, so you tell it:
curl -L https://example.com/release.tar.gz | tar -xzf -
The - after f means “read the archive from standard input”.
Extract just one file
Pass the path as it appears in tar -tf:
tar -xf project.tar.gz project/src/a.txt
tar recreates the folders too, so you get project/src/a.txt, not a.txt.
Remove the top folder when extracting
Most downloaded archives put everything inside a folder like project-1.2.0/. To extract the content without that folder, use --strip-components:
mkdir app
tar -xf project-1.2.0.tar.gz -C app --strip-components=1
The number is how many leading folders to drop from each path.
Exclude files and folders
Use --exclude when you create the archive. For example, to archive a JavaScript project without the huge node_modules folder and the Git history:
tar -czf project.tar.gz --exclude=node_modules --exclude=.git project
Common pitfalls
f must come last in a group of options. The archive name is the word right after f. tar -cvf archive.tar files works. tar -cfv archive.tar files creates an archive named v, and then tries to add a file called archive.tar to it.
Extracting overwrites files with the same name. If you extract into a folder where config.json already exists, tar replaces it. Use -k to keep the files that already exist.
Absolute paths are made relative. If you run tar -cf backup.tar /etc/hosts, tar prints Removing leading '/' from member names and stores etc/hosts. When you extract, you get ./etc/hosts in the current folder, not a file in /etc. That’s a safety feature, not an error.
The dash is optional. You’ll see commands like tar czf archive.tar.gz files with no -. That’s the old style, and it does the same thing.
tar on macOS and Linux
Linux uses GNU tar. macOS ships bsdtar, from the libarchive project. All the commands in this post work on both.
The tar command works on Linux, macOS, WSL, and anywhere you have a UNIX environment.
Want me to talk about your product? You can sponsor this site.
Related posts about cli: