How to Zip a Folder in Linux: ZIP command in Linux

For Linux users, compressing files and folders is a common task that makes managing, sharing, and storing large amounts of data easier. We’ll demonstrate How to Zip a Folder in Linux in this tutorial. This approach is simple and effective, making it ideal for both novice and seasoned users.

How to Zip a Folder in Linux: Options in ‘Zip’ command in Linux

Options                   DescriptionSyntax
-d(Remove files from the archive):
This option allows you to remove specific files from a zip archive. After creating a zip file, you can selectively remove files using the -d option.
zip -d [file_name.zip] [files_name]
-u(Update files in the archive):
You can update files in an existing zip archive by using the -u option. To update or add new files to the archive, you can provide a list of files. Only when the updated version is more recent than the one that is already in the zip archive does the update take place?
zip -u [file_name.zip] [files_name]
-m(Move files into the archive):
You can transfer specific files into the zip archive by using the -m option. After the zip archive is created, this operation also removes the target directories or files. A directory is also erased if, after the files are removed, it is left empty. Proceed with caution when using this option as it deletes the input files permanently.
zip -m [file_name.zip] [files_name]
-r(Recursively zip a directory):
You can recursively zip a directory and its contents by using the -r option. In the resulting zip archive, it contains every file found in the designated directory and all of its subdirectories.
zip -r [file_name.zip] [directory_name]
-x(Exclude files from the zip):
You can prevent certain files from being included in the zip archive by using the -x option. When you want to zip every file in a directory but keep some unwanted files out, this is helpful.
zip -r [file_name.zip] -x [directory_name]
-v(Verbose mode):
The verbose mode, which provides diagnostic information during compression, is enabled by the -v option. It provides verbose diagnostic information about the zip file structure along with a progress indicator. When used independently, it prints information about the target environment and the zip executable along with a diagnostic screen.
zip -v [file_name.zip] [file_name]

Zipping files and folders

zip test.zip README.MD

The zip command outputs the compression technique and the file names that have been added to the zip file. A new zip file will now be produced.

adding: README.md (stored 77%)

The -q option can be used to conceal the message above.

zip -q test.zip README.md

The added files and the compression technique won’t be printed to the terminal by the aforementioned code.

Rather than specifying each filename, we can use * to zip every file in a folder.

zip allfiles.zip *

The aforementioned command will zip every file and folder in the current directory (files inside folders are not included).

The following folder name can be added to a folder to zip it up.

zip folders.zip folder_1

Only the files in folder_1 will be zipped by the code above. The files within folder_1’s subdirectories won’t be compressed.

Using the -r command, which will compress every file in the folder, we can add every file to the folder and subfolder.

zip -r folders.zip folder_1

The code mentioned above will recursively zip every file in folder 1. The zip file preserves the folder hierarchy.

By recursively zipping files, we can also merge several folders and files.

Deleting a file from a compressed file

We can use -d to remove a file from the compressed file.

zip -d multiplefolder.zip README.md

Removing files after zipping

Once the zip file is created, we can use the -m command to remove the original files.

zip -m test.zip README.md

Creating a password-protected zip file

To create a password-protected zip file, use -e.

zip -e secret.zip README.md

Conclusion

How to Zip a Folder in Linux, File management is made easier by zipping folders in Linux using the zip command, which is simple to use and effective. Users can easily customize their zip operations with options like -e for encryption, -m for file moving, and -r for recursion. This adaptability improves workflow for both inexperienced and seasoned users.

Read More

Leave a Comment