- How to Zip and Unzip a directory and its files in Linux [duplicate]
- 3 Answers 3
- Unzip all zip files in a target folder?
- 2 Answers 2
- 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
How to Zip and Unzip a directory and its files in Linux [duplicate]
I am a newbie in Linux.Whats is the complete process to zip and unzip a directory and its files.Please mention if any installation has to be done.
3 Answers 3
You say you are a newbie. Maybe you got Windows-experiences. If you do not have the packages installed yet, you need to
sudo apt-get install zip gzip tar
first (or by a graphical pkg-manager).
Then, for an entry it would be the easiest way to use zip/unzip:
zip -r my_arch.zip my_folder
Zip stores relative path names by default. There are several parameter-options available for zip. For that read: the manual (man zip). For a starting this will do.
Most often you will see .tar.gz endings in linux-world. That’s the product of two tools: TAR (the tape archiver) and GZIP (the GNU-Zip). Tar has got the call option to automatically gzip/gunzip files after «taring».
tar -cvzf may_arch.tar.gz my_folder
- -c means «create»
- -v means «verbose» (sometimes bothersome and slowing down. )
- -z means «use (GNU)zip»
- -f XYZ declares the name of the output file. (You should chose a helping name like XYZ.tar.gz)
There may also be .tar.bz2 endings. This is the product of the -j parameter instead of the -z parameter: you will choose compression with BZIP2 (-> man bzip2).
To extract you simply use -x (eXtract) instead of -c (Create):
You can use the zip and unzip command line utilities. These can be installed by running
sudo apt-get install zip unzip
I know several ways, but since you’re new on linux. So I’ll tell you how to zip a file using GUI method (the easiest way).
- Create a new folder and fill it with anything you want, for example many of file (In my case, I’ll fill it with theme folder) :
- Right click the folder you want to zip and select «Compress. » option :
- You can choose which file format you want by clicking combobox next to «Filename» textbox. Also you can set the folder you want to zip location.
Clicking «Other options» will lead you to password section, in other word you can set password for your preferred file so someone (include you) have to enter password before unzip the file.
Unzip all zip files in a target folder?
I have a curl cronjob which periodically downloads a bunch of zip files into a target folder. Like this:
curl www.zipdownloadfile.com/file.zip > file.zip
Now I need to have Ubuntu unzip all of the zip files in that target folder. What’s the proper command which will unzip all of the files in a target destination/folder? Thanks
2 Answers 2
thats your command man this will unzip all zip file in a destination file
find /path-to-folder -name '*.zip' -exec unzip -d destination_folder <> +
The issue here is that unzip *.zip doesn’t work for some reason. I guess the unzip doesn’t accept wildcard path arguments for files.
Nux’s answer is good but I use a slightly different version of the find command. If you cd into the folder you’ve downloaded all the zip files then you can just run:
find . -type f -name «*.zip» -exec unzip <> \;
This will search the current working directory and all sub-directories recursively with . , then find only files, -type f , then search for anything named *.zip with -name and it will execute unzip exec unzip and substitute the result of the search which is the full file name and place it with <> after the unzip command as the argument. \;` is just magical syntax to say ‘I’m done the command is finished.’.
This still worked for me in Ubuntu 16.04 to unzip all files in the current directory after downloading a huge number.
A similar command can also used to unrar a set of rar files you’ve downloaded simultaneously as well:
find . -type f -name «*.rar» -exec unrar x -e <> \;
The above magical command will search all files in the current directory and all folders and files recursively in sub-directories to find all files that end in .rar and then unrar them directly to the current working directory.
Let’s say you want to unpack a bunch of stuff that you’ve just recently downloaded:
Fri Nov 17 10:56 AM Downloads: tree . . ├── download1 │ ├── show01.r00 │ ├── show01.r01 │ ├── show01.r02 │ └── show01.rar ├── download2 │ ├── movie01.r00 │ ├── movie01.r01 │ ├── movie01.r02 │ ├── movie01.r03 │ └── movie01.rar └── download3 ├── program01.r00 ├── program01.r01 ├── program01.r02 ├── program01.r03 └── program01.rar 3 directories, 14 files
This is only a small example you could have 20 or even 40 different foldedrs each containing their own master .zip file or .rar file that needs to be unrar’d. Doing a CD into each individual download directory to use ‘unrar’ is both time consuming and wasteful. Just use the find command!
sudo apt-get install unzip sudo apt-get install unrar cd ~/Downloads find . -type f -name "*.rar" -exec unrar x -e <> \; find . -type f -name "*.zip" -exec unzip <> \;
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