tar

file managementLinux/Unix
The tar command is one of the most frequently used commands in Linux/Unix-like operating systems. tar Tape Archive: create, extract, and manipulate archive files

Quick Reference

Command Name:

tar

Category:

file management

Platform:

Linux/Unix

Basic Usage:

tar -czvf archive.tar.gz directory/

Common Use Cases

    Syntax

    tar [options] [archive-file] [file or directory to be archived]

    Options

    Option Description
    -c, --create Create a new archive
    -x, --extract, --get Extract files from an archive
    -t, --list List the contents of an archive
    -f, --file=ARCHIVE Use archive file or device ARCHIVE
    -v, --verbose Verbosely list files processed
    -z, --gzip, --gunzip Filter the archive through gzip
    -j, --bzip2 Filter the archive through bzip2
    -J, --xz Filter the archive through xz
    --zstd Filter the archive through zstd
    -a, --auto-compress Use archive suffix to determine the compression program
    -C, --directory=DIR Change to directory DIR before operation
    -p, --preserve-permissions Extract information about file permissions
    --exclude=PATTERN Exclude files matching PATTERN
    -r, --append Append files to the end of an archive
    -u, --update Only append files newer than copy in archive
    -P, --absolute-names Don't strip leading '/' from file names
    --strip-components=NUMBER Strip NUMBER leading components from file names on extraction
    --transform=EXPRESSION, --xform=EXPRESSION Use sed replace EXPRESSION to transform file names
    -k, --keep-old-files Don't replace existing files when extracting
    -g, --listed-incremental=FILE Handle new GNU-format incremental backup
    --help Display help information
    --version Display program version

    Examples

    How to Use These Examples

    The examples below show common ways to use the tar command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Create a tar archive tar -cf archive.tar file1 file2 directory1
    # Create a gzipped tar archive (tarball) tar -czf archive.tar.gz directory/
    # Create a bzip2-compressed tar archive tar -cjf archive.tar.bz2 directory/
    # Extract a tar archive tar -xf archive.tar
    # Extract a gzipped tar archive tar -xzf archive.tar.gz
    # Extract a bzip2-compressed tar archive tar -xjf archive.tar.bz2 # List contents of a tar archive tar -tf archive.tar # Advanced Examples Advanced # Create archive with verbose output tar -cvf archive.tar directory/ # Extract to a specific directory tar -xf archive.tar -C /target/directory/ # Archive with exclusions tar -czf backup.tar.gz /home/user --exclude=/home/user/Downloads --exclude=*.mp4 # Append files to an existing archive tar -rf archive.tar newfile.txt # Create archive with absolute paths tar -czPf backup.tar.gz /etc/passwd /etc/shadow # View detailed contents of an archive tar -tvf archive.tar # Extract specific files from an archive tar -xf archive.tar file1.txt file2.txt # Create archive with timestamp preservation tar -cpf archive.tar directory/ # Estimate the size of the archive before creating it tar -cf - directory/ | wc -c # Create incremental backup tar -czf backup-$(date +%F).tar.gz -g snapshot.file directory/ # Encrypt archive during creation (using gpg) tar -czf - directory/ | gpg -c > archive.tar.gz.gpg # Extract from encrypted archive gpg -d archive.tar.gz.gpg | tar -xz # Extract specific directory with path preservation tar -xf archive.tar --strip-components=2 directory/subdirectory/ # Create split archives (each 1GB) tar -cf - directory/ | split -b 1G - archive.tar.part # Reassemble and extract split archives
    cat archive.tar.part* | tar -xf -

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `tar` command, which stands for 'tape archive', is one of the most versatile and widely used utilities in Linux and Unix-like operating systems for archiving files and directories. Originally designed to write data to sequential I/O devices like tape drives (hence the name), it has evolved into a general-purpose archiving tool for all types of storage media. At its core, `tar` combines multiple files and directories into a single file (an archive) while preserving file system information such as permissions, ownership, timestamps, and directory structure. This makes it ideal for backup purposes, software distribution, and transferring multiple files as a single entity. Unlike some archiving utilities, `tar` itself doesn't perform compression. However, it can be used in conjunction with various compression programs (like gzip, bzip2, xz, or zstd) to create compressed archives, commonly known as 'tarballs'. This is done through options like `-z` for gzip compression, `-j` for bzip2, `-J` for xz, or `--zstd` for Zstandard compression. The `tar` command follows a somewhat unique option syntax. While it now supports the standard GNU-style options with double dashes (like `--create`), it's often used with bundled single-letter options without the leading dash (like `tar cvf` instead of `tar -cvf`). This historical syntax quirk can be confusing to newcomers. Common operations with `tar` include: 1. **Creating archives**: Using the `-c` (create) option, `tar` can package multiple files and directories into a single archive file. 2. **Extracting archives**: With the `-x` (extract) option, `tar` unpacks archives, restoring the original file structure. 3. **Viewing archive contents**: The `-t` (list) option allows users to view the contents of an archive without extracting it. 4. **Appending to archives**: The `-r` (append) option adds files to an existing archive. 5. **Updating archives**: The `-u` (update) option adds files only if they're newer than the versions already in the archive. `tar` supports numerous advanced features, including incremental backups, file transformations during archiving or extraction, selective file exclusion, and the ability to maintain absolute file paths. It can also handle special files like symbolic links, device files, and named pipes. The versatility of `tar` makes it an essential tool for system administrators, developers, and power users alike. Whether for simple backups, software distribution, or complex archiving tasks, `tar` remains a cornerstone utility in the Unix/Linux toolset.

    Related Commands

    These commands are frequently used alongside tar or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the tar command works in different scenarios.

    $ tar
    View All Commands