Tutorial :tar - exclude does not work


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.


 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.  


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}...  


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.


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

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


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »