3

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.

5 Answers 5

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.
4
  • For homework, I suggest to point the OP in the right direction instead of posting the answer. Commented Jun 28, 2010 at 11:56
  • man tar is too short for an answer ;) - but I'll keep that in my mind.
    – miku
    Commented Jun 28, 2010 at 11:57
  • 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
    Commented Jun 28, 2010 at 11:59
  • 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
    Commented Jun 28, 2010 at 14:23
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}...
1
  • 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. Commented Sep 9, 2014 at 13:26
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.

3
  • 2
    -1 - how is he supposed to use that with the output of find, possibly lots of files?
    – unbeli
    Commented Jun 28, 2010 at 11:57
  • 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??? Commented Jun 28, 2010 at 12:00
  • 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
    Commented Jun 28, 2010 at 12:02
1

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

tar -cvf java.tar -X Exclude $HOME/java
2
  • +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.
    – Helper Method
    Commented Jun 28, 2010 at 12:13
  • Uh, sry, only works with GNU tar this way...
    – Helper Method
    Commented Jun 28, 2010 at 12:59
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .