The Server Pages


TheServerPages Articles






Server Administration

Exclude Directories when Using Tar

Author: Wojjie     Posted: 2011-11-27     Viewed: 43,357

When performing regular back ups on a Linux server, "tar" is a very useful tool. To save time and space, you may want to exclude some directories such as the template cache, images, videos, large files or file downloads.

Excluding Directories

When listing directories by using any of the following two methods, make sure NOT to use a trailing forward slash (/) at the end of the directory name as it will not work as intended! I emphasize this as I have wasted some time due to this and I am sure someone else may do the same.

Bad: Excluding Directories will not work
Good: Will work

Exclude Directories Inline

To exclude directories inline (list within the executing command) you want to use --exclude.

Example: Command to exclude directories inline
tar -czvf backup-`date +%m-%d-%Y-%H%M`.tar.gz --exclude ./public_html/templates/cache ./public_html

Excluding Directories from a File

To exclude directories using a file to list everything you will want to use --exclude-from or the short form -X .

Example: Command to exclude directories listed in a file
tar -czvf backup-`date +%m-%d-%Y-%H%M`.tar.gz -X exclude.txt ./public_html
Example exclude.txt

Exclude Multiple Directories Inline

To exclude multiple directories inline you will want to list each directory separated by a comma and encased in {curly brackets}.

tar -czvf backup-`date +%m-%d-%Y-%H%M`.tar.gz \
     --exclude={./public_html/templates/cache,./public_html/templates/compiled,./public_html/images} \


Copyright © 2004-2015: