- Right-click on the file or folder we want to zip.
- Select “Compress (file/folder name)” from the drop-down menu This will create a .zip file in the same directory.
Did you know Phil Katz and Gary Conway designed the ZIP file format, which supports lossless data compression and is pervasive among computer users? The “PK” in PKZIP stands for his initials, Phil Katz.
Key takeaways:
ZIP files: This is a popular format for lossless data compression, commonly used to reduce file size and bundle multiple files together.
Benefits of ZIP: It has a smaller file size for faster transfers and easier storage.
Example commands:
Zip a single file:
zip zip_file_name.zip file_name
Zip a folder recursively:
zip -r zip_folder_name.zip folder_name
Zip all files in the current directory:
zip zip_file_name.zip *
Update a ZIP file:
zip -u zip_file_name.zip file_name
Unzip a ZIP file:
unzip zip_file_name.zip
Migrating data is a common task nowadays. Imagine sending gigabytes of project files to a client over a slow internet connection. Painful, isn’t it? How can we speed up the transfer?
Internet speed might not be in our control. The other important factor determining transfer times is the data size that needs to be transmitted. Can we reduce the size of data without loss of information? Yes. Zip is a tool or method that can reduce file size and ensure everything is neatly bundled in one place. It achieves all this without any loss of information.
Learning how to compress data in Linux will simplify file management and lossless data compression and make file transfers an easy process. Linux provides the zip
command to compress the data in the .zip extension.
zip
commandThe following is the syntax for the zip
command
zip [options] zipfile_name filename
//Syntax for folder
zip [options] zipfolder_name foldername
zipfile_name
is the name of the zip file.filename
is the file that we want to compress. If we want to compress multiple files, separate the file names by space.[options]
can be as follows:-d
is used to remove files from the zip
-m
delete the original files
-u
allows us to update the zip
-r
recursively zip a directory and subdirectories
-v
is used to enable verbose mode
-x
excludes specific files during the zipping
Note: If the
zip
command is not present in your system, install it with:sudo apt install zip
Suppose we are in the folder project, and the following is the structure of the project folder.
project
│ README.md
│ file1.txt
│
│
└───folder_1
│ │ file011.txt
│ │ file012.txt
│ │
│ └───subfolder_1
│ │ file1_1_1.txt
│ │ file1_1_2.txt
│
└───folder_2
│ file2_1.txt
│ file2_2.txt
To zip files, we need the file name and the files to be included in the zip file. In the following example, the zip file name is test.zip
, and the file that we want to compress is README.md
.
zip test.zip README.md
The zip
command prints the names of the files added to the zip file and the zip
command.
adding: README.md (stored 77%)
To hide the above message, we can use the -q
option in the zip
command.
zip -q test.zip README.md
The code above will not print the files added or the compression method in the terminal.
To zip a folder, we can add the following folder name:
zip folders.zip folder_1
The code above will only zip the files in the folder_1
. The files in the subfolders of folder_1
will not be compressed.
The -r
command compresses all files in the folder and its subfolders.
zip -r folders.zip folder_1
The above code will zip all the files recursively in folder_1
, maintaining the folder structure in the zip file.
We can also combine multiple folders and files by recursively zipping files.
zip -r multiplefolder.zip folder_1 folder_2 README.md
To zip all the files in a folder, we can use *
instead of specifying all the file names.
zip allfiles.zip *
The command above will zip all the files and folders (files inside the folder are not included) in the current directory.
How to unzip files into the current directory?
The terminal given below has a folder named project
, following the same directory structure we provided at the beginning. Your task is to recursively compress all files in folder_2
and the README.md
file.
We can use -u
to:
Let’s add file1.txt and update README.md
in text.zip
.
zip -u text.zip file1.txt README.md
The zip file will only be updated if the existing file is modified or if the file is not present in the zip.
To delete a file from the compressed file, we can use -d
.
zip -d multiplefolder.zip README.md
We can use the -m
command to delete the original files after the zip file is created.
zip -m test.zip README.md
The command above will delete README.md
.
We can use -e
to create a password-protected zip file.
zip -e secret.zip README.md
The command above will provide password encryption and ask for a password.
Enter password:
Verify password:
When we need to handle large files that exceed the system's storage or transfer constraints, splitting a ZIP archive into multiple parts can be helpful. We can use the zip
command with the -s
option in Linux to create a split ZIP file, which is a ZIP archive split into multiple parts.
zip -r -s 1g zipfile_name filename
Here, -s 1g
specifies that the archive should be split into parts of 1GB each. We can adjust this size depending on our needs.
The compression level defines how optimally the files can be compressed. The compression levels range from 0 to 9, but the default is 6. To change the compression level, we can use —compression_number
.
zip -9 optimized.zip *
The code above will zip files with the optimal compression. If we give -9
, the CPU takes more time to create the zip, but the zip file size will be less. If we specify -0
, the zip files are created without compression.
The compression method can be changed using the -Z
option followed by the desired compression method. By default compression method is deflate
. Other common compression methods are store
, bzip2
and lzma
. Let’s see compression ratios, speed, and use cases for common compression methods in the following table.
Compression Methods | Compression Ratio | Speed | Use Case |
| Good | Fast | General-purpose compression |
| None | Fastest | No compression required |
| High | Slower | Large text or data files |
| Highest | Slowest | Large datasets |
In the following snippet, -Z store
sets the compression method.
zip -r -Z store zipfile_name filename
The terminal given below has a folder named project
, following the same directory structure we provided at the beginning. Your task is to recursively compress all files and folders in the project
directory. Please make sure to delete the original files when compressing them. Next, delete the README.md
file from the compressed folder and unzip the folder using unzip zipfoldername.zip
to confirm whether README.md
is deleted or not.
In summary, the zip
command in Linux provides a powerful way to manage file sizes and simplify data transfers.
Quiz!
How do we zip all files and folders in Linux?
zip allfilesfolders.zip *
zip -r allfilesfolders.zip *
zip -u allfiles.zip *
zip -d allfiles.zip *
Haven’t found what you were looking for? Contact Us