Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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. Any ideas?

EDIT Forgot to mention the operating system, which is Solaris. The tar version there does not support exclusion by name.

share|improve this question

migrated from stackoverflow.com Jun 28 '10 at 13:31

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

up vote 3 down vote accepted
 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.
share|improve this answer
    
For homework, I suggest to point the OP in the right direction instead of posting the answer. –  Aaron Digulla Jun 28 '10 at 11:56
    
man tar is too short for an answer ;) - but I'll keep that in my mind. –  miku Jun 28 '10 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 Jun 28 '10 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 Jun 28 '10 at 14:23
add comment

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}...
share|improve this answer
add comment

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.

share|improve this answer
2  
-1 - how is he supposed to use that with the output of find, possibly lots of files? –  unbeli Jun 28 '10 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??? –  Aaron Digulla Jun 28 '10 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 Jun 28 '10 at 12:02
add comment

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

tar -cvf java.tar -X Exclude $HOME/java
share|improve this answer
    
+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 Jun 28 '10 at 12:13
    
Uh, sry, only works with GNU tar this way... –  Helper Method Jun 28 '10 at 12:59
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.