A commonly used archive file format for files and folders is known as Zip. Files can be compressed into archived formats to save disk space and network bandwidth. Zip files can be easily extracted in Windows, Linux, and MacOS operating systems.

This blog post will demonstrate how to zip a file in a linux terminal.

How To Zip a File in Linux Terminal?

In Linux, we can use the zip command to compress the files into ZIP format.

First, let’s create some text files in our system using the command:

$ touch file{1.5}.txt

It can be seen that five text files have been created

Now zip the above files using the below-listed command:

$ zip files filenames

It can be seen that the five files are zipped successfully.

To see the contents of a zip file, use the following command:

$zip -sf files.zip

It can be seen that the five text files are displayed.

Let’s see the zip files on our system.

Simply click on Files icon.

It can be seen that the zipped files are present in our system.

How To Add a Directory to a Zip File

We can also add a folder or a directory to our zip file. Use the following command to do so.

$ zip -r files directoryname

In our scenario, we will add the documents folder to our zip file.

Now to verify it, go to your zip file in the system. Right-click on it and select open with Archive Manager.

It can be seen that the Documents folder is added to the zip file.

How To Delete Files From a Zip

We can also delete our zip files using the command line terminal.

First, let’s see which files are present in our zip file using the command:

$ zip -sf files.zip

Now to delete a specific file, use the below-listed command:

$ zip -d files.zip filename.txt

In our scenario, we have deleted the text file 5 as shown below:

To verify whether the file is deleted or not, use the zip -sf files.zp command again.

It can be seen that the file 5 is deleted successfully.

How To Create and Encrypt Zip File

-e keyword is used to add password encryption to protect your zip file from unauthorized access.

To encrypt a zip file, use the following command:

$ zip -e files *.txt

It can be seen that the system prompts the user to provide a password to see the contents of a zip file.

Conclusion

A commonly used archive file format for files and folders is known as Zip. To zip files in a linux terminal we can use the zip files filenames command. To see the contents of a zip file, use the zip -sf files.zip command. To add a directory to a zip file, we can use the zip -r files directoryname command. To delete a zip file, use the zip -d files.zip filename command. This blog post discussed how to zip a file in a Linux terminal.

Frequently Asked Questions

How do I compress files using the zip command in Linux terminal?

You can compress files in Linux terminal using the zip command followed by the filenames you want to zip. For example, 'zip files file1.txt file2.txt file3.txt'.

Can I view the contents of a zip file in the Linux terminal?

Yes, you can view the contents of a zip file in the Linux terminal using the command 'zip -sf files.zip'. This command will display the files contained within the zip archive.

How can I add a directory to a zip file in Linux?

To add a directory to a zip file in Linux, use the command 'zip -r files directoryname'. For instance, 'zip -r files documents' will add the 'documents' folder to the zip file.

What is the process for verifying the contents of a zip file in Linux?

To verify the contents of a zip file in Linux, you can right-click on the zip file and select 'open with Archive Manager'. This will allow you to inspect the contents of the zip file.

How do I delete files from a zip archive using the command line terminal?

You can delete files from a zip archive using the command 'zip -d files.zip filename'. Simply replace 'filename' with the name of the file you want to remove from the zip archive.

Is it possible to unzip a file in the Linux terminal?

Yes, you can unzip a file in the Linux terminal using the 'unzip' command followed by the filename of the zip archive. For example, 'unzip files.zip'.

Can I compress multiple directories into a single zip file in Linux?

Yes, you can compress multiple directories into a single zip file in Linux by using the 'zip' command with the '-r' flag for each directory you want to include in the zip file.

How do I extract a specific file from a zip archive in the Linux terminal?

To extract a specific file from a zip archive in the Linux terminal, use the command 'unzip files.zip filename'. Replace 'filename' with the name of the file you want to extract.