Linux tar Command Examples

tar (tape archive) is the standard Unix tool for bundling files into a single archive. It is commonly combined with gzip or bzip2 compression for backups, deployments, and sharing file collections. Mastering tar flags is essential for any Linux workflow.

LinuxtarArchiveCLI
Create Archives
Create a tar archive
tar -cvf archive.tar files/
-c creates an archive, -v shows verbose output (filenames being added), and -f specifies the output filename. This produces an uncompressed .tar archive of the files/ directory.
Create a gzip-compressed archive
tar -czvf archive.tar.gz files/
Adding -z compresses with gzip, producing a .tar.gz (also called a tarball). This is the most common archive format on Linux — it offers a good balance of compression speed and ratio.
Create a bzip2-compressed archive
tar -cjvf archive.tar.bz2 files/
The -j flag uses bzip2 compression, which produces smaller files than gzip at the cost of more CPU time. Use .tar.bz2 when archive size matters more than compression speed.
Archive multiple directories
tar -czvf backup.tar.gz /etc /var/www /home/user
tar accepts multiple source paths. This creates a single archive containing the full /etc, /var/www, and /home/user directories. Useful for creating complete server configuration backups.
Extract Archives
Extract a gzip archive
tar -xzvf archive.tar.gz
-x extracts files, -z handles gzip, -v shows progress, and -f specifies the archive. Files are extracted into the current directory by default.
Extract to a specific directory
tar -xzvf archive.tar.gz -C /destination/
The -C flag sets the extraction destination directory. The target directory must already exist. This avoids cluttering your current working directory when extracting large archives.
Extract a single file from archive
tar -xzvf archive.tar.gz path/to/file.txt
Append the internal archive path to extract only one specific file. Use tar -tzvf archive.tar.gz first to list all files and find the exact internal path you need.
Inspect & Misc
List archive contents without extracting
tar -tzvf archive.tar.gz
-t lists the archive contents without extracting. This shows every file path, size, and timestamp inside the archive. Use this to verify an archive before extracting or to find the path of a specific file.
Extract and strip leading directory
tar -xzvf archive.tar.gz --strip-components=1
The --strip-components=1 flag removes the top-level directory from the extracted paths. Useful when an archive wraps everything in a version-named folder (e.g., myapp-1.0/) and you want the contents directly.
Add a file to an existing archive
tar -rvf archive.tar newfile.txt
The -r flag appends files to an existing uncompressed .tar archive. Note: you cannot append to a compressed archive (.tar.gz); you must extract, add, and recompress.

How to Use

  1. Remember the core flags: -c (create), -x (extract), -t (list), -f (filename).
  2. Add -z for gzip (.tar.gz) or -j for bzip2 (.tar.bz2) compression.
  3. Always add -v to see progress when working with large archives.
  4. Use -C /path/ to extract to a specific directory instead of the current one.
  5. Preview archive contents with -t before extracting to avoid surprises.

Frequently Asked Questions

What is the difference between .tar, .tar.gz, and .tar.bz2?

.tar is an uncompressed archive (just bundles files). .tar.gz adds gzip compression (faster, widely compatible). .tar.bz2 uses bzip2 (better compression ratio, slower). Most Linux software distributions use .tar.gz or the newer .tar.xz format.

How do I create a tar archive excluding certain files?

Use the --exclude flag: tar -czvf archive.tar.gz ./project --exclude="./project/node_modules" --exclude="./project/.git". You can use multiple --exclude flags and wildcards.

Can I use tar with pipes for network transfers?

Yes! Combine tar with SSH for efficient transfers: tar -czvf - ./files | ssh user@host 'tar -xzvf - -C /destination/'. The dash - uses stdout/stdin instead of a file, enabling streaming without a temporary archive file.

Related Tools