How to Unzip Files in Linux?

Posted in

How to Unzip Files in Linux?

Maaz Bin Asad
Last updated on June 9, 2022

    Zip is a common file format for archived files and perhaps the most popular one. Other formats such as tar.gz, tar.bz2, etc., are also used, but zip still tops the charts. How to unzip files in Linux? It's simple. Use the zip commands in Linux to perform lossless data compression of directories and files. In simpler words, a zip file can be considered as a data container that has compressed directories or files stored inside it. In this tutorial, we will use the unzip command in Linux to extract zipped directories and files. Before we proceed, please make sure that you have access to a Linux machine, Linux terminal, and sudo privileges. Also, it’s always risky when we work with sudo privileges in a Linux machine as you have access to all the configuration and system files, and if you mess up with them, your whole system might crash. So we urge you to run the below-mentioned commands with utmost care. Also, to use the unzip command, it’s mandatory to have the unzip package installed on our Linux machine. You can simply use the package manager of any popular Linux distribution you are running to install the unzip package. However, most of the Linux distributions come pre-installed with this package.

    $ sudo apt-get -y install unzip

    $ sudo apt-get -y install unzip If you are using Fedora, CentOS, etc. and you have yum package manager, you can use:

    $ sudo yum install unzip

    How to Unzip Files in Linux?

    Before we move ahead, please note that the zip files in Linux don’t follow the traditional ownership format. Once you have extracted the files and directories from a zipped file, it belongs to the user that has run the unzip command. Also, you should have to write privileges on the directory where you wish to unzip the files. When we run the unzip command without any options, in the most native form, it simply extracts all the files and directories inside the current directory that you are in.

    $ unzip archive.zip

    $ unzip archive.zip

    Output Suppression

    You can see that the unzip command enlists the details of the files that it is extracting from the archived file. To avoid printing the details and suppress the output, we can simply use the quiet ( -q ) option along with the unzip command.

    $ unzip -q archive.zip

    $ unzip -q archive.zip You can see that the output has now been suppressed.

    How to Extract Files to a Different Directory in Linux?

    By default, the unzip command will extract all the files and directories inside the zipped file to the current directory. If you want to extract them to a different directory, you can use the -d option and specify the path to the destination directory. Please note that if you don’t have write access to the destination directory, you can use the sudo command to get the root privileges.

    $ unzip archive.zip -d ~/Documents

    $ unzip archive.zip -d ~/Documents

    How to Extract a Password-Protected File in Linux?

    If you want to unzip a file that is password-protected, you can use the -P option along with the unzip command.

    $ unzip -P <password> <zipped-file>

    $ unzip -P <password> <zipped-file> However, typing the password as plain text in the terminal (command line) could be insecure. What you can do is simply extract the zipped file in a normal manner. When it tries to unzip the files and finds a file with a password, it will automatically prompt you to input the password.

    $ unzip archive.zip

    $ unzip archive.zip

    Excluding Files During Extraction

    If you want to exclude certain files or directories while extracting a zipped file, you can use the -x option to include the files or directories that you want to exclude, separated by spaces. Let’s try to exclude all the text files while extracting the zipped file.

    $ unzip archive.zip -x “*.txt”

    $ unzip archive.zip -x “*.txt” You can see that the command has excluded all the text files.

    How to Unzip Multiple Files in Linux?

    If you want to extract or unzip more than one compressed zipped file, you can simply do so using this command:

    $ unzip ‘*.zip’

    $ unzip ‘*.zip’ You can see that both the zip files have been unzipped successfully.

    Listing the Contents Without Unzipping

    Before unzipping, if you want to list all the contents of the zipped file, you can use the list ( -l ) option along with the zip command . You will get parameters such as the size, timestamp, and path.

    $ unzip -l archive.zip

    $ unzip -l archive.zip

    Overwriting Files

    If you have unzipped a file once and tried to unzip the same file again, you will be prompted whether you want to replace, rename files, skip the extraction of all or current files, etc. If you don’t want the prompt, you can simply use the -o option to overwrite the files.

    $ unzip -o archive.zip

    $ unzip -o archive.zip

    Wrapping Up!

    To conclude this detailed and comprehensive article, we discussed how to unzip files in Linux using the unzip command. We saw multiple examples pertaining to the different versions of the Linux command, such as extracting a single file, multiple files, suppressing the output using the quiet option, changing the destination directory using the -d option, extracting or unzipping password-protected files, listing the contents before extracting them, overwriting already extracted files, and so on. To gain mastery over Linux, you can buy the course from here. Happy Learning! People are also reading:

    Leave a Comment on this Post

    0 Comments