tar exclude folders

From WebHostingNeeds.com
Jump to: navigation, search

To exclude a file when backing up with tar, you can use option --exclude

If you want a list of files excluded when taking backup, use the option

       -X, --exclude-from FILE
              exclude files listed in FILE

With the help of -X option, you can exclude a folder and all files inside. To do this, you need to create a list of files in the folder with find command.

EXAMPLE

I want to take backup of folder

/home/USERNAME/public_html

But i do not want to include files inside 2 sub folders.

/home/USERNAME/public_html/flvideo/
/home/USERNAME/public_html/video/

This can be done with following commands

find /home/USERNAME/public_html/flvideo/ -iname '*' > my-excludes
find /home/USERNAME/public_html/video/ -iname '*' >> my-excludes

tar cvf 1.tar /home/USERNAME/public_html -X my-excludes