At times we have to extract multiple zipped and rar’d files at once, all located in a single folder. Doing so through the Ubuntu 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 them individually by entering the file extraction commands one by one. Here comes the bash 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 to extract multiple files of the following type through the Ubuntu command line:

  • Zip files
  • Tar.xz files
  • Rar files
  • 7z files

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system. Our sample zip and tar folders contain 4 compressed files of zipped and rar’d file types respectively. We are using the Terminal application for using Ubuntu command line. You can open it through the system Dash or the Ctrl+alt+T shortcut.

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

Unzip multiple files on Linux shell

Here is how you can achieve the same task through one single command:

$ for z in *.zip; do unzip "$z"; done

Same as above, just in one command

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

Extract multiple tar.gz files on Linux

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 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 for loop to perform various other similar tasks that can take longer when you run them one by one.

How to Simultaneously Unzip or Unrar Multiple Files in Linux