# tar — Create and Extract Archives

> Practical guide to tar — create and extract archives with gzip, bzip2 and xz compression, backups, SSH transfer and selective extraction.

Source: https://www.jpkc.com/db/en/cheatsheets/archives/tar/

<!-- PROSE:intro -->
tar is the go-to tool on Linux and Unix for bundling whole directory trees into a single archive file – preserving permissions, timestamps and symlinks. Originally built for tape drives (hence "tape archive"), it remains the backbone of nearly every backup and software distribution. tar doesn't compress on its own; it hands the data stream off to gzip, bzip2 or xz, which you select with a single extra flag. This guide walks you through the essential commands for packing, extracting, inspecting and building advanced backup workflows.
<!-- PROSE:intro:end -->

## Create Archives

`tar -cf <archive.tar> <files>` — Create an uncompressed tar archive.

```bash
tar -cf backup.tar /home/user/docs/
```

`tar -czf <archive.tar.gz> <files>` — Create a gzip-compressed archive.

```bash
tar -czf backup.tar.gz /home/user/docs/
```

`tar -cjf <archive.tar.bz2> <files>` — Create a bzip2-compressed archive.

```bash
tar -cjf backup.tar.bz2 /home/user/docs/
```

`tar -cJf <archive.tar.xz> <files>` — Create an xz-compressed archive (best compression).

```bash
tar -cJf backup.tar.xz /home/user/docs/
```

`tar -cf <archive.tar> -C <directory> .` — Create archive from a directory without including the parent path.

```bash
tar -cf backup.tar -C /home/user/docs/ .
```

`tar -czf <archive.tar.gz> --exclude='<pattern>' <files>` — Create archive excluding files matching a pattern.

```bash
tar -czf backup.tar.gz --exclude='*.log' /home/user/docs/
```

## Extract Archives

`tar -xf <archive.tar>` — Extract a tar archive to the current directory.

```bash
tar -xf backup.tar
```

`tar -xzf <archive.tar.gz>` — Extract a gzip-compressed archive.

```bash
tar -xzf backup.tar.gz
```

`tar -xjf <archive.tar.bz2>` — Extract a bzip2-compressed archive.

```bash
tar -xjf backup.tar.bz2
```

`tar -xJf <archive.tar.xz>` — Extract an xz-compressed archive.

```bash
tar -xJf backup.tar.xz
```

`tar -xf <archive.tar> -C <directory>` — Extract to a specific directory.

```bash
tar -xf backup.tar -C /tmp/restore/
```

`tar -xf <archive.tar> <file>` — Extract only a specific file from the archive.

```bash
tar -xf backup.tar home/user/docs/readme.txt
```

`tar -xf <archive.tar> --wildcards '*.txt'` — Extract only files matching a wildcard pattern.

```bash
tar -xf backup.tar --wildcards '*.txt'
```

## List Contents

`tar -tf <archive.tar>` — List all files in an archive without extracting.

```bash
tar -tf backup.tar
```

`tar -tzf <archive.tar.gz>` — List contents of a gzip-compressed archive.

```bash
tar -tzf backup.tar.gz
```

`tar -tvf <archive.tar>` — List contents with detailed file info (permissions, size, date).

```bash
tar -tvf backup.tar
```

`tar -tf <archive.tar> | grep <pattern>` — Search for a specific file in the archive.

```bash
tar -tf backup.tar | grep config
```

## Append & Update

`tar -rf <archive.tar> <files>` — Append files to an existing tar archive (uncompressed only).

```bash
tar -rf backup.tar newfile.txt
```

`tar -uf <archive.tar> <files>` — Update files in the archive only if they are newer.

```bash
tar -uf backup.tar /home/user/docs/
```

`tar --delete -f <archive.tar> <file>` — Remove a file from the archive (uncompressed only).

```bash
tar --delete -f backup.tar oldfile.txt
```

## Advanced Options

`tar -czf - <files> | ssh <host> "cat > backup.tar.gz"` — Create a compressed archive and send it to a remote server via SSH.

```bash
tar -czf - /home/user/docs/ | ssh server "cat > backup.tar.gz"
```

`tar -czf <archive.tar.gz> --newer-mtime='<date>' <files>` — Archive only files modified after a certain date.

```bash
tar -czf recent.tar.gz --newer-mtime='2025-01-01' /home/user/docs/
```

`tar -czf <archive.tar.gz> -T <filelist>` — Create archive from a list of files in a text file.

```bash
tar -czf backup.tar.gz -T files.txt
```

`tar -czf - <files> | split -b <size> - <prefix>` — Create a split archive for large backups.

```bash
tar -czf - /data/ | split -b 100M - backup_part_
```

`cat <prefix>* | tar -xzf -` — Extract a split archive.

```bash
cat backup_part_* | tar -xzf -
```

<!-- PROSE:outro -->
## Conclusion

tar is remarkably versatile: a single tool for backups, software packages and shipping entire directory trees across the network. Remember the two letter pairs `-c` (create) and `-x` (extract) plus the compression switch (`z` for gzip, `j` for bzip2, `J` for xz) and you cover the daily essentials. When creating archives, tar strips leading `/` from absolute paths, so archives extract relative by default. Still, be cautious when unpacking archives from untrusted sources: inspect them first with `-t`, since crafted paths (path traversal via `../`) could overwrite files outside the target directory.

## Further Reading

- [GNU tar manual](https://www.gnu.org/software/tar/manual/) – the official, complete reference for every option
- [tar – Wikipedia](https://en.wikipedia.org/wiki/Tar_(computing)) – background on the file format and its history
<!-- PROSE:outro:end -->

## Related Commands

- [7z](https://www.jpkc.com/db/en/cheatsheets/archives/7z/) – high-compression archiver with its own 7z format
- [gzip](https://www.jpkc.com/db/en/cheatsheets/archives/gzip/) – fast stream compression, often paired with tar
- [zip](https://www.jpkc.com/db/en/cheatsheets/archives/zip/) – cross-platform archive format with per-file compression

