Extract a tar.gz Archive into a Specific Folder
Unpack a gzipped tar archive straight into any target directory using tar -xzf with -C.
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/restore02What 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
!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/restore05Sources
- tar(1) man pageEvidence: 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)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
Find files larger than 500 MB on Mac
List the biggest files in your home folder that are eating disk space.
Show file extensions for all files on Mac
Make Finder display .pdf, .docx, .png and every other extension at all times.
Find Which Process Has a File or Volume Open with lsof
List every open file descriptor — files, directories, and network sockets — per process.