files

tar

Create, list and extract tape archives, with or without compression.

Updated

tar bundles files and directories into a single archive and unpacks them again. One mode letter picks the job (c, t, x, r, u) and -f names the archive. The cards were checked against bsdtar; GNU tar accepts the common mode letters, but confirm less common options with tar --help.

Modes

Pick one mode

OptionMeaning
-cCreate a new archive
-tList archive contents
-xExtract to disk
-rAppend entries to an existing archive
-uAppend only entries newer than the archived copy

Name the archive

tar -cf site.tar public/
tar -tf site.tar
tar -cf - public/ | wc -c     # archive to stdout

-f - means standard input or output.

Create

Compressed archives

OptionMeaning
-zgzip (.tar.gz, .tgz)
-jbzip2 (.tar.bz2)
-Jxz (.tar.xz)
--zstdzstd (.tar.zst)
tar -czf notes-2026-10.tgz notes/

Let the suffix decide

tar -a -cf release.tar.xz build/

-a picks the compression from the archive name.

Archive from another directory

tar -czf backup.tgz -C /srv/app config data

-C changes directory first, so stored paths start at config/ and data/.

Leave things out

tar -czf src.tgz --exclude '*.log' --exclude node_modules project/
tar -czf src.tgz --exclude-vcs project/
tar -czf src.tgz -X skip-list.txt project/

--exclude-vcs skips version-control metadata such as .git directories.

Archive a file list

tar -cf picked.tar -T list.txt

Combine -T with --null when the list is NUL-separated, for example from find -print0.

Inspect and extract

List with details

tar -tvf site.tar
tar -tzf notes-2026-10.tgz 'notes/2026/*'

Reading auto-detects gzip, bzip2 and xz, so -z is optional when listing or extracting.

Extract

CommandPurpose
tar -xf site.tarUnpack into the current directory
tar -xf site.tar -C /tmp/siteUnpack into an existing target directory
tar -xf site.tar public/index.htmlUnpack only named entries
tar -xOf site.tar public/robots.txtPrint one entry to stdout

Drop a leading folder

tar -xzf tool-1.4.0.tgz --strip-components 1 -C ~/opt/tool

Removes the first path element; entries with fewer elements are skipped.

Keep existing files

tar -xkf site.tar

-k refuses to overwrite files already on disk.

Untrusted archives

Warning

Extraction overwrites files with the same name. List an unknown archive with -tvf first and extract into an empty directory; never add -P, which keeps absolute paths and .. components.

Variants

Rename while archiving

Note

tar -cf out.tar -s '/^build/app-1.4/' build/

-s path substitution is a bsdtar option; check your GNU tar manual for its equivalent.

Check your implementation

tar --version

macOS ships bsdtar (libarchive); most Linux distributions ship GNU tar. Option spellings can differ beyond the basics.