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
Note:
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: