tar - How to exclude a directory?

Karl Auer kauer at biplane.com.au
Wed Mar 22 13:08:23 UTC 2017


On Wed, 2017-03-22 at 13:22 +0100, Ralf Mardorf wrote:
> tar --exclude=./winOS_2[...]

Don't put anything in front of the name you wish to exclude. It should
look exactly as it appears in the current directory or in the tar file.

mkdir ralf
cd ralf
for i in fred mary john ; do
{
   mkdir $i
   touch $i/blah.txt
}
done
tar --exclude=mary -cvzf test.tar.gz *
tar tvzf test.tar.gz
rm test.tar.gz
cd ..
rm -fr ralf

A trailing slash on mary will make a difference for empty directories;
it means the empty directory will NOT be excluded (because the slash
will only match files IN the directory), so leave the slash off if you
don't want the empty directory either.

Also see tar options -P, --anchored, --no-anchored and any option with
"wildcard" in it. If you have lots of directories you want to exclude,
consider using a tag file and check out the --exclude-tag-* options.

Regards, K.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
http://twitter.com/kauer389

GPG fingerprint: A52E F6B9 708B 51C4 85E6 1634 0571 ADF9 3C1C 6A3A
Old fingerprint: E00D 64ED 9C6A 8605 21E0 0ED0 EE64 2BEE CBCB C38B






More information about the ubuntu-users mailing list