Software & AppsOperating SystemLinux

How To Extract a Zip Archive with a Different Folder Name Using Unzip

Ubuntu 19

In this article, we will delve into the process of extracting a zip archive with a different folder name using the unzip command in Unix-based systems like Linux or MacOS. The unzip utility is a powerful tool that allows you to list, test, and extract compressed files in a ZIP archive.

Quick Answer

To extract a zip archive with a different folder name using the unzip command, you can use the -d option followed by the target folder name. For example, to extract a zip file named ‘example.zip’ into a folder named ‘examplefold’, you can use the command unzip example.zip -d examplefold.

Understanding the Unzip Command

Before we proceed, it’s crucial to understand the unzip command. The basic syntax of the unzip command is as follows:

unzip filename.zip

This command will extract the contents of ‘filename.zip’ into a folder named ‘filename’ in the current directory.

However, if you want to extract the zip file into a folder with a different name, you can use the -d option followed by the target folder name.

Extracting a Zip Archive into a Different Folder

To extract a zip archive named ‘example.zip’ into a folder named ‘examplefold’, you can use the following command:

unzip example.zip -d examplefold

In this command, -d examplefold specifies the target directory where the zip file will be extracted. The -d option stands for directory.

Please note that if the ‘examplefold’ directory does not exist, the unzip command will create it. If there is already a folder named ‘examplefold’ in the current directory, the unzip command will overwrite its contents. So, make sure to choose a target directory name that does not conflict with existing folders.

Renaming the Extracted Folder

Another way to achieve the same result is to first extract the zip file using the unzip command and then rename the extracted folder using the mv command. Here is how you can do it:

unzip example.zip && mv example examplefold

In this command, && is a logical AND operator that allows you to execute the second command (mv example examplefold) only if the first command (unzip example.zip) completes successfully. The mv command is used to rename or move files and directories.

Extracting Multiple Zip Files into Different Folders

In the context of a bash script, you can modify the script as follows to extract multiple zip files with different target folder names:

for zipName in 2 25 45 65 85 110 264 1000 10352
do 
 unzip $zipName.zip -d examplefold_$zipName
done

This script will extract each zip file into a folder named ‘examplefold_’ followed by the respective zip file name. The $zipName is a variable that holds the name of the zip file in each iteration of the loop.

Conclusion

The unzip command is a versatile tool that can be used to extract zip archives into folders with different names. Whether you’re dealing with a single zip file or multiple zip files, the unzip command combined with the -d option or the mv command can help you achieve your goal.

Remember to be cautious while choosing the target directory name to avoid overwriting existing folders. And always feel free to refer to the man page of the unzip command to explore more options and functionalities.

What is the purpose of the `unzip` command?

The unzip command is used to extract the contents of a zip archive.

Can I extract a zip archive into a folder with a different name?

Yes, you can use the -d option followed by the target folder name to extract the zip file into a folder with a different name.

What happens if the target folder already exists when using the `-d` option?

If the target folder already exists, the unzip command will overwrite its contents.

How can I rename the extracted folder after using the `unzip` command?

You can use the mv command to rename the extracted folder.

Can I extract multiple zip files into different folders?

Yes, you can use a loop in a bash script to extract multiple zip files into different folders with unique names.

Leave a Comment

Your email address will not be published. Required fields are marked *