$linuxjunkies
>

tar(1)

Create, extract, and manage tape archive files for backing up or distributing groups of files.

UbuntuDebianFedoraArch

Synopsis

tar [OPTION]... [FILE]...

Description

tar packages multiple files and directories into a single archive file (tarball), optionally compressing it. It preserves file permissions, ownership, and directory structure, making it ideal for backups and software distribution.

The three main modes are: create an archive (-c), extract files (-x), or list contents (-t). Compression is handled transparently with -z (gzip), -j (bzip2), or -J (xz).

Modern tar accepts options in any order and auto-detects compression, so tar xf archive.tar.gz works without specifying the compression type.

Common options

FlagWhat it does
-cCreate a new archive
-xExtract files from an archive
-tList the contents of an archive
-fSpecify the archive filename (use - for stdin/stdout)
-zCompress or decompress with gzip
-jCompress or decompress with bzip2
-JCompress or decompress with xz
-vVerbose; list files being processed
-pPreserve file permissions and ownership
-CChange to directory before processing files
--strip-components=NRemove N leading path components when extracting
-OExtract to stdout instead of to files

Examples

Create a gzip-compressed archive of the documents directory

tar -czf backup.tar.gz /home/user/documents

Extract all files from an uncompressed tarball

tar -xf archive.tar

Extract a gzip archive into the /tmp directory

tar -xzf archive.tar.gz -C /tmp

List the first 20 files in a compressed archive without extracting

tar -tzf archive.tar.gz | head -20

Create a bzip2 archive excluding all .log files

tar -cjf backup.tar.bz2 --exclude='*.log' /var/www

Extract archive, removing one leading directory level from all paths

tar -xf archive.tar --strip-components=1

Stream a compressed archive over SSH to backup on a remote machine

tar -czf - /home/user | ssh remote 'tar -xzf - -C /backup'

List all files matching 'README' in an archive with details

tar -tvf archive.tar | grep README

Related commands