"zip" and "unzip" - Create and Extract ZIP Files

This section provides a tutorial example on how to use 'zip' command to compress multiple file and sub-directories into a ZIP file and use 'unzip' command to extract files out of a ZIP file.

If you want to backup a directory tree with many files and sub-directories, you may want to use "zip" command compress the entire directory tree into a single ZIP file. Later, you can extract the entire directory tree out of the ZIP file using the "unzip" command.

Here is what I did to create a backup of my GCC directory tree as a ZIP file.

1. Go to the parent directory where the directory tree is located. This is to ensure that only short relative pathnames are compressed into the ZIP file.

herong$ cd /home/herong

2. Create the ZIP file using the "zip -r" command.

herong$ zip -r gcc-backup.zip gcc

adding: gcc/ (stored 0%)
adding: gcc/hello.c (stored 0%)
adding: gcc/hello.cpp (deflated 3%)
adding: gcc/Makefile (deflated 26%)
adding: gcc/include/ (stored 0%)
adding: gcc/include/myheader.h (stored 0%)
adding: gcc/include/myfunc.h (deflated 13%)
adding: gcc/lib/ (stored 0%)
adding: gcc/HeaderTest.cpp (deflated 27%)
adding: gcc/MyStatic.cpp (deflated 19%)
adding: gcc/MyStatic.o (deflated 67%)
adding: gcc/libMyStatic.a (deflated 66%)
adding: gcc/MyStaticTest.cpp (deflated 26%)
adding: gcc/MyDynamic.cpp (deflated 19%)
adding: gcc/MyDynamicTest.cpp (deflated 26%)
adding: gcc/a.out (deflated 74%)
adding: gcc/hello (deflated 71%)
adding: gcc/MyDynamicTest (deflated 70%)
adding: gcc/libMyDynamic.so (deflated 71%)
adding: gcc/hello.o (deflated 66%)

3. Test the ZIP file using the "unzip -t" command.

herong$ unzip -t gcc-backup.zip

Archive:  gcc-backup.zip
    testing: gcc/                     OK
    testing: gcc/hello.c              OK
    testing: gcc/hello.cpp            OK
    testing: gcc/Makefile             OK
    testing: gcc/include/             OK
    testing: gcc/include/myheader.h   OK
    testing: gcc/include/myfunc.h     OK
    testing: gcc/lib/                 OK
    testing: gcc/HeaderTest.cpp       OK
    testing: gcc/MyStatic.cpp         OK
    testing: gcc/MyStatic.o           OK
    testing: gcc/libMyStatic.a        OK
    testing: gcc/MyStaticTest.cpp     OK
    testing: gcc/MyDynamic.cpp        OK
    testing: gcc/MyDynamicTest.cpp    OK
    testing: gcc/a.out                OK
    testing: gcc/hello                OK
    testing: gcc/MyDynamicTest        OK
    testing: gcc/libMyDynamic.so      OK
    testing: gcc/hello.o              OK
No errors detected in compressed data of gcc-backup.zip.

4. Extract all files and sub-directories from the ZIP file using the "unzip" command.

herong$ mkdir tmp
herong$ cd tmp

herong$ unzip ../gcc-backup.zip
Archive:  ../gcc-backup.zip
   creating: gcc/
 extracting: gcc/hello.c
  inflating: gcc/hello.cpp
  inflating: gcc/Makefile
   creating: gcc/include/
 extracting: gcc/include/myheader.h
  inflating: gcc/include/myfunc.h
   creating: gcc/lib/
  inflating: gcc/HeaderTest.cpp
  inflating: gcc/MyStatic.cpp
  inflating: gcc/MyStatic.o
  inflating: gcc/libMyStatic.a
  inflating: gcc/MyStaticTest.cpp
  inflating: gcc/MyDynamic.cpp
  inflating: gcc/MyDynamicTest.cpp
  inflating: gcc/a.out
  inflating: gcc/hello
  inflating: gcc/MyDynamicTest
  inflating: gcc/libMyDynamic.so
  inflating: gcc/hello.o

5. If you want to encrypt the ZIP file, you can use the "-P password" option.

herong$ cd /home/herong

herong$ zip -r -P TopSecret gcc-backup.zip gcc
...

6. If a file is encrypted in the ZIP file, the "unzip" command will prompt you for the password to decrypt the file.

herong$ unzip -t gcc-backup.zip
Archive:  gcc-backup.zip
    testing: gcc/                     OK

[gcc-backup.zip] gcc/hello.c password: TopSecret
...

Table of Contents

 About This Book

 Introduction to Linux Systems

 Cockpit - Web Portal for Administrator

 Process Management

Files and Directories

 "find" - Search for Files

 "more", "head" and "cat" - Read Files

 "split" and "cat" - Split and Join Files

 Truncate Log Files

 "compress/uncompress" - Compressed *.Z Files

 "gzip/gunzip" - Compressed *.gz Files

 "xz/unxz" - Compressed *.xz or *.lzma Files

 "tar -c" and "tar -x" - Create and Extract Archive Files

"zip" and "unzip" - Create and Extract ZIP Files

 "Operation not permitted" Error on macOS

 Users and Groups

 File Systems

 Block Devices and Partitions

 LVM (Logical Volume Manager)

 Installing CentOS

 SELinux - Security-Enhanced Linux

 Network Connection on CentOS

 Software Package Manager on CentOS - DNF and YUM

 vsftpd - Very Secure FTP Daemon

 Postfix - Mail Transport Agent (MTA)

 Dovecot - IMAP and POP3 Server

 Email Client Tools - Mail User Agents (MUA)

 LDAP (Lightweight Directory Access Protocol)

 Administrative Tasks

 References

 Full Version in PDF/EPUB