- How to Zip and Unzip a directory and its files in Linux [duplicate]
- 3 Answers 3
- How Do I Zip All Files In A Directory In Linux?
- Install Zip
- For Debian/Ubuntu
- For REHL/CentOS
- The Zip Command
- How to Zip Files In Linux
- How to Compress Zip Directories In Linux
- How to Zip all Files in a Directory In Linux
- How to Zip All Files, Including Hidden Files
- Conclusion
- About the author
- John Otieno
- How to zip a folder in Ubuntu Linux / Debian Linux
- What Is Zip Used For?
- Install Zip on Debian and Ubuntu Systems
- Install Unzip on Linux CentOS and Fedora
- How to Use Zip and Unzip in Linux
- zip Command
- Create Zip Files in Linux
- Using Linux to Unzip a file
- Remove a File from a .zip File
- How to Update Zip Files
- Move a File to a Zip
- Recursive Use of Zip on Linux
- Exclude Files in a Zip
- Unzip to a Different Directory
- Use Linux Unzip with Multiple Zip Files
- Creating a Password Protected ZIP file
- Creating Split Zip File
How to Zip and Unzip a directory and its files in Linux [duplicate]
I am a newbie in Linux.Whats is the complete process to zip and unzip a directory and its files.Please mention if any installation has to be done.
3 Answers 3
You say you are a newbie. Maybe you got Windows-experiences. If you do not have the packages installed yet, you need to
sudo apt-get install zip gzip tar
first (or by a graphical pkg-manager).
Then, for an entry it would be the easiest way to use zip/unzip:
zip -r my_arch.zip my_folder
Zip stores relative path names by default. There are several parameter-options available for zip. For that read: the manual (man zip). For a starting this will do.
Most often you will see .tar.gz endings in linux-world. That’s the product of two tools: TAR (the tape archiver) and GZIP (the GNU-Zip). Tar has got the call option to automatically gzip/gunzip files after «taring».
tar -cvzf may_arch.tar.gz my_folder
- -c means «create»
- -v means «verbose» (sometimes bothersome and slowing down. )
- -z means «use (GNU)zip»
- -f XYZ declares the name of the output file. (You should chose a helping name like XYZ.tar.gz)
There may also be .tar.bz2 endings. This is the product of the -j parameter instead of the -z parameter: you will choose compression with BZIP2 (-> man bzip2).
To extract you simply use -x (eXtract) instead of -c (Create):
You can use the zip and unzip command line utilities. These can be installed by running
sudo apt-get install zip unzip
I know several ways, but since you’re new on linux. So I’ll tell you how to zip a file using GUI method (the easiest way).
- Create a new folder and fill it with anything you want, for example many of file (In my case, I’ll fill it with theme folder) :
- Right click the folder you want to zip and select «Compress. » option :
- You can choose which file format you want by clicking combobox next to «Filename» textbox. Also you can set the folder you want to zip location.
Clicking «Other options» will lead you to password section, in other word you can set password for your preferred file so someone (include you) have to enter password before unzip the file.
How Do I Zip All Files In A Directory In Linux?
Zip archives refer to container archives that contain one or more compressed files and directories. Zip files are cross-platform, allowing you to create zip archives in Windows, Linux, and macOS using various utilities. In addition, zip archive files take less space, making them easier to transfer and store.
In Linux, we use the zip archive utility to create zip archives. Throughout this tutorial, we will focus on how to go about creating zip archives in Linux using the zip utility.
Install Zip
Depending on your Linux distribution, you will need to install the zip utility. Since all Linux distributions support it, you can use the package manager to install it.
For Debian/Ubuntu
On Debian, use the command:
For REHL/CentOS
On CentOS and REHL family, use the command:
The Zip Command
The zip command is simple to use. The general syntax for the command is:
To create a zip archive of more than one file, pass them in a list (separated by space) after the zip filename. It is also good to ensure you have to write permissions in the directory you are creating the zip file.
How to Zip Files In Linux
We can zip files in a directory as:
The command above displays the name of the file added to the archive and the compression method.
Zip utility automatically adds a .zip extension to the archive filename—if not explicitly specified.
How to Compress Zip Directories In Linux
You can compress directories and the corresponding sub-directories by using the -r flag. The -r flag will tell zip to traverse the entire directory recursively.
For example, consider the /var/log directory. To create an archive of all the files and directories, we use the command:
To suppress the output from the compression process, use the -q for quiet mode. The command creates a zip archive of the specified files with no output.
How to Zip all Files in a Directory In Linux
What if you want to zip all files in a directory? In that case, we use wildcard expressions to do this.
The above command adds all the files and directories in the specified path and adds them to the zip archive.
How to Zip All Files, Including Hidden Files
To add even hidden files to a zip archive, use the wildcard (.* *). The command for that is:
Conclusion
As shown in this tutorial, Linux allows you to create zip archives. You can use any archive utility such as WinRar, 7zip, unzip; to unarchive the files.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list
How to zip a folder in Ubuntu Linux / Debian Linux
This quick blog shows you how to Zip (compress) files and directories in Linux using the zip command.
Suresh Ramani
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 from one to another machine more quickly 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 on most Linux flavors by default, but can be easily installed. By creating .zip files you can match .tar.gz file compression!
This quick blog 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 may choose to use zip files:
- When you are frequently working between Windows and Unix-based systems. Not only does this compress files but also is a file package utility. Works on multiple operating systems
- To save bandwidth. If you have limited or restricted bandwidth, then zip can be used between two servers for file transfer
- Transfers files quickly. Zip utility reduces file size, therefore reducing transfer time
- Upload or download directories at a faster speed
- Save disk space
- Unzip password-protected .zip files
- Enjoy a good compression ratio
Remember, before taking advantage of Unzip on Linux, you’ll have to SSH into your virtual private server.
Install Zip on Debian and Ubuntu Systems
Installing unzip is easy! With Ubuntu and Debian use the command below to install unzip:
Sit back and wait a minute, until the installation is finished.
To create zip files, you’ll also have to install zip. You can do this with the following command:
Install Unzip on Linux CentOS and Fedora
This again is simple and can be done using the below command:
Once the installation is complete you can check the path with the following command:
After you execute the direction in the command line, you should get an output that looks like this:
You can also confirm everything is installed properly by using the command below. It will give a verbose with unzip utility details.
How to Use Zip and Unzip in Linux
Now that we know how to install the utility, we can start learning its basic uses of it:
zip Command
Zip is a command-line utility that helps you create Zip archives.
The zip command takes the following syntax form:
zip OPTIONS ARCHIVE_NAME FILES
To create a Zip archive in a specific directory, the user needs to have write permissions on that directory.
Zip files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command. To preserve the file ownership and permissions, use the tar command.
The zip utility is not installed by default in most Linux distributions, but you can easily install it using your distribution package manager.
Create Zip Files in Linux
The basic syntax to create a .zip file is:
zip options zipfile list_Of_files
To test this, we created two files – ExampleFile.txt and ExampleFile1.txt. We’ll compress them into sampleZipFile.zip with 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. This will unzip all files to the current directory. One such example is as shown below:
This by default will be unzipped in the current folder provided you have read-write access.
Remove a File from a .zip File
Once a .zip file is created, you can remove or delete files in it. So, if you want to remove ExampleFile.txt from the existing sampleZipFile.zip, then you can use the following command:
zip –d sampleZipFile.zip ExampleFile.txt
Once this command is executed, you can unzip the .zip file using:
Over here you will find that ExampleFile.txt has been removed and can’t be seen on extraction.
How to Update Zip Files
Once a .zip file is created, you can add a new file to an existing .zip file. Suppose a new file ExampleFile2.txt needs to be added to the already existing sampleZipFile.zip. You can do this with the command shown below:
zip –u sampleZipFile.zip ExampleFile2.txt
Now if you extract sampleZipFile.zip, you will find the new file ExampleFile2.txt added to it.
Move a File to a Zip
You can easily move specific files to a zip file. That means that after adding the files, they will be deleted from their original directories. This is mostly used when you have a large file or directory, but need to conserve disk space. This is done 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 used to recursively zip files. This option will compress all the files present within a folder. An example of such a command is shown below:
zip –r sampleZipFile.zip MyDirectory
In the example, MyDirectory is a directory that has multiple files and sub-directories to be zipped.
Exclude Files in a Zip
While creating a .zip file, you can exclude unwanted files. This is done by using the -x option. Below is an example:
zip -x sampleZipFile.zip ExampleFile.txt
Here ExampleFile.txt will not be added to the sampleZipFile.zip.
Unzip to a Different Directory
In case you do not want to unzip to the current directory but want to specify a directory location, then this can also be done. Use the -d option to provide a directory path in the unzip command. An example of such a command is shown below:
unzip sampleZipFile.zip -d /usr/sampleZip/ExampleDir
Use Linux Unzip with Multiple Zip Files
If you want to unzip multiple zip files existing within your current working directory then you can use a command as shown below:
This command will unzip all the individual zip files.
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:
zip -e archivename.zip directory_name
Creating Split Zip File
Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB, and your Zip archive is 5GB.
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).
zip -s 1g -r archivename.zip directory_nameCopy
The command above will keep creating new archives in a set after it reaches the specified size limit.
archivename.zip archivename.z01 archivename.z02 archivename.z03 archivename.z04
Thank you for reading this article.
If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.