30-Jan-2024
.
Admin
Hello Dev,
Today, How to Zip a Folder in Ubuntu/Linux Using the Command Line is our main topic. I’m going to show you about How to Install the zip Utility on Ubuntu/Linux. I would like to show you How to Zip a Folder in Ubuntu/Linux. In this article, we will implement a How to Zip a Folder Excluding Specific Files or Directories.
ZIP is the most widely used archive file format that supports lossless data compression.
A ZIP file is a data container containing one or more compressed files or directories. Compressed (zipped) files take up less disk space and can be transferred more quickly between machines than uncompressed files. ZIP files can be easily extracted in Windows, macOS, and Linux using the utilities available for all operating systems.
Unzip is a utility that is not available by default on most Linux flavors but can be easily installed. By creating .zip files, you can achieve file compression similar to .tar.gz files.
This quick blog post shows you how to zip (compress) files and directories in Linux using the 'zip' command.
What Is Zip Used For?
Below are a few scenarios in which you might choose to use zip files:
When you frequently work between Windows and Unix-based systems, using zip files is advantageous. This utility not only compresses files but also serves as a versatile file packaging tool that works seamlessly across multiple operating systems.
To conserve bandwidth, zip can be utilized for file transfer between two servers, especially in cases of limited or restricted bandwidth.
The zip utility transfers files quickly by reducing file size, thereby minimizing transfer time.
Upload or download directories more efficiently by leveraging the speed advantages of zip compression.
Save disk space by utilizing the zip utility, which compresses files and directories, reducing their overall size and optimizing storage efficiency.
Learn how to unzip password-protected .zip files, ensuring the security of your compressed data.
Experience the benefits of a good compression ratio with the zip utility, efficiently reducing file sizes while maintaining data integrity.
Keep in mind, to make use of Unzip on Linux, you'll need to SSH into your virtual private server.
Install Zip on Debian and Ubuntu Systems\
Installing unzip is a straightforward process! If you're using Ubuntu or Debian, simply use the following command to install unzip.
sudo apt install unzip
Please wait for a moment while the installation completes.
To create zip files, you'll need to install zip. Execute the following command to do so.
sudo apt-get install zip
Install Unzip on Linux CentOS and Fedora
This process is straightforward and can be accomplished with the following command.
sudo yum install unzip
After the installation is complete, you can verify the path by using the following command.
which unzip
After executing the command in the command line, you should receive an output resembling the following.
/usr/bin/unzip
To confirm proper installation, use the following command. It will provide a verbose output with details about the unzip utility.
unzip -v
How to Use Zip and Unzip in Linux
Now that we've covered the installation process, let's delve into the basic uses of this utility.
zip Command
Zip is a powerful command-line utility designed to facilitate the creation of Zip archives.
The zip command follows the syntax form.
zip OPTIONS ARCHIVE_NAME FILES
To create a Zip archive in a specific directory, ensure that the user has write permissions for that directory.
Zip files do not support Linux-style ownership information. The extracted files will be owned by the user running the command. To preserve file ownership and permissions, consider using the tar command.
The zip utility is not installed by default in most Linux distributions, but you can effortlessly install it using your distribution's package manager.
Create Zip Files in Linux
The basic syntax to create a .zip file is as follows.
zip options zipfile list_Of_files
To test this, we created two files – ExampleFile.txt and ExampleFile1.txt. Now, let's compress them into a file named sampleZipFile.zip using the following command.
zip sampleZipFile.zip ExampleFile.txt ExampleFile1.txt
Using Linux to Unzip a file
The unzip command can be used without any options to unzip all files to the current directory. An example is demonstrated below
unzip sampleZipFile.zip
By default, this will be unzipped in the current folder, assuming you have read-write access to the directory.
Remove a File from a .zip File
Once a .zip file is created, you have the flexibility to remove or delete files from it. If you want to remove ExampleFile.txt from the existing sampleZipFile.zip, you can use the following command
zip –d sampleZipFile.zip ExampleFile.txt
Once this command is executed, you can unzip the .zip file using the unzip command.
unzip sampleZipFile.zip
After executing this command, you will observe that ExampleFile.txt has been removed and won't be visible upon extraction.
How to Update Zip Files
After creating a .zip file, you have the option to add a new file to an existing .zip file. For instance, if you want to add a new file, ExampleFile2.txt, to the already existing sampleZipFile.zip, you can use the following command.
zip –u sampleZipFile.zip ExampleFile2.txt
If you extract sampleZipFile.zip now, you will find that the new file, ExampleFile2.txt, has been successfully added to it.
Move a File to a Zip
You can easily move specific files to a zip file, which means that after adding the files, they will be deleted from their original directories. This is often used when dealing with large files or directories to conserve disk space. Achieve this by adding the -m option. A sample of this command would be
zip –m sampleZipFile.zip ExampleFile2.txt
Recursive Use of Zip on Linux
The -r option is employed to recursively zip files, compressing all the files within a folder. An example of a command using this option is shown below.
zip –r sampleZipFile.zip MyDirectory
In the example, MyDirectory represents a directory containing multiple files and sub-directories that are intended to be zipped.
Exclude Files in a Zip
While creating a .zip file, you can exclude unwanted files by using the -x option. Here's an example.
zip -x sampleZipFile.zip ExampleFile.txt
Here ExampleFile.txt will not be added to the sampleZipFile.zip.
Unzip to a Different Directory
If you prefer to unzip to a specific directory rather than the current one, you can achieve this by using the -d option in the unzip command. Here's an example.
unzip sampleZipFile.zip -d /usr/sampleZip/ExampleDir
Use Linux Unzip with Multiple Zip Files
If you have multiple zip files within your current working directory and want to unzip them all, you can use the following command.
unzip '*.zip'
This command will effectively unzip each individual zip file present in the current working directory.
Creating a Password Protected ZIP file
If you have sensitive information that needs to be stored in the archive, you can encrypt it using the -e option. Here's an example.
zip -e archivename.zip directory_name
Creating Split Zip File
In a situation where you want to store a Zip archive on a file hosting service with a file size upload limit of 1GB, and your archive is 5GB, consider breaking it into smaller parts using the split command before uploading.
You can create a new split Zip file using the -s option followed by a specified size. The multiplier can be k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes). This allows you to break down large Zip archives into manageable parts for storage or transfer.
zip -s 1g -r archivename.zip directory_nameCopy
The command above will continuously create new archives in a set after reaching the specified size limit, making it convenient for managing large files within the constraints of storage or transfer limitations.
archivename.zip
archivename.z01
archivename.z02
archivename.z03
archivename.z04
I hope it can help you...
#Ubuntu