How do I exclude files while gz in Solaris; I want to gz directories inc sub directories but exclude /temp directory and *.gz files. This is what I have used so far;

tar -cvf api_v2.x.tar.gz --exclude *.gz --exclude ./temp api

which still including the excluding folders.


The Solaris tar command features the X switch to exclude files/directories. Please see the man page man tar for usage.

From the man page:


Exclude.  Use the exclude-file argument as a file containing a list
of relative path names for files (or directories)  to  be  excluded
from the tarfile when using the functions c, x, or t. Be careful of
trailing white spaces. Also beware of leading white spaces,  since,
for each line in the excluded file, the entire line (apart from the
newline) is used to match against the initial string  of  files  to
exclude. Lines in the exclude file are matched exactly, so an entry
like "/var" does not exclude the /var directory if tar  is  backing
up  relative  pathnames.  The entry should read "./var" under these
circumstances. The tar command does not expand shell metacharacters
in the exclude file, so specifying entries like "*.o" does not have
the effect of excluding all files with names suffixed with ".o". If
a  complex list of files is to be excluded, the exclude file should
be generated by some means such as the find(1) command with  appro-
priate conditions.

Multiple  X  arguments can be used, with one exclude-file per argu-
ment. In the case where included files (see -I  include-file  oper-
and)  are  also  specified, the excluded files take precedence over
all included files. If a file is specified in both the exclude-file
and the include-file (or on the command line), it is excluded.

Note the last sentence of the first paragraph stating:

If a complex list of files is to be excluded, the exclude file should be generated by some means such as the find(1) command with appro- priate conditions.

On recent Solaris versions there is also the GNU tar which can be used as gtar with the corresponding man page at man gtar. This version features the --exclude=PATTERN


Example using tar:

/tmp$ find api/

/tmp$ cat api/exclude_from_tar 

/tmp$ tar cvfX api.tar api/exclude_from_tar api
a api/ 0K
a api/a.gz excluded
a api/temp excluded
a api/exclude_from_tar 1K
a api/b 0K

/tmp$ tar tf api.tar 

Example using gtar:

/tmp$ gtar cvf api.tar --exclude="*.gz" --exclude=temp api

/tmp$ tar tf api.tar 
  • Thanks for the tip but this is the closest that I came up with but still dont work :( tar -cvfXX api_v2.x.tar.gz *.gz ./temp api – BoomRamada Jul 16 '19 at 23:36

You need to quote the *.gz pattern ("*.gz") if the current directory contains .gz files, otherwise the shell will expand the pattern before tar sees it.

You will also need to add z to the argument list if you want tar to produce a gzipped archive.

  • The z argument does not exist in tar on Solaris. To create a gzipped archive use tar cf - source | gzip -c > source.tar.gz or gtar -z – Lambert Jul 16 '19 at 11:30
  • The z argument exists in tar on Solaris 11, since 2011. It does not exist on Solaris 10 and older. – alanc Jul 16 '19 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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