Do you know that the Zip algorithm was first released in 1989?
Zip remains a popular compression format and is now a native component of major operating systems. As such, you can find an unzip utility in all Linux distributions.
The unzip command in Linux has evolved from a simple archive unpacking command to a powerful utility that offers excellent control over how you pack and unpack archives. You can use several options and flags to use the utility from the command line and in shell scripts.
In this detailed tutorial on the unzip command in Linux, we’ll explore the utility, how to install it, its syntax, and the arguments you can use to carry out various commands. You’ll see some practical examples of how the command offers archive and file management flexibility.
Let’s start with the Zip algorithm’s benefits for building archive files.
Table of content
Why Should You Zip to Archive Files?
The Zip algorithm and the unzip command in Linux offer the following benefits to the users:
Disk Space Optimization: The Zip algorithm reduces the size of the files in an archive without any significant loss, resulting in a single file that is much smaller in size than the size of all the files in the archive. This frees up disk space on your system.
Easier Transfer: Zipped archives are usually small enough to be transferred over email and popular chat platforms. Users can transfer a large number of files in a compressed zip archive without worrying about missing files or corruption.
Faster Transmission: An additional benefit of Zip archives is the time required to transmit over the Internet. Since a zipped archive can contain multiple files, you can technically transfer multiple files in the time required to transfer a single file.
Secured Archives: You can optionally secure the archive with passwords that add a security layer to the archive. In addition, you can combine the unzip command in Linux with security algorithms to enhance archive security.
Prerequisites to Using the unzip Command in Linux
Here’re the prerequisites you need to satisfy before you can use the unzip command in Linux:
1. A server running a Linux distro such as Ubuntu, Debian, Fedora, or CentOS.
2. Knowledge of the Linux Command-Line Interface (CLI). We recommend using the unzip command on the CLI to explore the command’s potential.
3. You should know how to use a package manager: Common options include APT (Advanced Package Tool) for Debian-based systems, YUM (Yellowdog Updater, Modified), DNF (Dandified YUM) for Fedora and, CentOS and Pacman for Arch Linux.
What is the unzip Command in Linux?
On Linux systems, the unzip command is a file packaging utility that decompresses files and directories in your filesystem. Users typically use the CLI to call up the utility for managing archives with the .zip extension.
The unzip utility is often native to popular Linux distributions because of its proven track record and ease of use. The command is easy to use and extracts files from the ZIP archives to the indicated directory. However, the default behavior is to extract all files from the provided ZIP archive into the current directory.
A zip archive contains the compressed contents of the included files and essential information like names, relative paths, dates, and last modification time.
The zip algorithm works best with text files, with compression ratios between 2:1 and 3:1. The zip command uses a deflation-based compression method. The Zip algorithm is versatile and can be used to archive files, package a group of files for distribution, and temporarily compress unused directories or files to free up disk space.
How to Install unzip?
As mentioned earlier, the unzip command often comes preinstalled in most Linux variants. However, if that’s not the case, you can always install it using the following process.
Install unzip on Debian-based Linux Distributions
Use the following command to download unzip on Debian-based Linux distributions such as Kali, Ubuntu, Deepin, ParrotOS, Mint, MX Linux, Pure OS, AntiX, etc.
# sudo apt install unzip
Install unzip on Fedora-based Linux Distributions
Use the following command to download the unzip utility on Fedora-based Linux distributions, like Berry Linux, Qubes, Clear OS, Korora, Hanthana, and Archetype:
# sudo yum install zip
The Syntax of Linux unzip Command
The basic and straightforward syntax of the unzip command is this:
# unzip <options> <filename(s)>
Let’s explore the two fields in the unzip command in some detail:
This field accepts several arguments that affect the unzip command’s behavior, output formatting, and printing requirements. The unzip command offers multiple options to fine-tune the performance and output of the command.
This field takes a single or multiple file or directory names. This field is very flexible, and you can enter several files or directories at once to compress them into a single archive.
How to Use the unzip Command in Linux
After that brief introduction to the basic syntax, let’s explore some practical uses of the unzip command in Ubuntu. Note that the following commands work with all Linux distros with minimal changes. We highly recommend checking your distro’s documentation for details.
Extract a Zip File
Execute the unzip command with the filename to extract all the files from the zip archive into the current directory:
As you can see, the command gives a short progress update that mentions the filenames as the command unpacks them.
Unzip the File(s) to a Specific Directory
If you wish to extract the contents of a zip file to a different directory than the current one, run the unzip command with the following syntax:
# unzip redswitches.zip -d /home
This command unzips the archive contents to the /home directory. As you can see, if a filename already exists in the target directory, the command offers you the option to replace or rename the file.
Skip Some Files When Extracting a Zip File
You can exclude specific directories and files while extracting files from a Zip archive by pairing the unzip command with the “-x” option:
# unzip redswitches.zip -x "sample.txt"
As you can see, the command doesn’t indicate the operation’s success.
Overwrite Existing Files
Consider the following scenario:
You are extracting the files of an archive with the following command:
# unzip redswitches.zip
By default, the unzip utility will ask whether you want to rename the current file, rename all files, skip extraction of all files, overwrite only the current file, or overwrite all files.
You can skip this dialog and directly overwrite the existing files in the target directory with the -o option:
# unzip -o redswitches.zip
Important: We highly recommend taking care when using this option because any modifications you make to the files are permanent and cannot be reversed.
List the Contents of a Zip File
Use the -l option with the zip command to read a list of a zip file’s contents:
# unzip -l redswitches.zip
You can see the test.zip folder’s files as the output of the command:
Unzip Multiple Zip Archives
The directory /Documents/zipped contains multiple compressed zip files in this example scenario. Here’s how you can uncompress these archives.
First, navigate to the source directory with the cd command:
# cd zipped
Now, to extract the files from the archives in the directory, use the following command:
# unzip “*.zip”
The wildcard character is used to apply the command on all files with the ZIP extension. The command locates and unzips all zip files.
How to Unzip Password-protected Files
Password-protected zip archives are commonly used to deliver files to specific users. Software companies often use these protected archives to ensure only the users with the password can extract and use the archive’s content.
You can use the unzip command in Linux with the -P option to extract files from password-protected archives. The basic syntax of the command is:
# unzip -P <password> <filename.zip>
Other Options for the Linux unzip Command
The unzip command provides many options, as is typical for Linux utilities. Let’s quickly look at some of the essential unzip command options.
This option extracts only files that already exist on the disk and are more recent than the on-disk copies.
With this option, the files are extracted to standard output. Usually, the files are always extracted in binary format when the option “-p” is used, and the file data is sent to standard output (stdout).
This option extracts specified files into memory and compares the expanded file’s cyclic redundancy check (CRC) value to the stored CRC value of the original file.
With this option, the most recent file in each archive is used as the standard date for the archive or archives.
This option is used to extract the latest version of the files and create new files if they’re not there in the first place.
The option makes the output verbose and helps the user understand what is happening.
This option affects text files in the archive. The text files are automatically extracted with necessary conversions to line endings, end-of-file characters, and the character set when enabled.
This option disregards the filename-related information in the macOS-specific additional fields. Instead, the most compatible filename in the entry’s generic header is used.
This option changes any filename created on a file system or operating system that only supports capital letters.
This option replicates the Unix “more” command by sending all output through an internal pager.
When enabled, the existing files are never overwritten. The -n option skips the extraction of a file if it already exists without asking.
With this option, encrypted zip file entries are decrypted using a password. We recommend caution when using this option.
This option suppresses most errors that could occur during the extraction process. The output is also in silent mode and remains blank.
With this option, the spaces in filenames are changed to underscores. This is an excellent option if you’re working on a system that doesn’t support spaces in filenames. This option extracts filenames conveniently, with underscores rather than spaces.
This option keeps the version numbers for the files.
This option shows the unzip command’s help menu.
The unzip command in Linux is very flexible – you can use the basic version to extract the files from a ZIP archive or use the option flags to customize the output and actions of the utility. The zip algorithm has stood the test of time and efficiency, and the unzip utility works great from the command line.
If you are using Linux as the OS for your projects, you need dependable infrastructure that keeps pace with the growth of your business. Users can customize our bare-metal servers to support various projects, from SaaS apps to corporate portals. Get a free consultation from our support engineers to give your projects the best possible foundations.
Q1: What is the “unzip” command in Linux?
The “unzip” command is a utility in Linux that allows users to extract files from ZIP archives.
Q2: How do I use the “unzip” command?
To use the “unzip” command, specify the ZIP file you want to extract. For example, running “unzip archive.zip” will extract all files from the “archive.zip” file into the current directory.
Q3: Can I extract specific files or directories using the “unzip” command?
Yes, you can extract specific files or directories by providing their names as arguments after the ZIP file name. For example, “unzip archive.zip file1.txt dir/” will extract “file1.txt” and the “dir” directory from the “archive.zip” file.
Q4: How can I overwrite existing files during extraction?
By default, the “unzip” command does not overwrite existing files. However, you can use the “-o” option to force the extraction and overwrite any existing files with the same names.
Q5: Can I list the contents of a ZIP archive without extracting them?
Yes, you can use the “-l” option with the “unzip” command to list the contents of a ZIP archive without extracting them. This can help you preview the files before extraction.
Q6: Can password-protected ZIP archives be extracted with the “unzip” command?
If a ZIP archive is password protected, the “unzip” command will prompt you to enter the password during extraction. Alternatively, you can use the “-P” option followed by the password as a command-line argument.
Q7: Can more detailed information be displayed during the extraction process?
Yes, the “-v” option enables verbose mode, providing more detailed output during extraction. This can be useful for tracking the progress and seeing which files are being extracted.
Q8: What are some everyday use cases for the “unzip” command?
The “unzip” command is commonly used for tasks like extracting software packages, downloading and extracting files from the Internet, or simply managing ZIP archives in a Linux environment.
Q9: Are there any alternative commands or tools for handling compressed files in Linux?
Yes, Linux provides several other commands and tools for handling compressed files. The list includes “tar” for creating and extracting tar archives, “gzip” for compressing and decompressing files, and “7z” for working with various archive formats.