- How to zip files and folders in Linux
- Syntax for the zip command
- Zipping files and folders
- Updating or adding a file to the zip file
- Deleting a file from a compressed file
- Removing files after zipping
- Creating a password-protected zip file
- Changing compression levels
- How to Zip a Folder in Linux
- Installing Zip Utilities on Ubuntu/Debian
- Installing Zip Utilities on RHEL 7/CentOS 7
- Zipping a Folder/Directory Graphically
- Zipping a Folder/Directory using the Command Line Interface (CLI)
- Extracting the Zip Archive Graphically
- Extracting the Zip Archive using the Command Line Interface (CLI)
- About the author
- Shahriar Shovon
- How to Zip and Unzip a directory and its files in Linux [duplicate]
- 3 Answers 3
How to zip files and folders in Linux
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
To zip a file in Linux or macOS, we can use the zip command.
Syntax for the zip command
zip [options] zipfile_name filename_separated_by_space
If the zip command is not present in your system, install it with:
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
Zipping files and folders
To zip files, we need the zipped filename and the files to be included in the zip file.
The zip command prints the names of the files added to the zip file and the compression method the default compression method is deflated . Now, a new zip file will be created.
adding: README.md (stored 77%)
To hide the above message, we can use the -q option.
The above code will not print the files added or the compression method in the terminal.
To zip all the files in a folder, we can use * instead of specifying all the filenames.
The above command will zip all the files and folders (files inside the folder are not included) in the current directory.
To zip a folder, we can add the following folder name.
The above code will only zip the files in the folder_1. The files in the subfolders of folder_1 will not be compressed.
To add all files in the folder and subfolder, we can use the -r command, which will compress all the files in the folder.
zip -r folders.zip folder_1
The above code will zip all the files recursively in folder_1. The folder structure is maintained 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
Updating or adding a file to the zip file
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.
Deleting a file from a compressed file
To delete a file from the compressed file, we can use -d .
zip -d multiplefolder.zip README.md
Removing files after zipping
We can use the -m command to delete the original files after the zip file is created.
The above command will delete README.md.
Creating a password-protected zip file
We can use -e to create a password-protected zip file.
The above command will ask for a password.
Enter password: Verify password:
Changing compression levels
The compression level defines how optimally the files can be compressed. The compression levels range from 0-9 but, by default, the compression level is 6. To change the compression level, we can use -compression_number .
The above code 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.
How to Zip a Folder in Linux
In this article, I will show you how to Zip and Unzip a Folder or Directory in Linux. This might seem trivial, but sometimes people will struggle to get it right. Let’s get started.
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 ~/Downloads/ directory.
First navigate to the ~/Downloads directory:
Now run the following command to extract apt_backup.zip file:
The extracted apt/ directory.
That’s how you Zip and Unzip a Folder or Directory in Linux. Thanks for reading this article.
About the author
Shahriar Shovon
Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.
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.