Friday, February 24, 2012

Exclude directories from tar

All I wanted was to exclude a couple of directories while creating a tar file; after extensive googling for almost 3 days I got a number of examples using --exclude option; but none of them resolved the problem and syntax were different everywhere.

Within inches from going for the alternative approach, of listing files and directories to be included, and passing it on to tar as source; My colleague's search came with another approach, and it finally worked.

Lets cut to the chase and see how to resolve this problem:

The best way is to create a list of all the directories to be excluded in a file, one entry per row with no extra spaces towards end, and pass it as an parameter to tar.

Here goes the Syntax:
tar cvfX path_of_the_tar path_of_the_exclude_file path_of_directory_to_tar

Let’s first create a file with all directories we want to exclude:
$ cat excluded-files.txt
test/a
test/b/ba


Now just use the syntax with the file name which created earlier:
$ tar cvfX test.tar excluded-files.txt test
a test/ 0K
a test/a excluded
a test/b/ 0K
a test/b/ba excluded
a test/b/bb/ 0K
a test/b/bb/bba.txt 0K
a test/c/ 0K
a test/c/ca/ 0K
a test/c/ca/caa 0K
a test/c/cc/ 0K
a test/c/cc/cca 0K


If you see excluded in the verbose output; you are done!!!

4 comments:

  1. Reference: http://www.unix.com/solaris/63549-tar-exclude-list.html

    ReplyDelete
  2. Finally, someone who was able to explain this well. Thanks!

    ReplyDelete
  3. Thanks , i went through almost 10 links to get it working on solaris . Spent almost an hr to figure it out .Thanks again .

    ReplyDelete