Signup/Sign In

How to zip a file and unzip in Linux with Examples

Posted in Tricks   LAST UPDATED: JULY 12, 2023

    The zip archive format is a compressed file format that supports lossless compression. You can compress a file or a complete directory into a zip archive.

    The zip archive format is supported by almost every operating system including the most popular ones like Windows OS, Linux OS, macOS, etc. In this tutorial, we will learn how to install the zip and unzip utilities in our Linux machine and then we will learn the various commands that we can use to zip and unzip files and directories with examples.

    You need the sudo or root privilege for installing zip and unzip utilities on your Linux machine.

    Install Zip and Unzip on Linux

    Linux operating system has many flavors like Debian, CentOS, Fedora, Ubuntu, etc. Different operating systems have different ways of installing utilities.

    Ubuntu and Debian use apt to manage software installation and update. The below-specified command will install zip and unzip commands on your Ubuntu or Debian machine:

    sudo apt-get update
    sudo apt-get install zip unzip -y

    To install zip and unzip tool/commands on CentOS or Fedora machines, we will use the yum utility. Below is the command which can be used for the installation:

    sudo yum update
    sudo yum install zip unzip

    After running the above commands it should take 10-20 seconds for successful installation. Once the installation completes, you can run zip or unzip command to see if they are installed or not.

    How to Zip file or directory in Linux

    Following is the syntax for using the zip command to zip file in Linux:

    zip [options...] name-of-file.extension [FILES...]
    • This is the basic syntax of using the zip command to create a zip archive of a file in Linux.

    • We can provide different options for the zip command along with a name for the zip archive that will be created followed by the files that you want to archive.

    • You can archive a single file, multiple files, or a complete directory using the zip command.

    1. Zip multiple files in Linux

    If we have multiple files like file1.txt, file2.csv, etc, that we want to compress inside a single zip archive file then we can use the following command:

    zip myarchive.zip file1.txt file2.csv

    This command will create a zip archive named myarchive.zip with the two files inside the zip file.

    2. Compress(zip) a Directory in Linux

    To compress a directory along with its contents(files, sub-directories, etc), we use the zip command along with the -r option which means that all the files inside the directory, along with the files present in its sub-directories recursively will be included in the zip archive file.

    For example, if we have a directory /home/studytonight which has some files, and some directories with further files and more subdirectories, the following command can be executed to zip the entire content into a single compressed zip archive file with the name studytonight.zip:

    zip -r studytonight.zip /home/studytonight

    In the above command we have specified the complete path to the directory which we want to zip, in case some files are present in that directory, which means you want to zip all the files present in the current directory, then you can execute the following command:

    zip -r somearchive.zip *

    The above command will create a zip file compressing all the files present inside the current directory. Here, *(asterisk) refers to all the files present in the current directory.

    How to Unzip file in Linux

    To unzip files from a zip archive we will use the unzip utility. Following is the basic syntax of the unzip command:

    $ unzip [OPTIONS] [ZIP_ARCHIVE] [FILES_TO_PROCESS] [-x FILES_TO_EXCLUDE]

    Just like the zip command, we have many different options to use along with the basic unzip command. Also, along with providing the name of the zip archive that we wish to unzip, we can provide names of files to unzip or to exclude from the unzipping process. Let's learn how to use the unzip command with a few examples.

    1. Verify the Validity of a Zip archive

    Before unzipping any zip archive, its good practice to first validate the zip archive. Let's say you have received or you have downloaded a zip file somearchive.zip from the internet and you want to validate it before unzipping its content, you can do so using the unzip utility itself, by executing the following command:

    $ unzip -tq somearchive.zip

    For a valid zip archive file, the output of the above command should be,

    No errors were detected in the compressed data of somearchive.zip

    2. List all the files in the Zip in Linux

    Once you have validated a zip archive file, you can even list all the files and directories present inside the zip archive without extracting them.

    If we use the -l option along with the unzip command, we will get the complete list of all the files present inside the zip archive file, along with directories, sub-directories, and the files inside these directories, recursively.

    For an archive file with name somearchive.zip, you can execute the following command to list down the name of the files and directory inside this zip,

    unzip -l somearchive.zip

    3. Unzip a Zip file in Linux

    To unzip a zip file in Linux with the name somearchive.zip, we can run the following command:

    unzip somearchive.zip

    If you want to unzip the content of the zip archive file into a specific directory, you can do so by using the -d option along with specifying the name of the directory.

    In the command below, we have specified that the content of the zip archive studytonight.zip should be unzipped in the directory /home/studytonight.

    unzip -d /home/studytonight studytonight.zip

    4. Unzip an archive except for a few files

    If you want to exclude some files present in the zip archive during unzipping you can specify the file names to exclude from unzipping using the -x option while executing the unzip command,

    unzip smearchive.zip -x notrequired.txt

    Conclusion:

    In this tutorial, you learned about the zip and unzip utilities present in the Linux operating system, which you can use for creating zip archive files or unzipping zip archive files.

    Creating zip archive files for packaging is a good way to compress files and directories into a single file, the process of zipping is lossless and makes it easier to share as there is a single compressed zip archive file.

    You may also like:

    About the author:
    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS