Most of us, at one time or another, need to create zip files. They are helpful for several reasons and work on many different platforms. There are utilities from which you can make them from your desktop, but often we need to create them from Mac Terminal.
Creating zip files from a command line interface like Mac Terminal may sound a little overwhelming, but if you know what files and folders you need to zip up, you just need to use the zip command along with the required parameters.
zip -r <zipfilename.zip> <files and/or folders>
My name is Eric, and as a software engineer, I create zip files nearly daily. I use them to package software for releases, but there are many uses and benefits to using them, and I can show you how to quickly create a zip file from Mac Terminal.
Keep reading below if you would like to see how to create a zip file from Mac Terminal. I will also provide some tips to simplify the process and explain why zip files are so helpful in so many situations.
Contents
What is a zip file?
A zip file is a file, usually with an extension of .zip, which contains one or more files and/or directories packaged inside it. The zip file is normally in a compressed format, meaning its size is significantly smaller than the size of the original files.
Zip files are also sometimes referred to as archive files or compressed files since the zip format is often used to store and set aside files that are no longer in use. The zip format allows them to be stored together, and the compression reduces their space in the storage area.
Create a zip file using the zip command
To create a zip file, you must use the zip command above. You will also need to know where the files and directories you wish to put into the zip file and navigate to that directory.
Let’s look at an example using three files (mytest.txt mytest2.txt mytest3.txt) and a directory (testdir). I am already in the directory containing these files, and I will call my new zip file test.zip.
We will use the following command to zip the files and directories into test.zip.
zip -r <zipfilename.zip> <files and folders>
zip -r test.zip mytest.txt mytest2.txt mytest3.txt testdir
Where zip is the command, the -r parameter tells the command to recurse the directories and grab any files from within any of the directories. Test.zip is the name of the zip file that will be created, and the rest of the line is the names of the files and the directory that it adds to the zip file.
Once completed, you will have test.zip in your current directory.
Zip File Tips
Specifying each file and folder for the zip command, as shown above, works when we have just a few files and directories, but what if we have 20 files, 100 files, or even more? We can’t possibly type out all of those file names. Fortunately, there are ways to make this easier.
There are also other things we can do with the zip command, such as encrypting the zip file with a password or using the zip command to see what is inside a zip file so that we can verify it. Let’s look at some tips that can help you when using the zip command.
Put your Files in a Directory
One of the best methods I use when creating large zip files with multiple files and directories is to first copy all of them to a new directory that I set up just for making the zip file.
I can organize everything and ensure everything is there before the zip file is created. This also creates a nice format when you go to unzip the files since they will all be grouped nicely in the directory that you create and not just unzipped randomly into your current location.
Once the directory has all the files you want to zip up, you must specify the directory’s name in your command. See the example below.
zip -r <zipfilename.zip> <directoryname>
For this example, I have put all of my files in a directory called Archive, but you can name your directory anything you would like.
Now I just run the zip command as shown below.
zip -r myfiles.zip Archive
The zip file is created with one simple command.
Use Wildcards to specify files
Using wildcards allows you to specify a whole group of files to add to your zip file. For example, if I want to add all files with the extension .txt to my zip file, I would specify *.txt. I could also add all .doc files by specifying *.doc. If I want to create a zip file with both types, my command would be as shown below.
zip -R newzipfile.zip *.txt *.doc
Note that here we must use a capital -R for our recurse parameter. It tells the command to recurse all subdirectories searching for the patterns *.txt and *.doc. This will add all files with the extensions of .txt and .doc under my current directory to a zip file called newzipfile.zip.
If I just want to zip all files in the current directory, I can just use the * symbol, which specifies all files.
zip -R allfiles.zip *
Password Protect your Zip file
You can encrypt your zip file with a password if you would like. This will keep others from opening or extracting the file unless they have the password. This is an excellent and secure way to be able to send someone files with confidential or personal information.
To create a password-protected zip file, use the -e parameter and then enter any password you would like to use. You must verify the password to ensure you have typed it correctly.
Don’t forget the password, or you will be unable to open or extract this zip file.
Verify your Zip file
Sometimes you may want to verify your zip file, especially if it is an important one or you are using it to back up important files. You can look at the zip file’s contents to verify it using the -sf parameter, as shown below.
zip -sf <zipfilename.zip>
For example, let’s look at the zip file we created called myfiles.zip.
zip -sf myfiles.zip
Other Zip parameters and commands
You can use many other parameters with the zip command. If you just type zip without any parameters or file names, you can see the basic and most commonly used parameters.
Typing zip -h2 will show you more parameters and more details about them. You can also use the man command to see more information by typing man zip.
Why use a zip file?
Zip files have two main benefits. They allow multiple files and directories to be packaged together in one file and save space by compressing their size. Let’s look at these benefits in a little more detail.
Packaging
Packaging is the ability to put multiple files and directories (or package them) into one file. That one file makes the following items or activities much easier.
Installation Packages
Installation packages are files that contain the required files to install an application or a specific set of files on a computer. Zip files make it easy to package all of the required files into one file and then have some sort of installer that can extract the files.
Transporting
Transporting one single file is much easier than a group of multiple files and directories. Whether downloading or uploading from a server, emailing, or copying to a flash drive, dealing with one file makes things much simpler and quicker.
Backup and Archiving
If you use zip files to back up or archive files, it is convenient to have them all in one place in one file. It makes it easy to move them to an external storage location such as the cloud, a USB drive, or any other media type.
Compression
Compression takes files and stores them using less space than the original copies used. Compression techniques are a bit complicated and more than what we will discuss here, but zip files use compression to reduce the storage space required. Let’s look at how this can benefit us in the same three areas above.
Installation Packages
Installation packages can be much smaller and take up less space. This leaves more space on the target system for the actual application and lessens the chances of you needing to clear extra space before installing the application.
Transporting
Smaller files are quicker and easier to transport or transmit through a network connection, over the internet, through email, or any other method.
Backup and Archiving
System backups or archiving old files can eat up disk space very quickly. Smaller file sizes help to reduce this problem.
FAQs
Below are a few common questions when learning how to zip files and folders.
How do I unzip a file in Mac Terminal?
Extracting or unzipping the contents from a zip file is done with the unzip command.
Can I zip a zip file?
Yes, you can. A zip file can contain other zip files, and you can see that in some of my examples above. The only caveat is that if the zip file you zip is already compressed, it will not be able to compress any more than it already is.
Are there utilities I can use to create zip files?
Yes, there are many utilities out there that you can use to create zip files from your desktop. Just go to AppStore and search, and you will find a number of them available.
Conclusion
Zip files are important because they allow us to easily store and transport multiple files in one package and compress the size of the files, saving us storage space.
The zip command allows you to zip files and folders on Mac Terminal. There are multiple parameters you can use and variations of how to run the command, and we have covered a few of them above.
I hope the information provided can help you to zip up your essential files and folders. As usual, let me know if you have questions or feedback. I would love to hear from you!