Unrar is a useful utility that allows you to uncompress one or more files & folders. It even allows you to unrar a multipart file into single file. In this article, we will look at how to unrar multiple files and how to keep directory structure even after unrar.
How to Unrar Multiple Files
Here are the different ways to unrar multiple files. Please note, in this article, we will be uncompressing multiple files separately and not combining them. If you want to unrar a multipart rar file then here is how to extract multipart rar file.
1. Extract multiple .rar files
First of all, copy all rar files in a single folder e.g /home/ubuntu. Then open terminal and run the following command. Replace /home/ubuntu/ below with your folder of choice.
$ for z in /home/ubuntu/*.rar do sudo unrar e $z; done
OR
$ for f in /home/ubuntu/*.rar; do sudo unrar e “$f”; done
You may also create a shell script for the same.
2. Create empty shell script
Open terminal and run the following command to create an empty shell script.
$ sudo vi multirar_extract.sh
3. Add shell script
Add the following shell script to this new file.
#!/bin/bash for z in $1/*.rar do sudo unrar e $z; done
Save and close file.
In the above code, we have used $1 instead of folder location. We will be passing folder location containing .rar files as a command line argument. This allows us to use the script for any folder.
4. Make shell script executable
Make the above file executable.
$ sudo chmod +x multirar_extract.sh
5. Run shell script
Run the shell script as shown below.
$ ./multrar_extract.sh /home/ubuntu
Please do not add trailing slash after the command line argument since we have added it in our code.
That’s it. In this article, we have looked at how unrar multiple files in a directory. You can use the for loop mentioned in step or the shell script created afterwards to extract multiple .rar files in a folder.
Also read:
How to Find Recently Modified Files in Linux
How to Install VNC Server in Linux
How to Redirect Wildcard Subdomains to Root Domain
How to Mount ISO Files in Ubuntu
How to Mount Drive from Terminal
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.