How to zip or unzip files from the linux terminal
zip a folder in Ubuntu Linux using the cli
First install the zip command using apt command or apt-get command. Open the terminal and type the following command:
$ sudo apt install zip unzip
How do I use zip command to compress a folder?
The syntax is
zip -r filename.zip folder
zip -r filename.zip folder1 folder2
zip -r filename.zip /path/to/folder1 /path/to/file2
To create compressed archive named data.zip of data folder in the current directory, run:
zip -r data.zip data/
Verify file with the ls command:
ls -l data.zip
You can encrypt data.zip with a password by passing the -e option:
zip -r -e data.zip data/
Sample outputs:
zip command has many more options as follows:
| Option | Description |
|---|---|
| -f | freshen: only changed files |
| -u | update: only changed or new files |
| -d | delete entries in zipfile |
| -m | move into zipfile (delete OS files) |
| -r | recurse into directories |
| -j | junk (don’t record) directory names |
| -0 | store only |
| -l | convert LF to CR LF (-ll CR LF to LF) |
| -1 | compress faster |
| -9 | compress better |
| -q | quiet operation |
| -v | verbose operation/print version info |
| -c | add one-line comments |
| -z | add zipfile comment |
| [email protected] | read names from stdin |
| -o | make zipfile as old as latest entry |
| -x | exclude the following names |
| -i | include only the following names |
| -F | fix zipfile (-FF try harder) |
| -D | do not add directory entries |
| -A | adjust self-extracting exe |
| -J | junk zipfile prefix (unzipsfx) |
| -T | test zipfile integrity |
| -X | eXclude eXtra file attributes |
| -y | store symbolic links as the link instead of the referenced file |
| -e | encrypt |
| -n | don’t compress these suffixes |
| -h2 | show more help |
Compress a directory in Ubuntu Linux
The zip command syntax is as follows to compress a directory in Ubuntu Linux:
zip -r compressed_data.zip /path/to/foldername
zip -r compressed_data.zip /home/vivek/Jan-2018
zip a folder in Ubuntu Linux using the GUI method
To access and organize your files you use “Files” app (file manager)”. Use the Files file manager to browse and organize the files on your computer. Open it. Select folder name such as data and right click the “Compress…“:
Gif 01: Compressing a folder in Ubuntu Linux using GUI method i.e. Files file manager
Password protecting zip file
We can encrypt and password protect our zip file as follows:
zip -r -e output.zip /path/to/folder/
We can also use and state password on the CLI to encrypt zipfile entries:
zip -r -e -P ‘YOUR_PASSWORD_HERE’ output.zip /path/to/folder/
WARNING: The -P ‘YOUR_PASSWORD_HERE’ option to the zip IS INSECURE! Avoid it. Many multi-user operating systems such as Linux provide ways for any user to see the current command line of any other user; even on stand-alone systems there is always the threat of over-the-shoulder peeking. Storing the plaintext password as part of a command line in an automated script is even worse. Whenever possible, use the non-echoing, interactive prompt to enter passwords. And where security is truly important, use strong encryption such as Pretty Good Privacy (PGP/GPG) instead of the relatively weak standard encryption provided by zipfile utilities. See “ HowTo Encrypt And Decrypt Files With A Password Using GPG on Linux and Unix-like Systems” for more info.
Conclusion
You just learned how to compress a directory in Ubuntu Linux or Debian Linux using the zip command. The same command used to zip a folder in Linux. For more info please see this page here.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 1 comment so far. add one ↓
zip and unzip are linux commands line that often used to accomplish administrative tasks on “Terminal”. “zip” is used to compress your files into a zip archive, while “unzip” is to extract the zipped files from a zip archive 🗄️. This program is useful for packaging a set of files for distribution, archiving files, and saving disk space by temporarily compressing files or directories.
The terminal is a user interactive and outputs the results of “zip and unzip” commands which are specified by the user itself. Execution of typed command is done only after you press the Enter key. It has the power and flexibility, means that using it may be essential.
How to ZIP and UNZIP on Linux
zip unzip are typically packaged in a linux distribution for supporting system includes Debian, Fedora, Centos, OpenSUSE, Arch Linux, Ubuntu and many others, allowing modifications to the original files or directories 📁. You can easily zip or unzip files or directories on linux via terminal on your system with the following methods.
zip and unzip on linux system
ZIP on Linux
zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows, Minix, Atari, Macintosh, Amiga, and Acorn RISC OS. It is analogous to a combination of the Unix commands tar and compress and is compatible with MSDOS systems.
To zip one or more file(s) and folder(s) / directory via terminal on Linux:
- Open “Terminal” or by pressing “Ctrl+Alt+T” button
- Go to the directory (use “cd /path/directory-name/so-on”) that contains file(s) and folder(s) you want to zipped
- Use “zip” command and specify output-file-name with the folder / directory name and/or the file(s) that you want to add to the archive separated by space
- The syntax for using the zip command as shown below:
- Create a zip archive containing all the files in the current directory.
Example: zip archive.zip *
Create a zip archive containing all the files in the current directory including the hidden files (files starting with a dot).
Example: zip archive.zip .* *
Create a zip archive containing folder and multiple files in the current directory
Example: zip archive.zip Documents filename.txt image.jpg imaege.png
The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command.
UNZIP on Linux
unzip will list, test, or extract files from a ZIP archive, commonly found on MS-DOS systems. The default behavior (with no options) is to extract into the current directory (and subdirectories below it) all files from the specified ZIP archive. This is compatible with archives for MS-DOS, but in many cases the program options or default behaviors differ.
To unzip archived file via terminal on Linux:
- Open “Terminal” or by pressing “Ctrl+Alt+T” button
- Go to the directory (use “cd /path/directory-name/so-on”) that contains zip file that you want to extract
- Use “unzip” command and specify zip file in the working directory
- The syntax for using the unzip command as shown below:
- extract zipped file in the current directory.
Example: unzip archive.zip
unzip a ZIP file to a new folder name you specified in the current directory (If the folder / directory doesn’t exist, it will create one).
Example: unzip archive.zip -d new-folder
unzip a ZIP file to a different directory (in below case to Documents directory).
Example: unzip archive.zip -d /home/user/Documents
Note: You don’t always have to go to the terminal if you are using desktop Linux. You can perform zip and unzip in Graphical User Interface.
Zip is the most common feature to compress the file and transfer the files easily. With Zip, we can also send the directory directly. Also when we work in command-line server, there is no option we can zip file from the GUI. Also it is fast to zip or unzip files from command-line.
In this article, we will discuss on how to zip and unzip the files in Linux OS systems from the command line.
Zipping the files
Zip comes preinstalled in the most Linux desktop environment. You can check it from the bellow command.
You will get bellow response if zip is already installed.
Or you can also install zip with just bellow command
Ubuntu and Debian based OS distribution
CentOS and Fedora based OS distribution
To zip the file, just use the bellow command.
This will create filename.zip file.
Unzip the Zipped file
First install the unzip from the bellow command.
Ubuntu and Debian based OS distribution
CentOS and Fedora based OS distribution
The bellow command is the basic sintax to unzip the zipped file. It will unzip all the files in the currenct folder without folders.
Use the -d option if you want to unzip the files in the specifies directory.
If you want to see the content in the zipped file without unzipping, the use -l option.
This will display content of the zipped file.
If your zip file is password protected, then use the -p option with password.
If you do not want to unzip all the files and want to exclude some file, the use -x option with exclued files.
If you want to overwrite the file without asking, use -o option.
Or if you want don’t want to overwrite the file, then use -n option with file name
Unzip the Rar file
If you want to unzip the rar files, then first, you need to install unrar.
Ubuntu and Debian based OS distribution
CentOS and Fedora based OS distribution
To unzip the rar file to current directory
Check out all the options available for Unrar.
Conclusion
This way unzip will help you to work with files. If you have any question or suggestion, please feel free to comment bellow.
how to zip and unzip files and directories in ubuntu / linux using terminal. In this tutorial, you will learn how to create zip file in ubuntu / linux using terminal and how to extract or unzip files and directories using terminal in ubuntu / linux.
Note that, A ZIP file is a data container containing one or more compressed files or directories.
So, this tutorial guide will help you on how you can easily zip and unzip files and directories in linux / ubuntu system using command line. And as well as how to unzip password-protected files and directories in linux using terminal.
How to Zip UnZip Files And Directories In Ubuntu / Linux using Command Line
To learn how to zip and unzip files and directories or password protected files and directories in ubuntu / linux using command line or terminal:
- How to Zip Files And Directories
- How to Unzip Files And Directories
How to Zip Files And Directories
zip is not installed by default in most Linux distributions. So, you can easily install it using the following command.
Now open your terminal and execute the following command on it to install zip package:
- To install zip on Ubuntu and Debian:
- To install zip on CentOS and Fedora:
- To ZIP Files and Directories
To zip one file. So, you can easily zip it by using the following command:
To zip one directory. So, you can easily zip it by using the following command:
- To ZIP Multiple Files and Directories
To zip multiple file. So, you can easily zip it by using the following command:
To zip multiple directory. So, you can easily zip it by using the following command:
- Creating a Password Protected ZIP file
To create password procted zip file in linux system. So, you can execute the following command on your terminal:
The command will be prompted to enter and verify the archive password:
How to Unzip Files And Directories
un zip is not installed by default in most Linux distributions. So, you can easily install it using the following command.
Now open your terminal and execute the following command on it to install zip package:
- To install unzip on Ubuntu and Debian:
- To install unzip on CentOS and Fedora:
- To unzip Files and Directories
To unzip one file. So, you can easily unzip it by using the following command:
To unzip one directory. So, you can easily unzip it by using the following command:
- To unzip Multiple Files and Directories
To zip multiple file. So, you can easily unzip it by using the following command:
To unzip multiple directory. So, you can easily unzip it by using the following command:
Alternate way to unzip multiple files and directories in linux using command line. You can use regular expressions, as shown below:
- Unzip a ZIP File to a Different Directory
To unzip file or directories in diffrent direcoties, so you can use the following command:
- Unzip a Password Protected ZIP file or Directories
To unzip password procted file or directories in linux using command line, so you can use the following command:
Conclusion
how to zip and unzip files and directories in ubuntu / Linux using the terminal. In this tutorial, you have learned how to create a zip file in Linux using terminal and how to extract or unzip files and directories using the terminal in Linux.
Just downloaded a .zip file from the internet. I want to use the terminal to unzip the file. What is the correct way to do this?
9 Answers 9
If the unzip command isn’t already installed on your system, then run:
After installing the unzip utility, if you want to extract to a particular destination folder, you can use:
If the source and destination directories are the same, you can simply do:
You can simply use unzip .
A more useful tool is 7z , which zips and unzips a range of compression formats, notably lzma , usually the protocol offering the highest compression rates.
This command installs 7z :
This command lists the contents of the zip:
This command extracts the contents of the zip:
to extract to yourpath/somedir
If you want to extract to an absolute path, use
Using scripting tools: Perl and Python
Many answers here mention tools that require installation, but nobody has mentioned that two of Ubuntu’s scripting languages, Perl and Python, already come with all the necessary modules that allow you to unzip a zip archive, which means you don’t need to install anything else. Just use either of the two scripts presented below to do the job. They’re fairly short and can even be condensed to a one-liner command if we wanted to.
Python
If the source and destination directories are the same, you can simply do:
I prefer bsdtar to unzip / zip . For extracting, they are pretty similar:
However for zipping, bsdtar wins. Say you have this input:
and want this in the zip file:
This is easy with bsdtar :
zip does not have the -d option like unzip, so you have no way to achieve the above unless you cd first.
Here is the detailed description of options that I find useful:
Install unzip
So First of all we need to install unzip on our system if it’s not installed. unzip command is used to extract files from a ZIP archive.
Run the following command to install unzip
Now Follow the steps below:
UnZip File
OPTION 1 – If the Zip File is in the same directory/folder in which your terminal is and we want to extract it in the present working directory.
Use the following command to achieve the above described scenario
if the zip file is protected with some password, then use the following command :
Please make sure you use -P (capital P) not -p because the are different options.
OPTION 2 – If the zip file is not present in the same directory and we want to extract/unzip the file in different directory.
Use the following command to achieve the above described scenario
if we does not use option -d the file will be extracted to present working directory.
And if the zip file is password protected we can also use -P .
use tar Command in Linux / Unix
tar is an acronym for Tape Archive. tar command is used to Manipulates archives in Linux/Unix. System administrators uses tar command frequently to rip a bunch of files or directories into highly compressed archive which are called tarball or tar , bzip and gzip in Linux/Unix system.
tar Syntax
tar required Flags
tar optional Flags
Examples
Create tar Archive File by Compressing an Directory or a Single File
The terminal command below will create a .tar file called sample_dir.tar with a directory /home/codebind/sample_dir or sample_dir in present working directory.
Here’s what those flags (-cvf) actually mean
-c, –create – create a new archive
-x, –extract, –get – extract files from an archive
-f, –file ARCHIVE – use archive file or device ARCHIVE
Create tar.gz or tgz Archive File by Compressing an Directory or a Single File
The terminal command below will create a .tar.gz file called sample_dir.tar.gz with a directory /home/codebind/sample_dir or sample_dir in present working directory.
Notice that we have added extra flag -z to the command.Here’s what the flag -z actually mean
-z, –gzip, –gunzip –ungzip – Compress the archive with gzip
The command bellow will create a .tgz file. One this to notice is tar.gz and tgz both are similar.
Compressing Multiple Directories or Files at Once
Let’s say, For example we want to compress the sample_dir directory, the java_test directory, and the abc.py file to a tar file called sample_dir.tar.gz .
Run the following command to achieve the goal above.
Create .bzip2 Archive File by Compressing an Directory or a Single File
Notice that we have added extra flag -f to the command.Here’s what the flag -f actually mean
-f, –file ARCHIVE – use archive file or device ARCHIVE
Extract .tar Archive File
We can extract or untar the compressed file using the tar command. The command below will extract the contents of sample_dir.tar to the present directory.
The following command will extract or untar files in specified Directory i.e. /home/codebind/dir_name in this case.
we have added extra flag -C to the command.Here’s what the flag -C actually mean
-C, –directory DIR – change to directory DIR
Restoring a backup from a ZIP file – like the ones generated by our downloadable backup feature – can sometimes be a lengthy process. If you unzip a ZIP file locally on your computer before uploading the backup with SFTP, you’ll likely run into a bottleneck because SFTP can only transfer a limited number of files concurrently.
Fortunately, there is a faster method that can reduce the backup restore time significantly.
Instead of unzipping a ZIP file locally and uploading the unzipped folder, it’s often faster to upload a ZIP file to the server before unzipping it.
In this post, we’ll explain how to unzip a file uploaded to a server using SSH. For our example, we will unzip a backup file on a Kinsta server, but the same technique can be used to unzip any ZIP file on any Linux server.
Step 1 – Log In to Your Server with SSH
The first step is to log in to your server with SSH. To do this, you’ll need an SSH client like the built-in Terminal app in macOS and Linux or the free PuTTY client on Windows, and the SSH login details – IP address, username, password (optional), and port.
Password vs Public Key Authentication
You may be wondering why the password is optional. When logging in to your server with SSH, there are two authentication methods – password and public key authentication. The password authentication method uses a plain text password.
Public key authentication is widely regarded as a more secure alternative to password authentication. The public key authentication method requires you to generate a key pair – public key and private key. The public key is uploaded to the server, while the private key is stored locally on your computer. During the SSH login process, the cryptographic link between the two keys is verified to authenticate the user.
Kinsta users can upload SSH public keys in the MyKinsta dashboard. If you are using another server provider, we recommend taking a look at the relevant documentation on how to upload SSH keys to the server.
Now that we’ve covered the differences between password and public key authentication, let’s move on to the SSH login process. For Kinsta users, SSH login details along with the full SSH terminal command are provided in the MyKinsta dashboard.
SSH terminal command in MyKinsta.
If you are using another web host or server provider, the SSH details can typically be found in your dashboard as well.
After you have found the login details, you can log in with the following SSH command.
With the example login details below, the SSH terminal command would be ssh [email protected] -p 24910 . If you are not using public key authentication, you will also be prompted for the SSH password after executing the login command.
- IP Address: 146.148.59.197
- Username: kinstasite
- Port: 24910
After a successful SSH login, you should see something like this in your Terminal window.
SSH terminal window.
Step 2 – Install the Unzip Package (Optional)
In some Linux distributions, the unzip package is not installed by default. Kinsta users do not have to worry about installing the unzip package because it is automatically installed on all our site containers. If you are managing a server that does not have the unzip package installed, you can use the following command to install it – note that sudo level permissions are required.
Do you wish to know how to unzip files in Linux? Then you are in the right place. Find out this and much more in today’s blog.
List of content you will read in this article:
Zip is a widely used cross-platform command for data compression and archiving. Compression saves space by compressing data while archiving simplifies data transfer by merging several folders or folders into a single file. Consider the following scenario: if we need to send five files over the internet, each of which is 100 megabytes in size, sending each file one by one could take a long time. It would be much faster to copy the files if they are compressed to a size of up to 50MB and then archive all of them in a single file than if they are not compressed. Unzip is used to retrieve and decompress zipped files. Zip is the most commonly used utility for archiving and compressing files. So in this guide, we will give you brief details about zipping and unzipping the files in Linux.
Installing Zip and Unzip
Ubuntu systems come with zip and unzip packages loaded by chance. You can conveniently install these if they are absent from your setup. Using the Ctrl+Alt+T keyboard shortcut, open the Terminal. Then, in Terminal, type the following to load zip:
$ sudo apt install zip
Run the following command in Terminal to install unzip:
$ sudo apt install unzip
You will be required to zip files in the following circumstances:
- Since zipping reduces the file size, it saves disc space.
- Using e-mail to forward huge files
- Increasing the speed of uploading or copying files
- In order to conserve bandwidth
Unzipping a ZIP File in Linux
When used without any options, the unzip command removes all files from the designated ZIP folder to the current directory in its most basic form.
Let’s presume you downloaded an installation ZIP file as an example. Simply execute the following command to unzip this file to the current directory:
The Linux-style ownership details are not supported by ZIP archives. The person who executes the command owns the collected data.
The folder where you’re extracting the archive which is in zip format must have write permissions.
Suppress the unzip command’s output
Unzip prints the names of all the files it’s downloading by default, as well as a list when it’s finished.
To prevent these messages from being printed, use the -q key.
unzip -q filename.zip
Unzip a ZIP Archive to a New Location
Using the -d option to unzip a ZIP file to a separate directory from the present one:
unzip filename.zip -d /path/to/directory
To unzip an installation folder file.zip to the /var/xyz/folder, for example, run the given command below:
sudo unzip installation.zip -d /var/xyz
We’re using sudo in the command above because the user we’re logging in as normally doesn’t have write access to the /var/www directory. When you use sudo to decompress ZIP data, the extracted files and folders are owned by root.
Unzip a ZIP file that has been password protected.
If you want to unzip a protected file that is secured by a password, use the unzip command with the -P option and the required password for that file:
unzip -P ********(your password) filename.zip
Using the command line to type a password is vulnerable and should be avoided. Extracting the file normally without giving the password is a more safe choice. Unzip will ask you for the password if the ZIP file is encrypted:
[filename.zip] file.txt password:
As long as the password is valid, unzip can use it for all encrypted files.
When unzipping a ZIP file, exclude those files which we don’t want to unzip:
Using the -x alternative accompanied by a space-separated list of archive files you wish to remove from being extracted to exclude individual files or folders from being extracted:
unzip filename.zip -x file1-to-exclude file2-to-exclude
Except for the.git directory, we remove all files and folders from the ZIP folder in the following example:
unzip filename.zip -x “*.git/*”
Existing Files Would Be Overwritten
Let’s pretend you’ve already unzipped a ZIP file and you’re repeating the process:
Unzip will prompt you to overwrite only the existing file, all records, skip the extraction of the existing file, skip the extraction of all files, or rename the current file by default.
replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:
Using the -o method if you want to erase current files without prompting:
unzip -o filename.zip
With this alternative, proceed with caution. Changes to the files will be missed if you make them.
Without Overwriting Existing Files, Unzip a ZIP File
Assume you’ve already unzipped a ZIP file and made modifications to certain files, but you’ve even erased a few files by mistake. You want to retain the updates and recover the files that were removed from the ZIP directory.
Using the command -n to force unzip to skip the extraction of a file that already exists:
unzip -n filename.zip
Several ZIP Files Have To be Unzipped
Regular expressions should be used to balance different files.
If you have several ZIP files in your current working directory, for example, you can unzip all of them with only one command:
It’s worth noting that the *.zip is enclosed in single quotes. If you don’t quote the statement, the shell will enlarge the wildcard character, resulting in a mistake.
List the Zip File’s Contents all together:
Using the -l option to list the contents of a ZIP file:
unzip -l filename.zip
Conclusion
Unzip is a utility command that is used to list, test, and extract various compressed ZIP archives. The zip command is used to compress the files within the Linux operating system. Hence we have covered different operations related to unzip and understood how each varies depending on the usage of their command and wildcards.
The ‘unzip’ command supports command-line options to customize its use
- Tweet
- Share
Zipping files is an easy, efficient way to transfer data between computers and servers. When files are compressed, they not only save disk space on a local drive but also make it easier and more convenient to download files from the internet, using far less bandwidth in most cases than sending full-size files.
After you receive a zipped archive, decompress it with a single Linux command. The unzip command supports many switches to customize how the command works.
Decompress Single ZIP Files
The basic syntax for decompressing a file is:
Assume you’ve zipped an archive titled sample.zip that contains three text files. To unzip this file to the current folder, run the following command:
Unzip several files by listing them all sequentially—e.g., unzip first.zip second.zip third.zip—or by using a wildcard, e.g., unzip *.zip.
Alternatively, use the graphical user interface for your desktop environment to extract zip files. Each DE uses different approaches, but in general, a right-click on the zipped file and an uncompress or extract menu option will get you started.
Options
Use the following options to modify how the base unzip command works:
In this tutorial, we touch base on the zip and unzip commands and how they are used in Linux. Zip is a command used for creating an archive file or a zipped file. This allows you to compress your files and create more space for other files on your hard drive or removable drive. Additionally, zipping your files/directories makes them more portable and easier to upload, download or even attach and send them via email.The opposite of zipping is unzipping, and here we use the unzip command to decompress the files and access them individually.
Before start discussing zip and unzip command, let’s see how these commands can be installed from command line,
Install zip & unzip command on Ubuntu / Debian / Linux Mint
Open the terminal and run the following apt command,
Install zip & unzip command on CentOS / RHEL / Fedora
Open the terminal and execute the beneath command,
Let’s dive in and see how to zip and unzip files and directories in Linux with zip and unzip command with examples.
Zip command in Linux
Zipping a file is as easy as ABC. The syntax is quite straightforward:
$ zip OPTIONS archive.zip file
A few points to take into consideration before you proceed:
- You need to have write permissions on the file you are zipping and the directory location before zipping a file.
- When you unzip or extract a file, the user owns the file/directory.
Example 1) Zipping a single file and multiple files with zip command
Let’s assume you have a text file – reports1.txt – in your current directory and you want to zip it into an archive called reports.zip .
The command for this operation will be:
Additionally, you can zip multiple files at a go into an archive as shown:
$ zip archive.zip file1 file2 file3
Again, let’s assume that we have 3 text files reports1.txt, reports2.txt and reports3.txt. To zip them into the archive reports.zip, run the command:
Example 2) Adding a file to a zip archive (-u)
At times, you may find the need to add a file to a zip archive. To do so, use the -u flag . For example, to add another file reports4.txt, run:
Example 3) View contents of a zipped file
To view contents of a zipped file, use the command as shown:
Example 4) Zipping all the files in the current directory (* )
If you have multiple files in your current directory, you can zip all of them at a go using the wildcard symbol as shown in the syntax below:
For example, to compress all files in the home directory to home.zip archive, execute the command below. Be sure that you are working in the home directory.
Example 5) Delete a file from an archive (-d)
To remove a file from an archive, invoke the -d flag . For instance, to remove reports4.txt from the zipped file, run:
Example 6) Delete files after zipping (-m)
As you may have noted, the original files remain even after zipping or archiving them.
If you prefer you get rid of them during archival and conserve space, invoke the -m option during archival as shown:
Example 7) How to zip a directory (-r)
We have so far seen how to zip files. Often, you will be tasked with zipping directories since they take up more space for the most time.
To zip a folder, use the syntax below. The -r option zipped the folder recursively.
$ zip -r archive.zip folder
For example, to zip a directory data to an archive data.zip, execute:
As with files, you can also zip several folders concurrently as shown:
$ zip -r archive.zip folder1 folder2 folder3
Example 8) Zip a file to a different destination
To zip a file to a different destination other than the current directory simply specify the path to the zipped archive in your syntax as shown:
$ zip /path/to/destination/archive.zip file
For example, the file hello.txt is archived to file.zip file in the Documents/data/sales path.
Example 9) Zip both files and directories into their own archives
If, for whatever reason, you want to compress files and directories in the current directory into their individual zip archives, use the for loop as shown:
For example, the example below compresses the file hello.txt and folder sales into their own archives hello.zip and sales.zip respectively.
Unzip command in Linux
Let’s now shift gears and focus on unzip command. The command is used to unzip or decompress zipped files and directories. Let’s take a look at a few example usages of the command:
Example 1) How to view the contents of a zipped file (-l)
Before you are unzipping a file or directory, you can view the archive’s contents using the -l option as shown:
$ unzip -l archive.zip
Example 2) How to view detailed contents of a zipped file (-Z)
To view more detailed information about the file such as file permissions and the total size of the files in the archive, use the -Z option as shown:
Example 3) How to unzip/decompress a zipped file (-d)
In its basic and simplest syntax, unzipping a zipped file takes the syntax below and occurs in the current working directory.
Example 4) Unzip a file to another directory
To extract or unzip an archive to a different destination, use the -d flag and specify the destination path as shown
$ unzip archive.zip -d /path/to/destination/folder
Example 5) Unzip one or more files from an archive
Oftentimes, you will have a compressed file with multiple files. Instead of uncompressing the archive entirely, you may opt to just extract one or two files. To achieve this, specify the file(s) as shown in the syntax:
$ unzip archive.zip file1 file2
For example, to extract a file called hello.txt from the archive home.zip run
Additionally, you can extract the file to a different destination folder instead of the current directory. For example, the command below extracts the hello.txt file to the Documents/data/sales directory.
Example 6) Exclude certain files from being decompressed (- x)
Just as you can extract a specific file(s) from an archive, so can you exclude certain files from being decompressed. This is possible using the ( -x) option in the syntax shown
$ unzip archive.zip -x file1
For example, the example below excludes files file1.txt & file2.txt from being extracted.
Example 7) How to suppress the output of unzip command (-q)
As you may have noted, details of the unzip operation showing the location of the extraction and files being extracted is printed on the terminal. To suppress this info, use the -q switch as shown:
Example 8) How to overwrite existing files (-o)
When you run the unzip command twice in the same directory, you will be prompted whether to replace, ignore the extraction or rename the existing files as shown in the output below.
If you want to overwrite the existing file, without being prompted, use the -o option as shown:
Example 9) Unzip multiple files
Finally, to unzip multiple files at a go, use a wildcard in the syntax shown:
Take note of the single quote enclosing the *.txt parameter.
Conclusion
This brings us to the end of this guide; we have showcased the usage of the zip and unzip commands and that various options you can use. For more information about their usage, feel free to visit their man pages. We hope you have a firm grip of the usage of both commands and that you can comfortably zip and unzip your files.