Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to use zip command in Linux

    How to use zip command in Linux

    By RahulJune 22, 20224 Mins Read

    The zip command in Linux is a utility used for packaging a group of files into a zip archive. The zip command can also be used to compress files, making them take up less space on your computer’s hard drive. In addition, the zip command can be used to encrypt files, making it more difficult for unauthorized users to access the contents of the zip archive.

    Advertisement

    To unzip a zip archive, you can use the unzip command. This command will decompress the files in the zip archive, allowing you to access them.

    Zip Command Examples

    Go through the below real examples of Linux zip command.

    1. Zip all files in a directory
    2. This command will create a zip of all files in the /backup directory. I will not archive files under the sub directories recursively.

      zip backup.zip /backup/* 
      
      Output:
      adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/httpd/ (stored 0%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%)

    3. Zip files with wildcard
    4. Use Linux wildcards to archive files of specific extensions only. Like backup only .log extension files in a directory.

      zip backup.zip /backup/*.log 
      
      Output:
      adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%) adding: backup/pm-powersave.log (deflated 15%) adding: backup/wpa_supplicant.log (stored 0%) adding: backup/Xorg.0.log (deflated 83%) adding: backup/Xorg.9.log (deflated 83%) adding: backup/yum.log (deflated 77%)

    5. Zip files recursively
    6. The below command will create an archive recursively with files, directories, and its sub-directories as well. It also maintains the directory structure as source files.

      zip -r backup.zip /backup 
      

    7. Create password protected zip
    8. Sometimes we need to create a password-protected archive. The zip command provides you an option to make password-protected archive files. To create password-protected archive use -e option. This will prompt for a password and confirm the password.

      zip -e backup.zip /backup/*.log 
      
      Output:
      Enter password: Verify password: adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%)

    9. Zip with compression levels
    10. You can also define the compression level with the zip command. There are 10 levels of compression, which range from 0 to 9.

      • -6 is used as default compression level.
      • -0 is used for the lowest level compression.
      • -9 is used for the highest level compression
      zip -9 high-compressed-file.zip /backup/* 
      zip -0 lowest-compressed-file.zip /backup/* 
      

      Check differences between a compressed file

      ls -lh lowest-compressed-file.zip high-compressed-file.zip 
      
      Output:
      -rw-r--r--. 1 root root 50K Apr 11 14:14 high-compressed-file.zip -rw-r--r--. 1 root root 447K Apr 11 14:14 lowest-compressed-file.zip

      You can see the difference between both file sizes.

    11. List content of zip file
    12. Using -l switch with unzip command to list only files inside a zip archive without decompressing it.

      unzip -l backup.zip 
      
      Output:
      Archive: backup.zip Length Date Time Name --------- ---------- ----- ---- 140 04-11-2013 14:07 backup/anaconda.ifcfg.log 11153 04-11-2013 14:07 backup/anaconda.log 15446 04-11-2013 14:07 backup/anaconda.program.log 136167 04-11-2013 14:07 backup/anaconda.storage.log 2722 04-11-2013 14:07 backup/boot.log 211614 04-11-2013 14:07 backup/dracut.log 0 04-11-2013 14:08 backup/httpd/ 1382 04-11-2013 14:07 backup/kadmind.log 1248 04-11-2013 14:07 backup/krb5kdc.log 6485 04-11-2013 14:07 backup/mysqld.log 87 04-11-2013 14:07 backup/pm-powersave.log 0 04-11-2013 14:07 backup/wpa_supplicant.log 30186 04-11-2013 14:07 backup/Xorg.0.log 31094 04-11-2013 14:07 backup/Xorg.9.log 6739 04-11-2013 14:07 backup/yum.log --------- ------- 454463 15 files

    13. Extract a zip file.
    14. The unzip command is used to extract a zip file. Use the below command to simply extract a zip file.

      unzip backup.zip 
      

    15. Check an archive file
    16. Use -t to check and archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum).

      unzip -t backup.zip 
      
       
      
      Output:
      Archive: backup-11Apr2013.zip testing: backup/anaconda.ifcfg.log OK testing: backup/anaconda.log OK testing: backup/anaconda.program.log OK testing: backup/anaconda.storage.log OK testing: backup/boot.log OK testing: backup/dracut.log OK testing: backup/httpd/ OK testing: backup/kadmind.log OK testing: backup/krb5kdc.log OK testing: backup/mysqld.log OK testing: backup/pm-powersave.log OK testing: backup/wpa_supplicant.log OK testing: backup/Xorg.0.log OK testing: backup/Xorg.9.log OK testing: backup/yum.log OK No errors detected in compressed data of backup.zip.

    Wrap Up

    We’ve shown you how to zip files in Linux using the terminal. This is a handy skill that can come in handy when you need to send someone a compressed file or save storage space on your computer. Also, the Unzipping an archive is easy and we’ll show you how to do it quickly and easily.

    unzip command unzip. zip command zip zip example
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    cp Command in Linux (Copy Files Like a Pro)

    dd Command in Linux (Syntax, Options and Use Cases)

    Top 10 JQ Commands Every Linux Developer Should Know

    View 3 Comments

    3 Comments

    1. Serrio Kars on March 13, 2023 11:06 am

      Thank you, I’m a novice in Linux, it was really helpful!

      Reply
    2. Mistral on June 24, 2015 10:33 am

      > zip 9 high-compressed-file.zip /backup/*

      Mhmm I think it is :
      > zip -9 high-compressed-file.zip /backup/*

      Reply
      • Charriers on September 9, 2017 10:54 pm

        Yes! thanks!!!!!

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.