How to Extract Tar File to Specific Directory Ubuntu
This article provides a quick guide on restoring files from a tar archive in Ubuntu. It covers the essential commands needed to extract entire archives or individual files directly into a chosen folder path using the terminal.
To restore files from a tar archive, you will use the
tar command via the terminal. The key option for defining a
destination is -C, which changes the directory before
extracting. Ensure you know the name of your archive file and the path
where you want the files to reside.
Extract All Files to a Specific Location
To extract all contents from an archive to a specific directory, use the following syntax:
tar -xf archive.tar.gz -C /path/to/destinationReplace archive.tar.gz with your actual file name and
/path/to/destination with the full path of the target
folder. The -x flag tells tar to extract, -f
specifies the file name, and -C sets the destination
directory. If the destination directory does not exist, you must create
it first using mkdir.
Restore a Single File to a Specific Location
If you only need to restore one specific file from the archive, you can specify the file name after the archive name. First, list the contents to find the exact path within the archive:
tar -tf archive.tar.gzOnce you identify the file path inside the archive, run the extract command with the file name included:
tar -xf archive.tar.gz -C /path/to/destination ./path/inside/archive/file.txtNote that you often need to include the relative path of the file as
it appears inside the archive, usually prefixed with
./.
Verify the Restoration
After running the command, navigate to your destination directory to confirm the files were restored correctly:
cd /path/to/destination
lsThis method ensures your files are placed exactly where needed without requiring manual movement after extraction.