Skip to content

How to Extract a tar File to a Specific Directory or Extract Only One Folder in Linux

Updated 5 min read

Updated: 9 October 2026 · Applies to: GNU tar 1.35 (Debian 13, Ubuntu 24.04 and 26.04, AlmaLinux 10) and 1.34 (Rocky Linux 9); bsdtar 3.7 (libarchive) and the tar on macOS

To extract a tar file to a specific directory, add -C (capital C) and the target folder. To extract only one directory from the archive, add that directory's path, exactly as the archive lists it, at the end:

tar -xzf backup.tar.gz -C /var/www/site                 # everything, into /var/www/site
tar -xzf backup.tar.gz -C /tmp/restore site/images      # only the site/images folder

-x extracts, -z handles gzip (.tar.gz, .tgz) and -f names the archive. GNU tar also detects the compression on its own, so tar -xf works for .tar.gz, .tar.bz2, .tar.xz and .tar.zst too, as long as the matching program (bzip2, xz or zstd) is installed; without it tar stops with Child returned status 127. Every command and output below comes from our tests on the versions above.

1. Extract into another folder

mkdir -p /var/www/site
tar -xzf backup.tar.gz -C /var/www/site

The target folder must already exist. If it does not, tar stops before extracting anything:

$ tar -xzf site.tar.gz -C /tmp/nope
tar: /tmp/nope: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now

--directory=/var/www/site is the long form of -C.

2. List the archive first

Before you extract a single directory, check the exact paths inside the archive:

$ tar -tzf site.tar.gz
site/
site/index.html
site/css/
site/css/style.css
site/images/
site/images/logo.png
site/images/icons/
site/images/icons/x.svg
site/wp-config.php

For a large archive, add | head or | grep images.

3. Extract only one directory (or one file)

Put the path from the list after the options. tar extracts that directory with everything below it, and keeps the path:

$ mkdir -p /tmp/restore
$ tar -xzvf site.tar.gz -C /tmp/restore site/images
site/images/
site/images/logo.png
site/images/icons/
site/images/icons/x.svg

The files land in /tmp/restore/site/images/. A trailing slash (site/images/) also worked in our tests. Name several paths to extract several items, for example site/css site/wp-config.php.

If the path does not match, nothing is extracted and tar exits with status 2:

$ tar -xzf site.tar.gz -C /tmp/restore site/imgs
tar: site/imgs: Not found in archive
tar: Exiting with failure status due to previous errors

A common cause: archives made with tar -czf backup.tar.gz . store every path with a leading ./ (./site/images/). site/images is then "Not found in archive", while ./site/images works.

4. Extract files that match a pattern

tar -xzvf site.tar.gz -C /tmp/restore --wildcards 'site/*.php'

Quote the pattern so the shell does not expand it. On Debian and Ubuntu, a pattern without --wildcards fails:

tar: Pattern matching characters used in file names
tar: Use --wildcards to enable pattern matching, or --no-wildcards to suppress this warning
tar: site/*.php: Not found in archive

The tar package on AlmaLinux 10 and Rocky Linux 9 matched the same pattern without the option, but adding --wildcards works everywhere.

5. Extract a directory's contents without its parent folders

--strip-components=N removes the first N parts of every path. To put the contents of site/images straight into a folder:

$ mkdir -p /tmp/img
$ tar -xzf site.tar.gz -C /tmp/img --strip-components=2 site/images
$ find /tmp/img -type f
/tmp/img/icons/x.svg
/tmp/img/logo.png

With --strip-components=1 and no path, the whole archive goes into the target without its top-level folder, which is handy for a website archive such as tar -xzf site.tar.gz -C /var/www/html --strip-components=1.

6. Skip a directory, or print one file

  • Everything except one folder: tar -xzf site.tar.gz -C /tmp/restore --exclude='site/images'
  • Show one file without writing it to disk: tar -xzOf site.tar.gz site/wp-config.php (-O sends it to standard output, so you can also redirect it to a new file).

7. Create a tar archive of a directory

To tar a folder on Ubuntu or any other Linux, use -c (create). -C works here too: it changes into /srv first, so the archive stores site/... instead of the full path:

$ tar -czvf site.tar.gz -C /srv site
site/
site/index.html
site/css/
site/css/style.css
site/images/
site/images/logo.png
site/images/icons/
site/images/icons/x.svg
site/wp-config.php

If you give an absolute path instead (tar -czf css.tar.gz /srv/site/css), tar prints tar: Removing leading `/' from member names and stores srv/site/css/..., so it later extracts relative to the folder you are in, never over /. Add --exclude='site/images' to leave a folder out. More options, including bzip2, xz and zip: how to create and extract a tar file in Linux and zip and unzip to a specific directory.

8. macOS, FreeBSD and other Unix systems

macOS ships bsdtar (from libarchive) as tar. It takes the same -x, -z, -f, -C, member paths and --strip-components, and matches patterns such as 'site/*.php' without an extra option. In our test with bsdtar 3.7.4, a wrong path printed bsdtar: site/imgs: Not found in archive and exited with status 1.

If tar is missing

If you get tar: command not found, install it with sudo dnf install tar (AlmaLinux, Rocky Linux, RHEL) or sudo apt install tar (Debian, Ubuntu). Related: split a large tar archive into parts and download and extract a tar file with one command.

Official documentation: GNU tar manual.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Linux server management

hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.

Was this answer helpful?

Still stuck? Open a ticket