- Unzip All Files In A Directory
- 17 Answers 17
- Simultaneously Unzip or Unrar multiple files in Debian 10
- Unzip Multiple Files at Once
- Extract multiple tar.xz files at Once
- Unrar Multiple Files at Once
- Extract Multiple 7z files at Once
- Search
- About This Site
- Latest Tutorials
- How to Unzip a Zip File in Linux Command Line
- Prerequisites
- Install unzip Package in Linux
- Ubuntu / Debian
- Fedora / Redhat / AlmaLinux / Rocky Linux
- unzip Command in Linux
- Unzip a ZIP File with the unzip
- Extracting Files to a another directory
- Exclude Files
- Overwrite Existing Files
- Without Overwriting Existing Files
- List Contents of ZIP Archive
- Unzip multiple ZIP files
- Password protected zip file
- Verify ZIP Archive Integrity
- Conclusion
Unzip All Files In A Directory
I have a directory of ZIP files (created on a Windows machine). I can manually unzip them using unzip filename , but how can I unzip all the ZIP files in the current folder via the shell? Using Ubuntu Linux Server.
for windows in powershell: Get-ChildItem ‘path to folder’ -Filter *.zip | Expand-Archive -DestinationPath ‘path to extract’ -Force
17 Answers 17
This works in bash, according to this link:
Just put in some quotes to escape the wildcard:
+1 This one worked for me. I had to unzip filenames with a particular format while restricting the rest. I just kept the matching format within double quotes and it worked like charm. Output tells me the number of archives successfully processed.
The shell script below extracts all zip files in the current directory into new dirs with the filename of the zip file, i.e.:
./myfile1/files. ./myfile2/files.
Shell script:
#!/bin/sh for zip in *.zip do dirname=`echo $zip | sed 's/\.zip$//'` if mkdir "$dirname" then if cd "$dirname" then unzip ../"$zip" cd .. # rm -f $zip # Uncomment to delete the original zip file else echo "Could not unpack $zip - cd failed" fi else echo "Could not unpack $zip - mkdir failed" fi done
cd /dir/with/zips wget -O - https://www.toptal.com/developers/hastebin/suvefuxuxo.bash | bash
the ` saved my day! thanks! I am doing some loop, unzip, perform an action, copy, grep something, remove. The thing missing was how to go from file.gz to file as a variable in the bash script
This should be the ultimate answer to all unzipping anywhere and anytime, why is this not the accepted answer? 🙂
unzip *.zip, or if they are in subfolders, then something like
find . -name "*.zip" -exec unzip <> \;
Actually this will do exactly what is expected, the result of the find operation is being passed to unzip
This will extract all the zip files in current directory, what if I want the zip files (present in subfolders) to be extracted in the respective subfolders ?
Unzip all .zip files and store the content in a new folder with the same name and in the same folder as the .zip file:
find . -name '*.zip' -exec sh -c 'unzip -d "$" "$1"' _ <> \;
This will extract all the zip files in current directory, what if I want the zip files (present in subfolders) to be extracted in the respective subfolders ?
for i in *.zip; do newdir="$" && mkdir "$newdir" unzip "$i" -d "$newdir" done
This will unzip all the zip archives into new folders named with the filenames of the zip archives.
a.zip b.zip c.zip will be unzipped into a b c folders respectively.
This one worked for my use case, needs more up votes. The other approaches do not place the extracted files in a folder of the same name, as expected, but there are some cases where using this approach to separate the folders will be needed.
In any POSIX shell, this will unzip into a different directory for each zip file:
for file in *.zip do directory="$" unzip "$file" -d "$directory" done
aunpack -e *.zip , with atool installed. Has the advantage that it deals intelligently with errors, and always unpacks into subdirectories unless the zip contains only one file . Thus, there is no danger of polluting the current directory with masses of files, as there is with unzip on a zip with no directory structure.
aunpack -e -D *.zip if you want each zip to get its own output dir regardless of the number of files in it (similar to default behavior of ExtractAll in Windows)
for file in ‘ls *.zip’; do unzip «$» -d «$»; done
If by ‘current directory’ you mean the directory in which the zip file is, then I would use this command:
find . -name '*.zip' -execdir unzip <> \;
-execdir command ; -execdir command <> +
Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files. As with the -exec option, the ‘+’ form of -execdir will build a command line to process more than one matched file, but any given invocation of command will only list files that exist in the same subdirectory. If you use this option, you must ensure that your $PATH environment variable does not reference the current directory; otherwise, an attacker can run any commands they like by leaving an appropriately-named file in a directory in which you will run -execdir.
Simultaneously Unzip or Unrar multiple files in Debian 10
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:
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
$ 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
$ 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.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.
Latest Tutorials
How to Unzip a Zip File in Linux Command Line
Zip is a popular command-line tool to compress files. It allows us to create Zip archives. By default, it uses a lossless data compression technique.
Once the compressed zip file is placed in the target directory, it needs to be extracted. In this tutorial, we learn how to unzip a zip file in Linux command line using unzip command.
Prerequisites
- Unzip package installed
- If the package is not installed required a user account with sudo access
Install unzip Package in Linux
The new versions of Linux Distributions are already installed with unzip package. If not installed you may see «sudo: unzip: command not found» error when you try unzip command. You can easily install unzip package using the package manager.
Ubuntu / Debian
To install unzip on Ubuntu and Debian use the apt command:
Fedora / Redhat / AlmaLinux / Rocky Linux
On RPM-based Linux Distributions such as Fedora, Redhat, AlmaLinux, and Rocky Linux you can use the DNF package manager.
To install the unzip package using DNF, type:
You may also use yum to install unzip command:
unzip Command in Linux
The unzip command is mainly used for unzipping — extracting files from a specified zip archive file. The archive file will have a .zip file extension. You may also use unzip command to list and test the zip files. By default unzip command extracts into the current directory.
The basic syntax for unzip command:
unzip options [-d dir] [-x pattern] [-P password] file.zip
Unzip a ZIP File with the unzip
Unzipping your compressed zipped file is simple and straightforward.
For example to unzip a zip file named archived_file.zip, type:
When unzip is used without any options, it extracts to the current directory. You can use ls command to verify it.
Make sure you have write permissions on the current folder where you will extract zip files to.
By default, unzip linux will print the names and summary of the files it has extracted. Using the -q option will unzip all the files in a quiet mode:
Extracting Files to a another directory
To unzip a ZIP file to a different directory, we use the -d option followed by the absolute path:
unzip archived_file.zip -d /path/to/directory
Exclude Files
When unzipping you can exclude specific files and subdirectories. That means those files or directories will be kept as it is in the archive file, and won’t be extracted.
Use -x option to exclude files or directories while extracting.
For example to exclude files named filename1 and dual from being extracted, type:
unzip archived_file.zip -x filename1 dual
Overwrite Existing Files
Unzipping twice will prompt for overwriting existing files. This scenario happens in cases where you have deleted some extracted files and to fix that you have to extract files again.
When run unzip again, it will prompt to replace:
- [y]es: overwrite current file
- [n]o: don’t overwrite current file
- [A]ll: overwrite all of the files
- [N]one: overwrite none of the files
- [r]ename: Extract current file but give it a new name and prompt for a new name.
For unzip to overwrite the files without raising any prompt, use the -o option. This perform force unzip overwriting any existing files.
Without Overwriting Existing Files
For unzip to extract only files that are not present in the target directory use -n option.
Never overwrite a scenario that could happen when you have to keep all the extracted files with changes and restore the deleted files.
List Contents of ZIP Archive
Use -l option to list the contents of the ZIP archive file. This option helps to see what’s inside the zip file before extracting it.
For example to list the contents of the zipped archived file named archived_file.zip, type:
You can use unzip pipe through less to get the big list of files at a time.
unzip -l archived_file.zip | less
Unzip multiple ZIP files
To perform unzip multiple files which are in .zip extension, you may need to use regular expression.
The following command unzip all .zip files in the current directory:
Password protected zip file
Use -p option to unzip a password protected zip file. The -p option should be followed by the password which was used to zip the file.
unzip -P mypassword archived_file.zip
A rather more secure option would be to extract the file without providing the password. This way command will prompt for the password.
Verify ZIP Archive Integrity
Using unzip command you can test the archive files using the -t option. This helps to find any corrupt files inside the archive.
Remeber this process may take a longer time to complete as it performs cyclic redundancy checks.
Conclusion
In this tutorial, we learned how to unzip files from Linux command line. Commonly unzip command is used to extract compressed ZIP archives and list zip file contents.
For more information browse to the man unzip page. Thanks for reading, please provide your feedback and suggestions in the below comment section.
If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks