🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

tar Command

Beginner Compression & Archives man(1)

Archive files using tape archive format

👁 11 views 📅 Updated: Mar 15, 2026
SYNTAX
tar [OPTION]... [FILE]...

What Does tar Do?

tar (tape archive) creates and extracts archive files. While tar itself does not compress, it is almost always combined with compression: tar.gz (gzip), tar.bz2 (bzip2), tar.xz (xz). tar is the standard archive format for Linux backup and distribution.

tar preserves file permissions, ownership, timestamps, symbolic links, and directory structure. It is the preferred format for source code distribution, system backups, and data transfer.

The most common commands are tar czf (create gzipped archive) and tar xzf (extract gzipped archive). Modern tar auto-detects compression format with tar xf.

Options & Flags

OptionDescriptionExample
-c Create archive tar cf archive.tar files/
-x Extract archive tar xf archive.tar
-z Compress/decompress with gzip tar czf archive.tar.gz dir/
-j Compress with bzip2 tar cjf archive.tar.bz2 dir/
-J Compress with xz tar cJf archive.tar.xz dir/
-f Specify archive filename tar cf backup.tar /data
-v Verbose — list files processed tar czvf archive.tar.gz dir/
-t List archive contents tar tf archive.tar.gz
-C Change to directory before operation tar xf archive.tar.gz -C /opt/
--exclude Exclude files matching pattern tar czf backup.tar.gz --exclude='*.log' /var/www/

Practical Examples

#1 Create gzipped archive

Creates a compressed archive of the home directory.
$ tar czf backup.tar.gz /home/user/

#2 Extract archive

Extracts the archive. Modern tar auto-detects compression.
$ tar xf archive.tar.gz

#3 Extract to specific directory

Extracts the archive into /opt/ directory.
$ tar xf release.tar.gz -C /opt/

#4 List contents

Lists all files in the archive without extracting.
$ tar tf archive.tar.gz
Output: dir/file1.txt\ndir/file2.txt

#5 Exclude files

Creates archive excluding unnecessary directories and logs.
$ tar czf deploy.tar.gz --exclude='node_modules' --exclude='.git' --exclude='*.log' ./project/

#6 Create xz archive (best compression)

Creates an xz-compressed archive. Slower but smallest file size.
$ tar cJf archive.tar.xz /data/

#7 Incremental backup

Archives only files modified in the last day.
$ tar czf "backup_$(date +%Y%m%d).tar.gz" --newer-mtime="1 day ago" /var/www/

#8 Extract single file

Extracts only one file from the archive.
$ tar xf archive.tar.gz path/to/specific/file.txt

Tips & Best Practices

Remember: c-create, x-extract, t-list: tar czf = create gzip file. tar xf = extract file. tar tf = list (tell) file. f always comes last before filename.
Always list before extract: Use tar tf archive first to check contents. Some archives extract files without a top-level directory, cluttering your current directory.
Compression comparison: Speed: gzip > bzip2 > xz. Compression ratio: xz > bzip2 > gzip. gzip is the default for most uses. xz for distribution.

Frequently Asked Questions

How do I create a tar.gz file?
tar czf archive.tar.gz directory/. c=create, z=gzip, f=filename.
How do I extract a tar.gz file?
tar xf archive.tar.gz. Modern tar auto-detects compression, so just tar xf works for .tar.gz, .tar.bz2, and .tar.xz.
How do I see what is in an archive without extracting?
tar tf archive.tar.gz lists all files. Add v for details: tar tvf archive.tar.gz.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →