How to ZIP and UNZIP Files in the Linux Terminal

ZIP is the most commonly used archive file format that supports lossless data compression. A .zip file is a data container that contains one or more compressed files or directories. Let’s learn how to ZIP (compress) and UNZIP (decompress) files in Linux using the command line.

How to Install ZIP and UNZIP in Linux

Before you can compress and decompress files, you need to ensure the utilities are installed on your system.

On Debian and Ubuntu, you can use the following command:

sudo apt-get install zip unzip

On CentOS and Fedora, you can use the following command:

sudo yum install zip unzip

How to ZIP Files and Directories

To ZIP one or more files, you need to use the zip command followed by the name of the archive you want to create, followed by the names of the files you want to add, separated by spaces.

zip archivename.zip filename1 filename2 filename3

To ZIP one or more directories recursively, use the -r flag. This tells the command to traverse the whole directory structure and include all subdirectories and files.

zip -r archivename.zip directoryname1 directoryname2

You can even mix files and directories in the same command:

zip -r archivename.zip filename1 directoryname1

How to UNZIP Files

To UNZIP an archive in the current directory, use the unzip command followed by the archive’s name.

unzip archivename.zip

If you want to extract just a single file from the archive, specify the file name after the archive name:

unzip archivename.zip filename1

If you want to extract the files into a specific destination directory rather than the current one, use the -d flag followed by the path:

unzip archivename.zip -d ./destination_directory/

Leave a Reply

Your email address will not be published. Required fields are marked *