Linux how to zip folder
Linux how to zip folder
[ZIP File/Zip Folder Linux] Zip Command in Linux with Examples
Are you looking for ZIP commands in Linux that can help you to zip files Linux and zip folder Linux? Then read our blog post to have a detailed understanding of it.
List of content you will read in this article:
The most popular lossless data compression archive file format is Zip. A Zip file is a data object that encapsulates one or more compressed files and folders. Uncompressed files take up more disc space and take longer to transmit from one device to another. Zip files may be readily extracted on Microsoft Windows, macOS, and Linux operating systems using the appropriate software for each platform. In this guide, you’ll learn how to use the zip command in Linux with examples [ZIP File Linux/Zip Folder Linux] to compress files and directories.
Zip command in Linux with examples
Zip is a command-line tool that aids in the creation of Zip archives.
The syntax for the zip command is as follows:
zip Option Archive-name File
A user must have write permission on a directory in order to build a Zip archive in that directory.
Zip files do not allow ownership information in the Linux way. The person who executes the command owns the extracted files. Use the tar command to keep file ownership and permissions intact. Most Linux distributions do not include the zip program by default, but you may simply install it using your distribution’s package management.
Zip may be installed on Ubuntu and Debian systems by the following command:
sudo apt install zip
Zip may be installed on CentOS and Fedora systems by following this command: CentOS and Fedora
sudo yum install zip
How to Compress Files and Directories in Linux [zip file and folder in linux]
To zip file/folder in Linux, you have to divide the files to be included in the pack by a space, as demonstrated below:
zip xyz.zip file1 file2 file3
Output:
adding: file1 (deflated 53%)
adding: file2 (stored 0%)
adding: file3 (deflated 48%)
zip xyz.zip file1 will produce the same archive as zip xyz file1.
Creating a zip archive of a directory that includes the content of subdirectories is common.
3. In the same zip file linux, you can also add numerous files and directories:
Compression Methods and Levels [Zip Compression]
Deflate is Zip’s default compression algorithm. If a file cannot be compressed, the zip utility just puts it in the file as it is, instead of compressing it with the store method. The bzip2 compression algorithm is supported by the zip program in most Linux variants.
Output:
adding: sub_dir/ (stored 0%)
adding: sub_dir/filename1 (bzipped 27%)
adding: sub_dir/filename2 (bzipped 68%)
The zip operation becomes more CPU-intensive when the compression level is increased, and it takes longer to complete.
How to Create a Password-Protected ZIP file?
The archive password must be entered and verified using the following command:
The password you will be entering will not be seen on the screen. However, the next time you log in, make sure you remember your password.
How to Create a Split Zip File?
Assume you wish to upload the Zip archive to a file hosting provider with a 1GB upload restriction, and your Zip archive is 5GB in size.
After a set hits the stated size limit, the operation above will keep producing new archives in it.
Bonus Zip Commands in Linux
1. Create a xyz.zip file that contains all of the files in the current directory.
2. You may use the following command to include all the hidden files:
3. Make a xyz.zip file that contains all MP4 files in the current directory, compressed.
Conclusion
The zip command in Linux may be used to build Zip archives [Zip files and Folders in Linux]. On a Linux operating system, the unzip command may also be used to decompress a ZIP file. We have seen how to zip various files and directories in this tutorial and various available zip command options in Linux.
How to Zip a Folder in Linux
Installing Zip Utilities on Ubuntu/Debian
First update the apt package repository cache with the following command:
The apt package repository cache should be updated.
Now install zip and unzip packages with the following command:
Zip and unzip packages should be installed. In my case, they are already installed.
Installing Zip Utilities on RHEL 7/CentOS 7
First update the yum package repository cache with the following command:
Now install the zip and unzip packages with the following command:
Now press y and then press to continue.
zip and unzip packages should be installed.
Zipping a Folder/Directory Graphically
If you have any graphical desktop environment installed on your chosen Linux distribution, then you can use it to Zip archive any folder you like very easily.
First open your favorite File Manager and go to the location where you have the folder you want to Zip archive. In my case I am using the Nautilus file manager in GNOME 3 desktop environment.
Let’s say you want to Zip archive the Downloads/ directory as marked in the screenshot below.
Now right click on the Downloads/ directory and click on Compress… as marked in the screenshot below.
Now type in a name for your Zip archive and select .zip
Once you’re done, click on Create.
A backup.zip file should be created. This is the Zip archive of the Downloads/ directory.
Zipping a Folder/Directory using the Command Line Interface (CLI)
If you don’t have any graphical desktop environment installed on your computer, don’t worry. You can still use the command line interface (CLI) to Zip archive a folder.
First go to the location where the folder you want to zip archive is available with the following command:
NOTE: PATH is the location where your desired folder is located.
For example, if you want to Zip archive the /etc directory. So the PATH should be the root directory /.
Again, if you want to Zip archive the /etc/apt directory, then the PATH should be /etc.
Let’s Zip archive, /etc/apt directory.
The command for Zipping a folder or directory is:
NOTE: Here FOLDER is the directory that you want to Zip archive. OUTPUT is the path to a file where the Zip archive of FOLDER will be saved.
For example, run the following command to Zip archive /etc/apt directory and save it to the HOME directory of your login user as apt_backup.zip:
The /etc/apt directory or folder should be Zip archived.
It should be save to
/apt_backup.zip file as you can see from the screenshot below.
Extracting the Zip Archive Graphically
If you have a graphical desktop environment installed, then extracting the Zip archive is very easy.
Just right click on the Zip archive you want to extract and you should see the following menu. Select either Extract Here or Extract to… to unzip it.
If you want to extract the archive to your current working directory (the directory you’re in right now), then click on Extract Here. It should be extracted as you can see from the marked section of the screenshot below.
If you want to extract it to a different directory, then click on Extract to…
A directory picker should be opened as you can see in the marked section of the screenshot below.
Select a directory and click on Select.
The Zip archive should be extracted in that directory as you can see in the marked section of the screenshot below.
Extracting the Zip Archive using the Command Line Interface (CLI)
If you don’t have the graphical desktop environment installed on your Linux distribution, don’t worry. You can extract the Zip archive using the command line interface (CLI).
First navigate to the directory where you want to extract the Zip archive with the following command:
NOTE: EXTRACT_DIR is the directory where you want to extract the Zip archive.
Then run the following command to extract the Zip archive:
NOTE: Here ZIP_ARCHIVE is the path to the Zip archive that you want to extract.
For example, let’s extract the
/apt_backup.zip file to
First navigate to the
Now run the following command to extract apt_backup.zip file:
/ apt_backup.zip file should be extracted.
The extracted apt/ directory.
That’s how you Zip and Unzip a Folder or Directory in Linux. Thanks for reading this article.
ZIP command in Linux with examples
Syntax for Creating a zip file:
Extracting files from zip file
Unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems. The default behavior (with no options) is to extract into the current directory (and sub-directories below it) all files from the specified ZIP archive.
After removing hello7.c from myfile.zip file, the files can be restored with unzip command
2.-u Option: Updates the file in the zip archive. This option can be used to update the specified list of files or add new files to the existing zip file. Update an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.
Syntax:
Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c
After updating hello5.c from myfile.zip file, the files can be restored with unzip command
3. -m Option: Deletes the original files after zipping. Move the specified files into the zip archive actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous removing all input files.
Syntax :
Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c
After this command has been executed by the terminal here is the result:
Suppose we have following files in my current directory (docs) are listed below:
unix.pdf
oracle.pdf
linux.pdf
Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c
This command on execution will compress all the files except hello3.c
Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c
This article is contributed by Mohak Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
How to ZIP a folder in Ubuntu
Using Zip file format for file compression takes less disk space and saves the network bandwidth while you transfer a file from one machine to another system. Zipped file format can be transferred more quickly as compared to uncompressed files. These compressed zip folders can easily be extracted using supportive utilities in Linux, macOS, and Windows operating systems.
You will learn in this guide how to zip a folder in Ubuntu Linux operating system. We will explain two methods for zipping a folder; one method is zip a folder using command-line and the other using the Gnome graphical interface environment. All steps we will cover in this article have been implemented on Ubuntu 20.04 LTS focal fossa distribution.
Method 1: Zip folder using the Terminal or command-line method
Before going into the details, ensure that the zip command-line utility is installed on your system to zip a folder.
Install zip command-line utility
The ‘zip’ is a command-line utility that helps you in file compression. In most Linux distributions, this zip utility is not installed by default. So, if this utility is not available on your Ubuntu distribution, then you can easily install it by using the Ubuntu official apt repository. To install the ‘zip’ command on the Ubuntu system, use the below-mentioned command on the terminal:
$ sudo apt update
$ sudo apt install zip
After installing the zip utility, check the installed version by using the following command:
Zip a folder using the zip utility
To zip a folder through the command line, use the follow the following syntax:
Example
For example, we want to zip a folder ‘MyDocsFolder’. The following command will create a zip archive of this folder:
Zip multiple folders using the Zip utility
You can add multiple directories or folders into a single zip archive. Similarly, you can also include multiple files in the same archive. Using the following command, you can create a zip archive of multiple files and folders:
Zip compression methods
Zip utility uses various compression methods. The two most commonly used compression methods are store and deflate.
Deflate: The default compression method of zip command is ‘deflate’.
Store: If the zip utility finds a file that can be compressed, it simply includes it into the zip archive without compression using the ‘store’ method.
In most of the Linux distributions, the zip command also supports the ‘bzip2’ compression method.
How to create a password-protected zip file?
If you want to store your sensitive information in a zip archive, then use the option ‘-e’ for archive encryption as follows:
The above command will be asked you to enter and verify the password for the archive:
Create a split Zip file by specifying the size
Sometimes, you need to store data on another server or file hosting service that only allows you to upload limited file size. For example, you have 2 Mb of data for upload, but it allows you to upload only 100 kb data files. In this situation, you can create a split archive file using option ‘-s’ followed by the limited specified archive file size as follows:
After reaching the specified size limit, the above command creates a new zip archive file in the home directory.
Utilization of zip command
Using the zip command-line utility, you can create a zip archive containing all files of the current directory.
To create an archive file by including all hidden files as follows:
You can also create a zip archive of all MP3 files of the current directory without compressing files as follows:
Method 2: Zip a folder using Gnome GUI
The following steps you need to perform while zipping a folder using Gnome graphical desktop environment:
Use the Gnome desktop, and navigate to the folder that you want to zip.
Select and right-click on the folder. Now, choose option ‘compress’ from the drop-down list as follows:
Next, choose the ‘.zip’ extension and assign a suitable name for the zip archive. Now, click on ‘Create’ as follows:
You will notice the zip archive will be created in the current directory where the original folder resides.
Conclusion
We discussed how to create a zip archive using the zip command-line utility in Ubuntu 20.04 system. We presented two methods for zipping a folder using the command line and using the Gnome GUI method. Based on your requirements, you can choose a suitable method. I hope the details mentioned above about zipping a folder using the zip command will help you.
About the author
Samreena Aslam
Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.
How can I create a zip archive of a whole directory via terminal without hidden files?
I have a project with lots of hidden folders / files in it. I want to create a zip-archive of it, but in the archive shouldn’t be any hidden folders / files. If files in a hidden folder are not hidden, they should also not be included.
I know that I can create a zip archive of a directory like this:
It didn’t work. All hidden directories were still in the zip-file.
8 Answers 8
First of all if you don’t have installed zip install it first as follows:
Then for simply creating a zip file:
If you want to exclude hidden files:
Meaning you could exclude a single file, say it’s named “Nothanks.jpg”
Let’s cover a few specific examples where this is useful.
If the directory includes subdirectories however, you’ll want to use another variation of that command to exclude the the ds_store files from subdirectories as well:
Note: not all shells require the quotations for this command to work properly, but in the bash shell (the default for OS X) you will need to use the quotes for excluding with wildcards and patterns.
Exclude Specific File Types from a Zip Archive
That could be modified for any specific file extension or pattern matched in a file name.
Exclude All Hidden Files from a Zip Archive
Or to exclude all invisible files from all subdirectories: