others-how to create tar ball with some excluded files or directories?

1. Purpose

In this post, I would demo how to exclude some files or directories when creating tarballs.



2. Solution

2.1 If you want to exclude specific directory

To exclude specific dir,execute the following command:

[root@iZ23x64v3hjZ opt]# tar zcvf tomcat.tar.gz --exclude tomcat/logs tomcat
tomcat/
tomcat/bin/startup.sh
tomcat/bin/bootstrap.jar
tomcat/bin/catalina.sh
tomcat/bin/configtest.sh
tomcat/bin/digest.sh
tomcat/bin/version.sh
tomcat/bin/tool-wrapper.bat
...
[root@iZ23x64v3hjZ opt]#

2.2 Exclude multiple directories

If you want to exclude multiple directories, you can add –exclude. The following command excludes the logs and libs directories and the file bswen.txt:

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

2.3 Pay attention here

When everyone should pay attention here, when we use tar’s –exclude command to exclude packaging, we cannot add “/”, otherwise the logs directory and the files under it will still be packaged.

wrong command:

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

correct command:

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



3. Summary

In this post, I demonstrated how to package linux directories with some excludes. That’s it, thanks for your reading.