How to exclude directories when backing up a website using tar in Linux

| | 1 min read

Some times we wish to exclude some files and directories when we try to take backup of a website. With tar it is possible. Use the --exclude option. For example, if you want to exclude .git folder and the imagecache directory inside the files folder, use the command:

tar -zcvf --exclude ".git/*" --exclude ".git" --exclude "sites/default/files/imagecache/*" --exclude "sites/default/files/imagecache" public_html

if you did not add --exclude "sites/default/files/imagecache" an empty directory imagecache will be there in the tarball.

Here is the meaning of zcvf (the options used to create the tarball):

z - use gzip to compress. if you use j the system will use bzip compression.
c - compress. use x to extract.
v - verbose mode. show what is happening in the console. this is not required.
f - filename, use tar.gz for z and tar.bz2 for j