Compressing files comes in handy when backing up important files or sending large files over the internet. This makes it easier and more convenient to download files while using far less bandwidth. These compressed files often have the following extensions such as zip, tar, tar.gz, gz
There are several tools on Linux to decompress such files. On several occasions, you have to extract the contents of an archived file to a specific folder on your system.
In this article, we will learn how to extract files to a specific directory on your disk.
Extract zip files to a specific directory
The unzip command is used to extract zip files using the terminal. By default, the unzip command extracts the zip file into your current working directory. If you want to extract the zipped files into a different directory, use the -d option followed by the path to the directory.
$ unzip zip-file-name.zip -d /path/to/directory
For example, to extract an archive file named font-files.zip to the /tmp/new directory run the command:
$ unzip font-files.zip -d /tmp/new
Note: The directory you want to extract needs to be already in existence. The unzip command cannot create a new directory on your system.
Extracting tar/tar.gz/tgz files to a specific directory
Most files in linux are compressed using the tar format. The tar command allows you to create tar archive files as well as decompress them. By default, the tar command will extract files to your current directory.
To extract files to a specific directory use the -c or –directory as shown in the syntax below:
$ tar -xf file-name.tar -C /path/to/directory
$ tar -xf file-name.tar --directory /path/to/directory
For example to extract backup.tar file to the /tmp/backup directory use the command shown. Ensure the directory exists or create it before extracting the files.
$ tar -xf backup.tar -C /tmp/backup
That’s it with extracting compressed files to a specific directory in Linux.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.