tar
Archive files into a single .tar file, optionally compressed
By CMD Script Team · 4 min read · Last updated
tar [OPTIONS] -f ARCHIVE [FILE...]Options
| Flag | Description |
|---|---|
-c | Create a new archive |
-x | Extract files from an archive |
-f | Specify the archive filename (almost always required, e.g. -f archive.tar) |
-z | Compress/decompress using gzip (produces/reads a .tar.gz) |
-j | Compress/decompress using bzip2 (produces/reads a .tar.bz2) |
-v | Verbose mode, list each file as it's processed |
-t | List the contents of an archive without extracting it |
-C | Change to a directory before extracting/creating, e.g. -C /tmp |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (BSD tar, mostly compatible)
What it does
tar ("tape archive," a name that survives from the days of literal magnetic tape
backups) bundles multiple files and directories into a single archive file, preserving
directory structure, permissions, and symlinks. On its own it does not compress
anything — that's what the -z (gzip), -j (bzip2), or -J (xz) flags add on top,
turning a plain .tar into a .tar.gz, .tar.bz2, or .tar.xz. It's the standard way
to package source code releases, backups, and software distributions on Linux.
Beginner examples
tar -cvf archive.tar file1 file2— create an uncompressed archive from two filestar -xvf archive.tar— extract an uncompressed archive into the current directorytar -czvf archive.tar.gz ./project— create a gzip-compressed archive of a directorytar -xzvf archive.tar.gz— extract a gzip-compressed archive
tar -czvf project-backup.tar.gz ./project
Advanced examples
- Extract into a specific directory instead of the current one:
tar -xzvf archive.tar.gz -C /opt/app - List an archive's contents before extracting, to check what's inside:
tar -tzvf archive.tar.gz - Create a bzip2-compressed archive for tighter compression:
tar -cjvf archive.tar.bz2 ./project - Extract only a specific file or subdirectory from a large archive:
tar -xzvf archive.tar.gz path/inside/archive/file.txt - Exclude a pattern while creating an archive:
tar --exclude='*.log' -czvf archive.tar.gz ./app
tar -czvf release-1.2.0.tar.gz --exclude='.git' --exclude='node_modules' ./app
Common mistakes
- Forgetting to match the compression flag to the archive's actual compression — using
plain
-xvfon a.tar.gzproduces a "not a tar archive" style error; you need-zfor gzip,-jfor bzip2,-Jfor xz. - Not checking an archive's contents with
-tbefore extracting, then being surprised when it dumps dozens of files into the current directory instead of a single subfolder ("tarbomb"). - Getting the option order confused with
-f— the archive filename must immediately follow-f, sotar -cvf backup.tar.gz ./projectworks but putting other flags between-fand the filename does not. - Assuming tar preserves absolute paths safely — archives created with leading
/in paths can, on extraction, overwrite files outside the intended directory if you're not careful (GNU tar strips leading slashes by default with a warning).
Tips
- Use
tar -tzvf archive.tar.gz | lessto page through a large archive's file listing before deciding whether/where to extract it. - Combine
--excludepatterns when creating archives to skip.git,node_modules, build output, or log files that shouldn't be part of a release package. tar -czvf - ./project | ssh user@host 'cat > project.tar.gz'streams an archive directly to a remote host without creating a local intermediate file.
Best practices
- Always verify an unfamiliar archive's contents with
tar -tvfbefore extracting it, especially archives from untrusted sources, to avoid a "tarbomb" scattering files everywhere. - Use
-C directoryto extract into an explicit target directory rather thancd-ing manually, reducing the chance of extracting into the wrong place. - Prefer gzip (
-z) for a good speed/size balance in routine backups, and reserve bzip2/xz (-j/-J) for cases where minimizing size matters more than speed.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Packaging a software release or source tarball for distribution (
myapp-1.0.0.tar.gz). - Creating compressed backups of configuration directories or databases before making risky changes.
- Bundling a directory tree for transfer to a remote server as a single file instead of copying many individual files.
Common interview questions
- What do the flags in
tar -xzvf file.tar.gzmean?xextract,zgzip decompression,vverbose,fread from the named file (file.tar.gz). - Does tar compress files by itself? No,
taronly bundles files into an archive; compression comes from an added flag (-zfor gzip,-jfor bzip2,-Jfor xz) that pipes the archive through a compressor. - How would you inspect a suspicious or unfamiliar tar archive safely before
extracting it? Run
tar -tvf archive.tar(or with-z/-jas appropriate) to list its contents first, checking for unexpected absolute paths or a lack of a single top-level directory before extracting.
Frequently Asked Questions
What do the letters c, x, f, z, j, v, t actually mean when combined like tar -xzvf?
c=create, x=extract, f=file (the archive name follows), z=gzip compression, j=bzip2 compression, v=verbose listing, t=list contents. tar -xzvf archive.tar.gz means: extract, using gzip, verbosely, from the named file.
Do I need the dash before the tar flags?
No — tar supports the classic combined-letters style without a leading dash (tar xzvf file.tar.gz) as well as the more standard tar -xzvf file.tar.gz; both work identically on GNU tar.
How do I see what's inside a tar file without extracting it?
tar -tzvf archive.tar.gz lists the contents (add z for gzip, j for bzip2, matching whatever compression the archive uses) without writing anything to disk.
What's the difference between tar.gz and tar.bz2?
Both are a tar archive with a compression layer applied; gzip (.tar.gz, use -z) is faster with slightly larger output, bzip2 (.tar.bz2, use -j) compresses tighter but is slower. xz (.tar.xz, use -J) typically compresses best but is slowest.
Cheat sheet
Download a quick-reference cheat sheet for tar.