how to exclude directories while using tar command

A quick post to teach on how to exclude directories while using tar command in linux.
Most of the developers/system administrator should know how to using tar command to create a archive/backup for an application.

But I am facing some difficulty to use the tar command exclude multiple directories until I found this post.

// TAR COMMAND SYNTAX
tar czvf <ARCHIVE_FILE_NAME> <PATH_TO_ARCHIVE>
 
// TAR COMMAND SAMPLE
tar czvf backup.tar.gz /var/www/html
 
// TAR COMMAND TO EXCLUDE IMAGES DIRECTORY 
tar czvf backup.tar.gz /var/www/html --exclude='/var/www/html/images'
 
// TAR COMMAND TO EXCLUDE MULTIPLE DIRECTORIES
tar czvf backup.tar.gz /var/www/html --exclude='/var/www/html/images' --exclude='/var/www/html/cache'

Hope this will solve your problems.
Cheers.

Leave a Reply