If you are looking for software to use, go to Huajun Software Park! software release AI product list
Location: Home pageTutorial strategyTutorialComputer software tutorials Linux at the same time...

How to decompress multiple compressed files simultaneously in Linux

Author: GiuliaQ Date: 2017-05-23

The following is a tutorial on how to decompress multiple compressed files in Deepin at the same time in Linux. Using this tutorial, you don’t have to decompress the files one by one.

How to decompress multiple compressed files simultaneously in Linux

$ls

backup1.tar backup2.tar backup3.tar

We need to unpack them all together, and how do we do that?

Let's first briefly explain the usage of tar. The tar command was originally used to read and write files from tape devices (tar is the abbreviation of Tape ARchiver). We can only specify the name of the file to be put into the compressed archive or to be extracted (such as tar x myfineonthe.tape). You can use the -f option to tell tar that the archive is not on a tape but in a file. This option accepts only one parameter - the file name of the compressed archive. All other (later) parameters are considered part of the archive mentioned above.

tar -x -f backup.tar myfile.txt

# Or use the more common syntax below

tar xf backup.tar myfile.txt

Now back to our previous problem: decompress the three files backup1.tar backup2.tar backup3.tar in the current directory at the same time. Some friends may want to use tar xf *.tar. Let’s take a look at its execution results:

$ tar xf *.tar

tar: backup2.tar: Not found in archive

tar: backup3.tar: Not found in archive

tar: Exiting with failure status due to previous errors

What's going on? The Shell replaces *.tar by matching the file name. The above line is actually equivalent to:

tar xf backup1.tar backup2.tar backup3.tar

From our previous explanation of the usage of tar, we know that the meaning of the command we use here is "Extract backup2.tar and backup3.tar from the compressed archive backup1.tar". Only if there is a corresponding file name in the compressed archive backup1.tar can the execution be successful.

Solution: Unzip the files one by one from the compressed archive.

We are using a UNIX shell (Bash) and can do this using a loop:

for tarname in *.tar; do

tar xf “$tarname”

done

Let’s talk about the two basic concepts of loops and for-loops. A loop is a structure used to repeat code within it until a certain condition is met. The loop stops when this condition is met, and the code outside it continues to execute. A for-loop is a type of loop structure that successively sets a variable to each value in a list and repeats until the list is exhausted.

Here, the for-loop will repeatedly call the file name matching *.tar as a parameter to execute tar xf. In this way, we will "automatically" decompress the compressed files one by one.

Another very common archive format is ZIP. The command to unzip a ZIP file is unzip. The same problem exists here: unzip only accepts one option to specify a ZIP file.

This can be solved in the same way:

for zipfile in *.zip; do

unzip “$zipfile”

done

There is an alternative to the unzip command: it reads in a shell-like pattern to specify the ZIP file name. To prevent the shell from interpreting these styles, quote .unzip (instead of the shell) which will interpret *.zip here:

unzip “*.zip”

# You can also use the following approach that looks clearer:

unzip *.zip

 

Related articles