1

I want to make a tarball of a directory, but excluding all files that end with .foo, except for one specific file. This excludes all files with .foo as ending:

tar czf "${nameTarball}" dir/ --exclude '*.foo'

What I'm missing is some sort of --include option. Does it exist? I'm using GNU tar 1.23.

1
  • Yay, 1k views, 0 votes.
    – pfnuesel
    Commented Jun 30, 2020 at 12:21

1 Answer 1

2

There is no --include-style option in GNU tar as far as I’m aware.

In GNU tar, later versions (starting with 1.29) treat --exclude and other filename-selection options in a position-sensitive fashion, so they only affects arguments after them on the command line. So you can include files by naming them before the --exclude option:

tar czf "${nameTarball}" dir/specific.foo --exclude '*.foo' dir/
3
  • I installed a newer version of tar and it works now. Thanks! (I wonder why certain distros use tar that is from 2010!)
    – pfnuesel
    Commented Aug 31, 2018 at 13:34
  • A typical tar implementation that is part of an operating system distro supports -I. This has been added to BSD tar (not bsdtar) in the late 1980s. But If a filename is in the -X file as well, itis excluded. Note that --exclude is a non-portable vendor unique GNU tar extension.
    – schily
    Commented Aug 31, 2018 at 13:59
  • @schily GNU tar supports both -T (equivalent to BSD’s -I, where -T also works) and -X, but as you imply they’re not helpful in this situation since the exclusions given by -X have greater precedence than the files given by -T. Commented Aug 31, 2018 at 14:31

You must log in to answer this question.

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