Tar in Linux: Archive and Extract Files Easily

Updated

Linux terminal with shell commands

tar xf archive.tar extracts into the current directory; add -C /target/dir to extract elsewhere, or --strip-components=1 to drop the archive’s top-level folder. If the archive is compressed (.tar.gz, .tar.bz2, .tar.xz), you don’t need a separate flag — see the auto-detection test on extracting .tar.bz2 files, which also has the compression-format benchmark. What follows is the part that actually causes damage: what happens when an archive’s internal paths don’t do what you expect.

Always list before you extract

tar -tf archive.tar prints every member path without writing anything to disk. Run it first on anything you didn’t create yourself — it’s the only way to see what an archive will do before it does it:

$ tar -tf release.tar
project-v1.2.3/
project-v1.2.3/src/
project-v1.2.3/src/main.c

An archive member with an absolute path

An archive can legally contain a member with an absolute path. Archiving a file by its absolute path and extracting the result with plain tar -xvf:

$ tar -cf abs.tar /tmp/hw_abs_demo/payload.txt
tar: Removing leading '/' from member names
$ tar -xvf abs.tar
x tmp/hw_abs_demo/payload.txt

bsdtar strips the leading / by default and writes into a relative tmp/hw_abs_demo/ folder under the current directory — it does not touch the real /tmp/hw_abs_demo. GNU tar documents the same default protection (also stripping the leading slash unless you pass -P/--absolute-names); that specific claim is cited from GNU’s manual, not tested here, since this Mac doesn’t have GNU tar. Either way: don’t rely on the default holding forever — a sufficiently old or reconfigured tar might not warn — and tar -tf first still catches it before extraction.

An archive member with a ../ component

This is the classic “zip slip” style attack: a member path that tries to walk outside the extraction directory. Referencing a file through a parent directory (tar -cf trav.tar ../target.txt from inside a subdirectory) stores the member with that literal ../ in its name, and extraction refuses it:

$ tar -tf trav.tar
../target.txt
$ tar -xvf trav.tar
x ../target.txt: Path contains '..': Unknown error: -1
tar: Error exit delayed from previous errors.

bsdtar 3.5.3 refuses outright and exits non-zero — nothing escaped the extraction directory. That’s the current libarchive default, not a universal guarantee across every tar implementation and version; treat it as a second line of defense, not a reason to skip tar -tf on untrusted archives.

–strip-components for archives with an unwanted top folder

Most release tarballs wrap everything in a version-named folder (project-v1.2.3/). Extracting normally reproduces that folder; --strip-components=1 drops the first path segment from every member as it’s written:

$ tar -xf release.tar --strip-components=1
$ find . -type f
./src/main.c

Set the count to the number of leading path segments to drop, not a fixed “1” — an archive nested two folders deep needs --strip-components=2, and over-stripping will collide filenames from different subdirectories into the same target path.

/verify/tar-extract-archive-files.sh rebuilds all three test archives and re-runs the listing, absolute-path, traversal, and strip-components checks.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.