Tar multiple folders – Tar exclude folders

Tar multiple folder and tar excluding folder, here we can see how to do it

Tar: multiple folders

If you need to “zip” multiple folders using TAR command, just list the folders at the end of the command.

For exempla if you need to zip /etc/folder1, /var/www/html/folder2 and /home/folder3 just list them at the end of the command

tar -czf mybackup.tar.gz /etc/folder1 /var/www/html/folder2 /home/folder3

Tar: exclude folders

If you want to zip one folder but not all the folders inside it, you should list the exluded folders before the folder you need to zip, using the opzion –exclude.

For expample: you have folder “mainfolder” and inside this folder there are 5 folders like “child1, child2, child3, child4 and child5”, something like this:

home
├── mainfolder
│   ├── Child1
│   │   └── file1a
│   ├── Child2
│   │   └── file2a
│   ├── Child3
│   │   └── file3a
│   │   └── file3b
│   ├── Child4
│   │   └── file4a
│   ├── Child5
│   │   └── file5a
│   │   └── file5b
│   │   └── file5c

if you want to zip mainfolder excluding folders “Child2” and “Child3”, here is the command

tar --exclude='/home/mainfolder/Child1' --exclude='/home/mainfolder/Child2' -zcvf /home/mybackup.tgz /home/mainfolder/*