How To Archive Files And Directories In Linux [Part 2]
In the previous guide, we learned how to archive files and directories using tar command in Linux. Today, we are going to do the same, but using Zip an Unzip programs. The zip application is both an archiver and compression utility, so we can use it for archiving, compressing and decompressing files and directories in Unix-like operating systems. This zip application is compatible for windows and other os zip application. However, gzip and bzip2 are the predominant compression applications. Now let us see how to archive files and directories in Linux using Zip program.
Archive Files And Directories In Linux Using Zip Program
The Zip program comes pre-installed with some Linux distros. Just in case if it is not available, you can install it using the distribution’s default package manager.
On Arch Linux and variants:
$ sudo pacman -S zip unzip
On RHEL, CentOS, Fedora:
$ sudo yum install zip unzip
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install zip unzip
$ sudo zypper install zip unzip
Now let us see some examples.
Creating a new archive
To archive a group of files into one, simply do:
$ zip file.zip file1 file2 file3
Sample output would be:
adding: file1 (stored 0%) adding: file2 (stored 0%) adding: file3 (stored 0%)
Here, we are create an archive named file.zip from file1, file2 and file3. You don’t have to use the .zip extension at the end of the file name. I have added it for the sake of clarity.
Similarly, to create an archive of a a directory, run:
$ zip directory.zip ostechnix/ adding: ostechnix/ (stored 0%)
Here, I have created an archive named directory.zip of the ostechnix folder. Please note that this only add the parent directory (i.e) to the archive. To archive all sub-directories recursively, use -r flag like below.
$ zip -r directory.zip ostechnix/ adding: ostechnix/ (stored 0%) adding: ostechnix/file.txt (deflated 67%) adding: ostechnix/audio.mp3 (deflated 6%) adding: ostechnix/image.jpg (deflated 3%)
Unlike tar utility, the zip program creates archive and compresses it where possible. But tar is an archiver, not a compression tool.
Create an archive of multiple directories and/or files at a time
Sometimes, you may need to create an archive from a group of files and folders. To do so, run:
$ zip -r directory.zip ostechnix/ file1 file2 file3 adding: example/ (stored 0%) adding: example/Stephen.jpg (deflated 3%) adding: file1 (stored 0%) adding: file2 (stored 0%) adding: file3 (stored 0%)
The above command create an archive named directory.zip from ostechnix directory and the group of files namely file2, file2, file3.
Now let me show you an another example.
$ zip directory.zip file4 adding: file4 (stored 0%)
Did you notice? I have specified the existing archive, but zip program updated the contents of existing archive file without replacing them. So, if you specified an existing archive, it gets updated rather than replaced. To put this simply, the existing archive is preserved, the new file4 is added. This is a notable advantage of zip program over tar utility.
Extracting archives is as simple as creating archives. To extract an archive, simply do:
$ unzip directory.zip
Sample output would be:
Archive: directory.zip creating: ostechnix/ inflating: ostechnix/file.txt inflating: ostechnix/audio.mp3 inflating: ostechnix/image.jpg extracting: file1 extracting: file2 extracting: file3 extracting: file4
You can also selectively extract a specific file or folder from the directory. For example, the following command extracts file4 from the the archive directory.zip.
$ unzip directory.zip file4 Archive: directory.zip extracting: file4
List contents of an archive without extracting it
To list the contents of an archive without actually extracting it, use -l flag.
$ unzip -l directory.zip Archive: directory.zip Length Date Time Name --------- ---------- ----- ---- 0 2018-03-29 15:55 ostechnix/ 1286 2018-01-11 12:16 ostechnix/file.txt 8073033 2018-03-29 14:03 ostechnix/audio.mp3 15240 2018-03-29 14:45 ostechnix/image.jpg 5 2018-03-29 15:58 file1 5 2018-03-29 15:58 file2 5 2018-03-29 15:58 file3 5 2018-03-29 16:37 file4 --------- ------- 8089579 8 files
This can be useful when you want to find and extract a specific file instead of extracting the whole archive.
Creating an encrypted archive
The another notable feature of zip command is it allows us to create an encrypted archive, so the users needs to enter the password in-order to extract or view the contents of the archive.
To create an encrypted archive, use -e flag.
$ zip -e -r directory.zip ostechnix/ file1 file2 file3 file4 Enter password: Verify password: adding: ostechnix/ (stored 0%) adding: ostechnix/file.txt (deflated 67%) adding: ostechnix/audio.mp3 (deflated 6%) adding: ostechnix/image.jpg (deflated 3%) adding: file1 (stored 0%) adding: file2 (stored 0%) adding: file3 (stored 0%) adding: file4 (stored 0%
When extracting or viewing the contents of the archive, you need to enter the correct password.
$ unzip directory.zip Archive: directory.zip [directory.zip] ostechnix/file.txt password:
Create a multi-part archive
Sometimes, you may need to share an archive file over Internet and that archive file itself might be too big to send over Internet or LAN. In such cases, we can create multi-par small sized archives and send them over Internet.
To archive a directory into multiple smaller parts, for example 3 MB each, we do:
$ zip -r -s 3m archive.zip ostechnix/
This command will create archive files of size 3MB like archive.z01, archive.z02, archive.z03 …. and archive.zip. You need to send these files to the recipient. When extracting these archive files, all contents will be extracted in a single folder named ostechnix.
To split the archive in GB, use “g” like below:
$ zip -r -s 3g archive.zip ostechnix/
Create archives of other programs
Zip can accept standard input, so it is possible to create an archive of other programs. The following command pipes the output of “ls” command as input to zip command.
$ ls -l Documents/ | zip ls-documents.zip - adding: - (deflated 56%)
Remember here ls-documents.zip is the archive of “ls” command. So, It won’t display the contents of the archive using -l switch.
Instead, you could view its contents using command:
$ unzip -p ls-documents.zip | less
Here, -p refers pipe.
Compress archives with compression rate
To Archive a directory and its contents with the highest level  of compression:
$ zip -r -9 archive.zip ostechnix/
Zip supports three compression levels as given below.
- 1 – Fastest (Worst)
- 9 – Slowest (Best or highest)
- 6 – Default level
Exclude files or folders while creating archives
We can exclude unwanted files or sub-directories while creating an archive. To do so, use -x flag like below:
$ zip -r directory.zip ostechnix/ -x ostechnix/image.jpg
The above command will create an archive of ostechnix directory, but exclude image.jpg file from the archive.
You can verify the contents without extracting the archive file using the following command.
$ unzip -l directory.zip
Delete files from an existing archive
You might forgot to exclude some unwanted stuffs while creating archives. No worries! You can delete those files even after creating the archive.
To delete the unwanted files from an existing archive, use -d flag as shown below.
$ zip -d directory.zip "ostechnix/file.txt" deleting: ostechnix/file.txt
Similarly, to delete a group of same type of files, for example .txt filesuse:
$ zip -d directory.zip "ostechnix/*.txt"
And, that’s all for now. We only scratched the surface of zip/unzip commands. You can do a lot more using these programs. For more details, refer the man pages.
$ man zip
$ man unzip
More good stuffs to come. Stay tuned!