Exclude common subdirectories when creating a tarball

I'm creating a tarball of a large codebase managed in ClearCase. Every directory has a sub-directory named ".CC". I'd like to exclude these from my tarball.

I've found Excluding directory when creating a .tar.gz file, but excluding that would appear to require passing each and every .CC directory on the commndline. This is impractical in my case.

Is there a way to exclude directories that meet a particular pattern?

EDIT: I am not asking how to exclude a specific finite list of directories. I am asking how to exclude all directories that end in a particular pattern.


Instead of manually typing --exclude 'root/a/.CC' --exclude 'root/b/.CC' ... you can type $(find root -type d -name .CC -exec echo "--exclude '{}'" ;|xargs)

You can use whatever patterns find supports, or even use something like grep inbetween find and xargs .


The following bash script should do the trick. It uses the answer given by @Marcus Sundman.

#!/bin/bash

echo -n "Please enter the name of the tar file you wish to create with out extension "
read nam

echo -n "Please enter the path to the directories to tar "
read pathin

echo tar -czvf $nam.tar.gz
excludes=`find $pathin -iname "*.CC" -exec echo "--exclude '{}'" ;|xargs`
echo $pathin

echo tar -czvf $nam.tar.gz $excludes $pathin

This will print out the command you need and you can just copy and paste it back in. There is probably a more elegant way to provide it directly to the command line.

*.CC could be exchanged for any other common extension and this should still work.

链接地址: http://www.djcxy.com/p/47082.html

上一篇: 打开一个目录,但不要在存档中存储完整的绝对路径

下一篇: 创建tarball时排除公共子目录