Zip Folder Terminal

broken image


OPTION 1 – If the Zip File is in the same directory/folder in which your terminal is and we want to extract it in the present working directory. Use the following command to achieve the above described scenario. Sudo unzip zipfilename.zip if the zip file is protected. To zip a folder and its content with Terminal you can use the zip command in the cli 1, and here are a few other goodies you can use to simplify your workflow, making sure the folder is compressed properly, all subdirectories are compressed recursively, and the zip filename is automatically set using the current folder.

How do you gzip a directory in Linux? Save pages as word. It's definitely not using the gzip command because if you try to compress a folder using gzip command, you'll see this error:

gzip: target is a directory -- ignored

Family feud matches. Interesting, isn't it? gzip command cannot compress a directory because essentially, gzip works on individual files, not the entire folder.

What can you do now? How to gzip compress a file in Linux? Here's what you can do.

gzip a directory using tar command

Instead of trying to compress the folder directly, you should use tar on it first. The tar command will collate all the files into one archive file. It doesn't compress the file itself.

If you combine tar with gzip, the tar command will create one single archive file from the folder and then gzip will compress this archive file.

The good thing is that you can do both of these steps in one single command by using the z option. The command looks something like this:

Let me explain the options used in the above command to you:

  • z – tells tar that it is dealing with gzip file
  • c – tells tar to create the archive file
  • v – verbose mode showing what files are being processed
  • f – output is a file

Example of using tar and gzip to compress a folder

Let's say I have a bunch of files in a directory. If I use the du command to get the size of the directory, it's 204 KB.

Now if I gzip compress the folder:

Zip Folder In Linux Terminal

Now if I check the size of the compressed folder using ls command, it's hardly 10 KB.

Keep in mind while using tar and gzip command

You should note a couple of things while using the tar command:

It's important to provide the filename in the command otherwise you'll see this error:

If you don't use the option -f at all, you will still see an error, a different one this time.

Zip Folder Terminal

I hope this command helped you to gzip a directory in Linux and you also learned a few related things around tar and compression. Now that you know how to compress a folder, maybe you would like to read on how to extract tar xz file in Linux.

Unzip Terminal

Any questions, suggestions or a simple word of thanks are always welcomed. Music cutter and mixer for pc.

Become a Member for FREE
Terminal
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only content

Linux Zip Directory

Join the conversation.





broken image