File compression is a way to create archives that help us in saving time, creating space, and downloading and transferring software and data faster. You can use a compressed file to distribute related files on the Internet, all compressed into a single file, without any loss of data. If you need space on your system or an external storage device, you can compress files into smaller archived files. At times we have to extract multiple zipped and rar’d files at once, all located in a single folder. Doing so through the Linux UI is fairly simple; all you need to do is select all the files you want to extract, right-click, and use the extract option to extract them altogether. The real deal is when we want to do the same task through the command line. It can prove to be quite lengthy, and frankly illogical, to extract one by one by entering the file extraction commands one by one. Here comes the bash’s for loop to rescue. You can use it to perform multiple similar operations at once.
This article describes how you can use the for loop in bash shell to extract multiple files of the following types through the Debian command line:
- Zip files
- Tar.xz files
- Rar files
- 7z files
We have run the commands and procedures mentioned in this article on a Debian 10 Buster system. We are using the Terminal application as the Debian command line. You can open it through the Application Launcher search as follows:
The Application Launcher can be accessed using the Windows/Super key on your keyboard.
Unzip Multiple Files at Once
Let us suppose that a folder, a “zip_files” folder in our case, contains multiple zipped files and we want to extract them simultaneously.
Here is how you can use the for loop to make the task simple:
$ for z in *.zip do unzip $z; done
Here is how you can achieve the same task through one single command:
$ for z in *.zip; do unzip "$z"; done
Extract multiple tar.xz files at Once
Let us suppose that a folder contains multiple tar.xz files and we want to extract them simultaneously.
Here is how you can use the for loop to make the task simple:
$ for z in *.tar.xz do tar -xf $z; done
Here is how you can achieve the same task through one single command:
$ for z in *.tar.xz; do tar -xf "$z"; done
Unrar Multiple Files at Once
Use the following command in order to unrar multiple rar files at once.
$ for z in *.rar do unrar e $z; done
Or,
$ for f in *.rar; do unrar e “$f”; done
Extract Multiple 7z files at Once
Use the following command in order to extract multiple 7z files at once.
$ for z in *.7z do 7z e $z; done
Or,
$ for z in *.7z; do 7z e "$z"; done
Through the use of the bash for loop, you can make the hectic task of extracting multiple compressed files, all at once. This small skill you learned in this article comes especially handy when we have to extract as much as hundreds of compressed files simultaneously. Not only for file extraction, but you can also use the power of the bash ‘for’ loop to perform various other similar tasks that can take longer when you run them one by one.