How to extract multiple compressed files at the same time in Linux

  

In the Linux system, the files can be decompressed on the Deepin command line. You can also decompress multiple tarball files at the same time using the Deepin command line. So how do you decompress multiple compressed files in Deepin at the same time in Linux?

$ ls

backup1.tar backup2.tar backup3.tar

We need to unpack them all together, how?

Let's 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 file or to be extracted (eg tar x myfineonthe.tape). You can use the -f option to tell tar that the file is not on a tape but in a file. This option accepts only one parameter —— the filename of the compressed archive. All other (later) parameters are considered part of the compressed file mentioned above.

tar -x -f backup.tar myfile.txt

# or use the more common syntax below

tar xf backup.tar myfile.txt

Now go back to our previous question: Unzip the backup1.tar backup2.tar backup3.tar file in the current directory. There may be friends who 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 is 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 The meaning of the command we use here is “ extract backup2.tar and backup3.tar” from the compressed file backup1.tar. Only if there is a corresponding file name in the compressed file backup1.tar can it be executed successfully.

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

We are using a UNIX shell (Bash) that can be implemented in a loop:

for tarname in *.tar; do

tar xf “$tarname”

done

Let's talk about the basic concepts of loops and for-loops. A loop is a structure used to repeat its internal code before a certain condition is met. When the condition is met, the loop stops and its external code continues to execute. The for loop is to set a variable to each value in a list, and repeat the loop structure until the list is exhausted.

Here, the for-loop will repeatedly call tar xf with the file name matching *.tar as a parameter. This will decompress the compressed file one by one automatically.

Another very common file format is ZIP. The command to extract ZIP files is unzip. Here is the same problem: unzip only accepts an option to specify a ZIP file.

Can be solved in the same way:

for zipfile in *.zip; do

unzip “$zipfile”

done

There is another way to do this with the unzip command: it can read a shell-like pattern to specify the ZIP file name. To prevent the shell from interpreting these styles, you need to use quotes .unzip (not the shell) to explain *.zip:

unzip “*.zip”

# The following seems clearer:

unzip \\*.zip

The above is a tutorial on how to extract multiple compressed files in Deepin at the same time in LinuxLinux. You don't need a tutorial to use this tutorial. One to decompress the file.

Copyright © Windows knowledge All Rights Reserved