Advertisement

Zip Command in Linux To Create and Update Zip File

Zipping files and folders is a usual way to compress and store/share them. Here we are going to learn to use zip command in Linux with all important options.

Zip Command in Linux To Create and Update Zip File

Zip command can be used to create and update any zip file. There are multiple zip options available while creating or updating any zip file in Linux operating system.

Syntax: 
# zip OPTIONS ZipFileName.zip ListOfFiles


Creating a Zip File of Files and Folders

It is very easy to create zip file in Linux Operating System. You can easily create a zip file of files and folders by using the following command.

Syntax: 
# zip ZipFileName File1 File2 Folder1 Folder2

Example: 
# zip Compressed.zip File1.txt File2.txt File3.txt MyFolder

Above example will create a zip file containing "Myfile1.txt, MyFile2.txt, MyFile3.txt and MyFolder" in it.

Output:

Output: Creating a Zip File of Files and Folders

Creating a Zip File of Directory

In Linux we can also create a zip file of any directory we want. To create zip file of a directory we use "-r" option.

Syntax: 
# zip -r ZipFileName Directory_Name

Example: 
# zip -r MyDirectory.zip Directory

Above example will create a zip file of all files (File1.txt, File2.txt, File3.txt) and folders in directory "Directory" and save it as "MyDirectory.zip".

Output:

Output: Creating a Zip File of Directory

You can also use options while creating a zip file in Linux operating system.

Delete Original Files While Creating Zip File: "-m Option"

While creating a zip file, you can use "-m" option to delete all original files after creating its zip file. It  deletes all original files only after successful creation of a compressed achieve. After deletion of original files if directory is empty, then this command deletes directory also.

Syntax: 
# zip -m ZipFileName File1 File2

Example: If you have File1.txt, File2.txt, File3.txt and File4.txt in a current directory then after following command all files in current directory will be deleted after successful creation of zip file.

Command: 
# zip –m Compressed.zip *.txt

Above command will create a zip file having all files with extension ".txt" and will be named as "Compressed.zip". It will also delete all original files in current directory with extension ".txt".

Output:

Output: Delete Original Files After Creating Zip File: "-m Option"

Exclude Files While Creating a Zip File: "-x Option"

While creating a zip file, you can use "-x" option to exclude specific files in current directory. These excluded files will not be added in a zip file.

Syntax: 
# zip -x ZipFileName ExcludedFile1 ExcludedFile2

Example: If you have File1.txt, File2.txt, File3.txt and File4.txt in a current directory and you want to exclude File2.txt while creating zip file. You can use this command to exclude it.

Command: 
# zip -x Compressed.zip File2.txt

Above command will create zip file only having File1.txtFile3.txt and File4.txt in it.

Output:

Output: Exclude Files While Creating a Zip File: "-x Option"

Creating a Password Protected Zip File

If you want to create a password protected zip file, you can create it by using "e" option available in Linux operating system.

Syntax: 
# zip -e ZipFileName File1 File2 Folder1 Folder 2

Example: 
# zip -e Compressed.zip File1.txt File2.txt MyFolder

After executing the above code, you will be asked to enter your desired password. This will create a password protected zip file of entered files and folders with name "Compressed.zip".

Updating a Zip File in Linux

In Linux you can remove or add files in a zip file without extracting the actual zip file. You can use -u and -d options to respectively add and remove desired files from a zip file

Remove Specific File From Zip File: "-d Option"

If you want to remove specific file from a zip file without extracting it, you can use "-d" option with zip command.

Syntax: 
# zip -d ZipFileName File1 File2

Example: If you have File1.txtFile2.txtFile3.txt and File4.txt in a zip file named as "Compressed.zip" and you want to remove File2.txt from that zip file without extraction it, you can use following command.

Command: 
# zip -d Compressed.zip File2.txt

Above command will keep only File1.txtFile3.txt and File4.txt in "Compressed.zip" archive file.

Output:

Output: Remove Specific File From Zip File: "-d Option"

Adding Specific File in Zip File: "-u Option"

You can also add any file in a zip file without extracting the actual zip file. For that you have to use "-u" option with zip command.

Syntax: 
# zip -u ZipFileName File1 File2

Example: If you have File1.txtFile2.txt and File3.txt in a zip file named as "Compressed.zip" and you want to add File4.txt in that zip file without extraction it, you can use following command.

Command: 
# zip -u Compressed.zip File4.txt

Above command will add File4.txt in "Compressed.zip" archive file. And the updated Compressed.zip file will contain File1.txtFile2.txtFile3.txt and File4.txt in it.

Output:

Output: Adding Specific File in Zip File: "-u Option"


Note:
To unzip any zipped file in Linux you can use;

Command: 
unzip filename.zip
Conclusion: I hope you have learned to use zip command in Linux to create zip file with all important options.

Post a Comment

0 Comments