Commands.page Logo

How to Extract Files to a Specific Directory in Ubuntu

This article provides a concise guide on extracting archive contents into a chosen folder within the Ubuntu operating system. It identifies the specific command-line flags required for the most common archiving utilities, ensuring users can manage file placement efficiently without moving data after extraction.

Using the Tar Command

The tar utility is the standard tool for handling compressed archives in Linux. To extract files to a specific directory using tar, you must use the -C flag. This flag changes the directory before performing any operations.

The syntax is as follows:

tar -xf archive.tar.gz -C /path/to/destination

In this command, -x tells tar to extract, -f specifies the filename, and -C defines the target directory. Ensure the destination directory exists before running the command, as tar will not create it automatically.

Using the Unzip Command

For .zip files, the unzip utility is commonly used in Ubuntu. The flag used to extract files to a specific directory with unzip is -d.

The syntax is as follows:

unzip archive.zip -d /path/to/destination

Here, -d directs the extracted contents to the specified path. Like tar, the destination folder should exist prior to execution to avoid errors.