Extract a tar.gz Archive into a Specific Folder

Unpack a gzipped tar archive straight into any target directory using tar -xzf with -C.

macOSFilesarchivetarLow risk
Run tar -xzf backup.tar.gz -C ~/Documents/restore to extract the archive into that folder.

01Command

Command
$ tar -xzf backup.tar.gz -C ~/Documents/restore

02What it does

The letters stack up predictably: x = extract, z = the archive is gzipped, f = the next word is the filename, and -C switches to the target directory before extracting, so nothing lands in your current folder. macOS ships bsdtar, which also opens .tar.bz2 (-xjf) and .tar.xz (-xJf) with the same pattern.

03Risk

RiskLow risk
!Warning: If the archive contains absolute paths or '..' entries, extraction can write outside the target folder — inspect first with tar -tzf backup.tar.gz.

04Undo

Undo
$ mkdir -p ~/Documents/restore

05Sources

  • tar(1) man page
    manpagez.com · technical · 2026-09-17
    Evidence: The tar man page describes -xzf for extracting a gzipped archive and the -C option to switch to a directory before extracting.
  • tar - Manipulate Tape Archives (bsdtar)
    SS64 · technical · 2026-09-17
    Evidence: Documents the -x extract, -z gunzip and -f file flags plus -C to change directory, extracting the archive into the named target folder.

Last verified: 2026-09-19 · Category: Files & Folders

06Related solutions