Tar directory contents without parent and exclude subdirectories/files

directoryexcludetar

Simplified directory structure:

/root/test
/root/test/dir1
/root/test/dir2
/root/test/dir3
/root/test/dir4

I want to tar /root/test so that when I extract the archive I get dir1 and dir2. I want to exclude dir3 and dir4 and possibly other files and subdirectories depending on the specific application of the command.

I realise there is a lot of tar examples and snippets around the net but I cannot seem to get a specific example or combine the ones available online to make this work as above. Any help would be much appreciated.

Best Answer

How about

cd /
tar cvf /tmp/test.tar --exclude="./root/test/dir[34]" ./root/test