linux tar compression method to exclude a folder

  • 2020-05-14 05:51:51
  • OfStack

It is very simple to use tar-zcvf test.tar.gz test directly.

Most of the time, we want to package a certain directory, and this directory has several 10 subdirectories and subfiles, we need to exclude one or two of them when packaging.

At this point, when we package with the tar command, we add the parameter --exclude will do the trick.

Such as:

We will take tomcat as an example. When packaging, we will exclude the tomcat/logs directory. The command is as follows:

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

If you want to exclude multiple directories, add --exclude. The following command excludes logs and libs directories and files xiaoshan.txt:

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

Here's one thing to note:

Everyone knows that linux will automatically complete the directory name when using the tab key, which is very convenient and often used.

If we press tab when we type tomcat/lo, the command line will automatically generate tomcat/logs/. For directories, there will be an extra "/" at the end.

Note here that when we use tar's --exclude command to exclude packaging, we cannot add "/", otherwise we will still package the logs directory and the files under it.

Incorrect writing:

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

Correct writing:

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


Related articles: