Sometimes we need to unzip multiple zipped and rar’ed files at once, all in a single folder. In the Ubuntu user interface, this is pretty easy: you just have to select all the files you want to unzip, right-click on them and use the Extract option to unzip them all. Things get really difficult when we want to do the same task from the command line. It can prove to be quite tedious and illogical to extract the files one by one, typing the commands to extract them one by one. This is where Bash’s for loop comes in handy. It allows you to perform several similar operations at once.

This article describes how you can use the for loop to extract multiple files of the following type using the Ubuntu command line:

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

We ran the commands and procedures mentioned in this article on an Ubuntu 22.04 LTS system. Our example zip and tar folders each contain 4 compressed files of type zip and rar. We use the terminal application for the Ubuntu command line. You can open it via the System Dash or the key combination Ctrl alt T.

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

<img alt="Unzip multiple files on Linux shell" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-80.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="287" src="data:image/svg xml,” width=”396″>

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

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

<img alt="Same as above, just in one command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-81.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="199" loading="lazy" src="data:image/svg xml,” width=”568″>

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

<img alt="Extract multiple tar.gz files on Linux" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-82.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="131" loading="lazy" src="data:image/svg xml,” width=”724″>

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

Using the Bash for loop, you can accomplish the task of unpacking multiple compressed files at once easily. This little skill you learned in this article is especially handy when we need to unpack hundreds of compressed files at once. You can use the for loop not only to unzip files, but also for other similar tasks that take longer if you do them one at a time.