How to unzip a zip file in Linux to a folder. Read this post to learn about unzip command in Linux with examples to extract zip file in terminal.
To use the unzip command, first install the unzip utility using the following command:
sudo apt-get install unzip
Unzip command is used to extract the .zip file only and by default it extracts the content in the current directory. When the zip file contains more than one file, it will do the operation with all the files.
If you use it to extract a zip file without any option (command line arguments), it will extract all the files in the current directory.
The unzip command has a really simple syntax (you can see it below).
unzip [option] zip_file
Note: By default, unzip prints the names of all the files it’s extracting and a summary when the extraction is completed.
When you run the unzip command it gives you the output message of the unzip command with the number of files unzipped. If you do not want any output messages, you can use the -q switch to not print any of these extraction messages.
unzip -q filename.zip
The command option -q allows to perform operations quietly. The -q[q] options suppress the printing of some or all of these messages.
Unzip Command In Linux With Examples
1. Unzip to a directory
The expected behavior is that you should have the files extracted to a certain directory, normally with the same name as the zip file. You can specify the target directory where you want to extract the files.
unzip -d target_directory zip_file
If the target directory doesn’t exist, it will be created. Also, you can also put the target directory at the end but not all options can be added at the end.
By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory.
2. Extract into the current directory only
Extract the files from archive hope.zip into the current directory only, regardless of the archive’s internal directory structure.
unzip -j hope.zip
The command argument -j denotes that the archive’s directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one).
3. Overwrite all the files without prompting
If there are already files with the same name in the directory where you are extracting the files, you’ll be promoted for each such files. You can force overwrite all the files with option -o.
unzip -o -d target_directory zip_file
The command argument -o denotes overwrite existing files without prompting. This is a dangerous option, so use it with care.
4. Do not overwrite any files -n
If you don’t want any existing file to be overwritten by newly extracted files, use the -n option (stands for never overwrite).
unzip -n -d target_directory zip_file
The argument -n is to never overwrite existing files. If a file already exists, skip the extraction of that file without prompting.
By default, unzip queries before extracting any file that already exists; the user may choose to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all existing files, or rename the current file.
- -a: make unzip auto-convert text files by default.
- -L: make it convert filenames from uppercase systems to lowercase.
- -C: make it match names case-insensitively.
- -q: make it quieter.
- -o: make it always overwrite.
- -n: never overwrite files as it extracts them.
5. See the content of the zip file without extracting
You can use unzip command to see the content of the zip file without extractin. This is a very useful method to see what the zip file contains before you extract it. Use the unzip command with -l option and it will show you the content of the zip file.
unzip -l zip_file
6. Password-Protected ZIP
To unzip and extract a password-protected ZIP file, use the unzip command with the ‘-P‘ option to specify the password. The following syntax is used:
unzip -P password filename.zip
Let us understand it by an example. Suppose we want to unzip ‘example.zip‘ which is protected with the password ‘pass123.’ In this case we will use the following syntax.
unzip -P pass123 example.zip
Those were some of the most common examples of the unzip command in Linux.