tar. extract a tar.gz to a specific folder

how to extract tar.gz file to specific folder Linux 2025

This refinement adds context by specifying the operating system (Linux) and includes 'how to' to target instructional content. The year 2025 is included to ensure the results are up-to-date.

Extracting a .tar.gz file to a specific folder on a Linux system is a straightforward process, but it requires the correct usage of the tar command with specific flags. This guide provides a comprehensive overview of how to perform this task effectively.

Understanding Tar and Tar.gz Files

Files with the .tar.gz extension are compressed archives that have been packaged using the tar (Tape Archive) command and subsequently compressed with gzip. This combination is commonly used for software distribution and backups on Unix-like systems.

Basic Command Structure

To extract files from a .tar.gz archive, you typically use the command:

tar -xvzf file.tar.gz
  • -x: Extract files from the archive.
  • -v: Verbosely list files processed.
  • -z: Decompress the archive using gzip.
  • -f: Use the next argument as the name of the archive file.

Extracting to a Specific Directory

To extract the contents of a .tar.gz file to a specific directory, you will use the -C option, which specifies the target directory for extraction. Here’s how to do it:

Step-by-Step Instructions

  1. Open Terminal: You can do this by searching for "Terminal" in your applications menu.

  2. Navigate to the File Location: Change to the directory where your .tar.gz file is located. For example:

    cd /path/to/directory
    
  3. Use the tar Command: Type the following command to extract the archive to your desired location:

    tar -xvzf file.tar.gz -C /path/to/target/directory
    

    Replace file.tar.gz with the actual name of your archive and /path/to/target/directory with the directory where you want the files to be extracted.

Example

If your .tar.gz file is named example.tar.gz and you want to extract it to a folder named output in your home directory, you would use:

tar -xvzf example.tar.gz -C ~/output/

Extracting a Specific Folder from the Archive

If you need to extract only a specific folder from within a larger .tar.gz archive, you can specify the folder path in the command. To do so, use the following syntax:

tar -xvzf archive.tar.gz --strip-components=1 -C /target/directory path/to/specific/folder/
  • --strip-components=1: This option removes the specified number of leading components from file names before extraction. This is useful if you want to avoid recreating the full path structure present in the archive.

Example

To extract only the docs folder into your ~/output directory, you would write:

tar -xvzf example.tar.gz --strip-components=1 -C ~/output/ docs/

Summary

The tar command is a powerful tool for managing archive files in Linux. By using the -C option, you can easily direct the extracted files to any location you wish. Remember to customize your command according to the specific structure of your .tar.gz file and the desired output folder. This method keeps your filesystem organized and helps manage space effectively.

For further reading on the tar command and its options, you can check the manual by typing man tar in the terminal or consult resources like phoenixNAP and tecMint.

Related Searches

Sources

10
1
Extract a specific folder to specific directory from a tar.gz
Stack Overflow

What I would like to do is extract a specific folder from a tar.gz to another folder in a different path that how it is in the tar.gz.

2
How to Extract or Unzip .tar.gz Files in Linux (With Examples)
Strongdm

Open your Linux file manager. · Go to the folder that contains the .tar.gz file. · Right-click on it and choose "Extract to." · Choose your ...

3
How to extract files to another directory using 'tar' command?
Askubuntu

To extract an archive to a directory different from the current, use the -C, or --directory, tar option, as in tar -xf archive.tar -C /target/directory.

4
How to Extract or Unzip .tar.gz Files in Linux | phoenixNAP KB
Phoenixnap

To unzip the .tar.gz file, use the tar command with the following arguments: tar –xvzf [archive name] Copy The basic command is tar, followed by four options.

5
How do you extract a single folder from a large tar.gz archive?
Unix

I am using this command on a 5GB archive tar -zxvf archive.tar.gz /folder/in/archive is this the correct way to do this? It seems to be taking forever with no ...

6
How do I unzip a tar gz archive to a specific destination? - Super User
Superuser

I want to unpack a .tar.gz file to a specific directory. The archive file is in /root/Documents. I want to unzip it into /root/Desktop/folder.

7
How To Extract a Tar Files To a Different Directory on a Linux/Unix ...
Cyberciti

This tutorial explains how to extract a tar ball archive to a specific directory on a Linux, BSD, macOS, and Unix using command prompt.

8
How To Extract and Unzip .tar.gz Files (for Linux & Windows) - Kinsta
Kinsta

Simply add a space-separated list of the files you want to extract. Extract file1 and directory1 from .tar.gz file to current working directory:

9
Extract specific folder from tarball into specific folder - Ask Ubuntu
Askubuntu

To extract a single file / folder from an archive that uses relative paths without its ancestors into a relative path you'll need two options: -C and --strip- ...

10
How to Extract Tar Files to Specific or Different Directory in Linux
Tecmint

In this guide, we shall take a look at how to extract tar files to a specific or different directory, where you want the files to reside.