tar(1)
Create, extract, and manage tape archive files for backing up or distributing groups of files.
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
| Flag | What it does |
|---|---|
-c | Create a new archive |
-x | Extract files from an archive |
-t | List the contents of an archive |
-f | Specify the archive filename (use - for stdin/stdout) |
-z | Compress or decompress with gzip |
-j | Compress or decompress with bzip2 |
-J | Compress or decompress with xz |
-v | Verbose; list files being processed |
-p | Preserve file permissions and ownership |
-C | Change to directory before processing files |
--strip-components=N | Remove N leading path components when extracting |
-O | Extract to stdout instead of to files |
Examples
Create a gzip-compressed archive of the documents directory
tar -czf backup.tar.gz /home/user/documentsExtract all files from an uncompressed tarball
tar -xf archive.tarExtract a gzip archive into the /tmp directory
tar -xzf archive.tar.gz -C /tmpList the first 20 files in a compressed archive without extracting
tar -tzf archive.tar.gz | head -20Create a bzip2 archive excluding all .log files
tar -cjf backup.tar.bz2 --exclude='*.log' /var/wwwExtract archive, removing one leading directory level from all paths
tar -xf archive.tar --strip-components=1Stream 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