tar - exclude does not work

3

2

I've supposed to archive a directory with tar and exclude some files. I have a directory $HOME/java which includes some .java and .class files. What I want to do is exclude all .class files using the -X exclude option.

I've created an Exclude file by using find

$ find $HOME/java -name "*.class" > Exclude

Then tried to archive the directory

$ tar -cvfX java.tar Exclude $HOME/java

But somehow it does not do the exclude. The tar version on Solaris does not support exclusion by name.

user27076

Posted 2010-06-28T11:52:42.350

Reputation: 234

Answers

3

 tar -cvf java.tar --exclude="*.class" $HOME/java

From the man pages:

--exclude pattern

    Do not process files or directories that match the specified pattern.  
    Note that exclusions take precedence over patterns or filenames 
    specified on the command line.

miku

Posted 2010-06-28T11:52:42.350

Reputation: 422

For homework, I suggest to point the OP in the right direction instead of posting the answer. – Aaron Digulla – 2010-06-28T11:56:47.210

man tar is too short for an answer ;) - but I'll keep that in my mind. – miku – 2010-06-28T11:57:47.097

2@Aaron - I suspect that the homework aspect isn't on using tar, but rather that he needs to turn in his homework as a tarball. Explicit directions, in this case, are probably acceptable. – tvanfosson – 2010-06-28T11:59:02.993

1--exclude is only for GNU tar though, not the Solaris native tar. (Though the original poster may want to check if GNU tar is also installed on the system, perhaps in /usr/sfw/bin, /usr/local/bin, /usr/gnu/bin, /opt/sfw/bin or /opt/csw/bin.) – alanc – 2010-06-28T14:23:46.127

4

Your syntax is perfectly correct for Solaris version of tar, in fact I just tried it on Solaris 8 and 10 as root and non-root user.

Are you sure that it did not exclude it? In verbose mode -v option, the excluded file is still listed and says "excluded" instead of the size as the last field.

Are there any error messages? If it can't find or read exclude file, it will say so....

 tar c[BDeEFhilnopPqTvw@[0-7]][bfk][X...] [blocksize]
     [tarfile] [size] [exclude-file]...
     {file | -I include-file | -C directory file}...

TD1

Posted 2010-06-28T11:52:42.350

Reputation: 155

Finally stumbled across this question and answer having spent too long looking at tar on Solaris - the 'exclude file' rather than a list of filenames (one in my case) is not intuitive. – Dave Richardson – 2014-09-09T13:26:17.273

1

Tar has an option to exclude files by name pattern. Try that one instead.

If you want to use an exclude file, then make sure the paths (i.e. the ones which tar outputs and the ones in the file) are exactly the same. So while "a//b" is the same as "a/b" on the command line, it's different for tar.

Aaron Digulla

Posted 2010-06-28T11:52:42.350

Reputation: 6 035

2-1 - how is he supposed to use that with the output of find, possibly lots of files? – unbeli – 2010-06-28T11:57:32.743

What are you suggesting? That he can't open the file "Exclude" with an editor and compare it against the output of the tar command above??? – Aaron Digulla – 2010-06-28T12:00:56.857

2@Aaron Digulla you've edited your answer, adding the second paragraph. My comment was for the first line, suggesting excluding files by name, which is obviously not practical – unbeli – 2010-06-28T12:02:59.827

1

If you really want to use -X option, try this:

tar -cvf java.tar -X Exclude $HOME/java

unbeli

Posted 2010-06-28T11:52:42.350

Reputation: 314

+1 Strange enough, that works, although I've read it different in Unix Power Tools, Paragraph 38.12 Getting tar's Arguments in the Right Order. – None – 2010-06-28T12:13:26.803

Uh, sry, only works with GNU tar this way... – None – 2010-06-28T12:59:43.587

1

Note that if you need to take care aboute relative paths.

So you need to use ./fileToExclude in the Exclude file – not the full path file name.

David

Posted 2010-06-28T11:52:42.350

Reputation: 11