Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»How to Exclude Files from a Zip Archive

    How to Exclude Files from a Zip Archive

    akhilBy akhilJanuary 19, 20154 Mins ReadUpdated:September 4, 2021

    You want to send files but you are facing a problem due to their large size? It has the solution. For this purpose, we have a tool known as zip, which is used to compress all the files and store them in a single folder. Now you can send the compressed zip folder containing all the files easily to anyone. But there is a folder having 100 files and you have to forward this folder excluding some specific files. Now instead of going to the folder and searching the specific files, we can exclude them by using some commands.

    In this article, we will discuss how to make a zip folder by excluding specific files and how it works.

    How to exclude files from a zip archive

    Generally all the Linux distribution have default zip package installed. The syntax of the command with excluding file and directories is as follows:

    zip -r [filename].zip [directory name to zip] -x [file/directory name/path to exclude]
    

    In the above syntax, the “r” flag is used to append files and “x” flag is used to exclude files. Now to understand it more clearly let us consider an example.

    Examples to Excludes Files in Zip Archive

    I have created a sample directory structure including some files. Where a directory named “docs” contains one directory and some files as shown in below screenshot:

    List Directory Tree in Linux
    List Directory Tree in Linux

    Let’s considering the above structure, here is the few examples to learn excluding files from zip archive.

    01. Exclude a Directory from Zip Archive:

    Create an archive file excluding the cache directory. Use the following command to create new archive named “docs.zip” excluding “cache” directory. So in command it will be written as:

    zip -r docs.zip docs -x "docs/cache/*" 
    
    Zip Command to  Exclude a Directory
    Zip Command to Exclude a Directory

    02. Exclude a Single File from Zip Archive

    Define the full file path to exclude a single file from archive file with zip command. For example, to exclude index.html from docs directory, type:

    zip -r docs.zip docs -x "docs/index.html" 
    
    Zip Command to Exclude Single File
    Zip Command to Exclude Single File

    03. Exclude Files with Wildcard from Zip Archive

    You can also use wild card characters to exclude multiple files from zip archive. For example, to exclude all files with “.log” extension, run:

    zip -r docs.zip docs -x "*.log" 
    
    Zip Command to Exclude files With Wildcard Names
    Zip Command to Exclude files With Wildcard Names

    04. Exclude Multiple Files/Directories from Zip Archive

    You can define -x multiple times in a single zip command to exclude multiple files and directories from zip archive.

    zip -r docs.zip docs -x "/docs/README.md" -x "docs/cache/*" 
    
    Zip Command to Exclude Multiple Files
    Zip Command to Exclude Multiple Files

    Real World Examples

    Here is some read world examples, which is frequently used by the users.

    01. Excluding “.git” Directory from Zip Archive

    The source code managed with Git contains a “.git” directory under root directory. Sometimes you may not required to archive this directory. Use the following command to exclude .git directory and it’s contents:

    zip -r mydir.zip myDir -x "*.git*" 
    

    02. Excluding “.svn” Directory from Zip Archive

    Applications source code managed through SVN contains “.svn” directory. Use the following command to exclude the .svn directory from zip archive.

    zip -r mydir.zip myDir -x "*.svn*" 
    

    03. Excluding “node_modules” Directory from Zip Archive

    All the Node.js modules are installed under node_modules directory. While archiving the source code, you can ignore the node_modules directory with below mentioned command:

    zip -r mydir.zip myDir -x "node_modules*" 
    

    Conclusion

    With the advancement of technology, it’s not a big issue to resolve anything. Now we can easily make a single zip file of many files and can email it to anyone easily. In this article, we learned how we can make zip files excluding some files that we don’t want to be a part of the zip file

    extract unzip zip
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Create tar Archive excluding some Files & Directories
    Next Article How To List Databases and Tables in PostgreSQL

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    How To Compare Two Files in Linux

    Updated:August 23, 20215 Mins Read

    1 Comment

    1. Simon on December 8, 2021 5:43 pm

      Hi. You can use a free online tool to create ZIP https://freetools.site/file-compressor/zip

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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