Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to Exclude Files from a Zip Archive

    How to Exclude Files from a Zip Archive

    By akhilJanuary 10, 20234 Mins Read

    Do 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.

    Advertisement

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

    Syntax

    Generally, all the Linux distributions have default zip packages installed. The syntax of the command excluding files and directories is as follows:

    1
    zip -r [filename].zip [directory_name_to_zip] -x [file_to_exclude]

    In the above syntax, the “-r” flag is used to append files, and the “-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 below screenshot:

    List Directory Tree in Linux
    List Directory Tree in Linux

    Let’s consider the above structure, here are a few examples to learn about excluding files from the zip archive.

    01. Exclude a directory

    Create an archive file excluding the cache directory. Use the following command to create a new archive named “docs.zip” excluding the “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

    Define the full file path to exclude a single file from the archive file with the zip command. For example, to exclude index.html from the 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

    You can also use wild card characters to exclude multiple files from the zip archive. For example, to exclude all files with the “.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

    You can define -x multiple times in a single zip command to exclude multiple files and directories from the 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

    05. Exclude “.git” directory

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

    zip -r filename.zip my_dir -x "*.git*" 
    

    06. Exclude “.svn” directory

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

    zip -r filename.zip my_dir -x "*.svn*" 
    

    07. Excluding “node_modules” Directory from Zip Archive

    All the Node.js modules are installed under the node_modules directory. While archiving the source code, you can ignore the “node_modules” directory with the 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

    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 2 Comments

    2 Comments

    1. Sergiu Postica on March 14, 2023 12:45 pm

      Thank you for the article, it was really helpful!

      Reply
    2. 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

    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.