Linux tar compression to exclude a folder

Source: Internet
Author: User

Linux tar compression to exclude a folder

Generally, it is easy to package directly with the tar command. You can directly use tar-zcvf test.tar.gz test.

In many cases, we need to package a directory with dozens of sub-directories and sub-files, we need to exclude one or two directories or files during packaging.

In this case, we can add the -- exclude parameter when packaging with the tar command.

For example:

Take tomcat as an example. to exclude the tomcat/logs directory during packaging, run the following command:

Tar-zcvf tomcat.tar.gz -- exclude = tomcat/logs tomcat

If you want to exclude multiple directories, add the -- exclude command. For example, run the following command to exclude two directories, logs and libs, and the xiaoshan.txt file:

Tar-zcvf tomcat.tar.gz -- exclude = tomcat/logs -- exclude = tomcat/libs -- exclude = tomcat/xiaoshan.txt tomcat


We all know that Linux will automatically complete the directory name when using the tab key, which is very convenient and commonly used.

If we press the tab key when entering tomcat/lo, the command line will automatically generate tomcat/logs/. For the directory, there will be an additional "/"

When we use the tar -- exclude command to exclude packaging, we cannot add "/". Otherwise, we will package the logs directory and its files.

Incorrect syntax:

Tar-zcvf tomcat.tar.gz -- exclude = tomcat/logs/-- exclude = tomcat/libs/tomcat

Correct syntax:

Tar-zcvf tomcat.tar.gz -- exclude = tomcat/logs -- exclude = tomcat/libs tomcat

Exclude the parameters of a directory or file during Linux tar Compression

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.