Zip is a popular compression format and tool used to put files in a single and compressed file with the *.zip extension. The zip provides a lot of advantages like compression and saving space, easy and fast transfer, single file, etc. Linux distributions like Ubuntu, Debian, RHEL, CentOS, Mint, etc. provide the zip command for the compression and decompression operations. One of the most popular use cases is the compression of a folder or directory in Linux. In this case, we can use the folder and directory interchangeably.
Zip Folder using zip Command
The zip command is used to compress folders or directories. But by default, the zip command does not enable the recursive compression which is required to zip folder or directory. So the recursive compression is enabled with the -r
option. The syntax to compress folder/directory is like below.
zip -r ZIP_FILE FOLDER1 FOLDER2 ...
- -r option is used for recursive compression which is required to compress folders.
- ZIP_FILE is the output file where the compressed data will be put. The ZIP_FILE generally has the
*.zip
extension to distungish. - FOLDER1, FOLDER2 etc. are folders we want to compress with zip. Single or more folders can be specified.
In the following example, we will compress a single folder/directory into a zip file named backup.zip
.
$ zip -r backup.zip BACKUP1
Alternatively, we can specify the folders with their full or absolute paths.
$ zip -r backup.zip /var/BACKUP1
Compress Multiple Folders Into Single Zip File
The zip command can be also used to compress multiple folders or directories into a single zip file. The folders/directories we want to compress are provided as parameters to the zip command with space separation.
$ zip -r backup.zip BACKUP1 BACKUP_OLD BACKUP_EXT
Alternatively, we can specify different paths for the folders or directories to zip.
$ zip -r backup.zip /var/BACKUP1 /mnt/BACKUP_OLD ../BACKUP_EXT
Zip Folder using find and zip Command
The find command is uısed to search and files and folders with different options. The find command can be also used to search some folders or directories and zip them. The find command provides the command execution features where the zip command is executed for matches folders and directories. In the following example, we compress folders/directories where those names contain backup
.
$ find /var -type d backup -exec zip mybackup.zip +
Zip with File Manager
Another way to zip a folder directory is using File Managers. Different desktop environments like GNOME, KDE, XFCE, etc. provide file managers whereas most of them provide compression features. In the following example, we right-click to the folder we want to zip and click on the Compress
item. Alternatively, we can select multiple folders to compress them into a single zip file.
The compress item opens the following screen where the compression algorithm can be selected. The .zip
is the default compression algorithm where we also want. Just click on the Create
button on the left upper corner to zip folder backup.