Linux 101: How to create a compressed archive of a folder from the CLI with zip

Find out how easy it is to create compressed archives from the Linux command line, using zip. Jack Wallen shows you how.

How to create a compressed archive of a folder in Linux from the CLI with zip

Recently, I showed you how to create compressed archives from the Linux command-line using the tar command. This time I want to demonstrate the same task, but using a tool you're probably already familiar with. The tool in question is zip, and it creates compressed zip files from whatever you throw at it. 

SEE: 5 Linux server distributions you should be using (TechRepublic Premium)

So, let's say you have the folder TEST and you want to create the compressed file TEST.zip so you can send it to a colleague. Fortunately, Linux can use the zip tool, but first, you might have to install it with a command like sudo apt-get install zip -y or sudo dnf install zip -y

Once installed, zip is ready to go. Let's compress that TEST folder. How you create the zipped file isn't quite as simple as you might think. The basic command is zip NAME.zip NAME (where NAME is the name of the folder to be compressed). However, if you simply issued the command zip TEST.zip TEST, you'd wind up with a file named TEST.zip, but after uncompressing it, you'd find it's missing the contents. 

Why? Because you only compressed the folder, not the contents. 

To include the contents of the TEST folder, you'd add the recursive flag, as in zip -r TEST.zip TEST

SEE: Rust: What developers need to know about this programming language (free PDF) (TechRepublic)

When you issue the command with the -r flag, you'll see zip adds every file within the directory to the compressed file. This time, if you decompress the file, with the command unzip TEST.zip, the contents of the directory remain intact. 

And that's all there is to creating compressed archives from the Linux Command Line Interface, using the zip tool.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see

linuxhero.jpg

Image: Jack Wallen

By Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic, The New Stack, and Linux New Media. He's covered a variety of topics for over twenty years and is an avid promoter of open source. For more news about Jack Wallen, visit his website jackwallen....