Extract Files to a Specified Folder in Linux

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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux system file queries.

In this context, we shall look into how to extract files to a specific directory on your Linux disk.

How to 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.

Its Syntax is given below:

$ 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.

How to Extract 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

[Need assistance in fixing Linux system issues? We can help you. ]

This article covers the process of extracting compressed files to a specific directory in Linux. 

To Unzip files in particular directory or folder under Linux, run the command:

$ unzip {.zip-file-name}-d {/path/to/extract}

Related Posts