Skip to content

Copy a directory tree with exclusions

How to exclude some files/folders while copying a folder tree.

Tar

To start with, tar and pipes can be used to copy directly trees1

tar cf - $src | tar xvf - -C $dst

A filter file .tarignore excludes stuff similar to .gitignore.23

tar -c -X .tarignore -f - srcfolder | tar xvf - -C dstfolder

The syntax is similar to .gitignore:

.tarignore
.DS_Store
.git
.gitignore

rsync

Use --exclude flag in rsync to exclude certain folder(s). 4

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude

Warning

The excluded directory paths are relative to the sourcefolder.

Comments