I need to create a .tgz archive of the following folder

    vendor (folder)
    another (folder)

Just issuing an exclude=index.php etc has unwanted consequences: vendor might in turn have a file bearing that name directly or in one of its sub folders. I haven't been able to establish from the documentation whether I can instruct tar to ignore all file contents in the rest folder but NOT the sub folders under it.

2 Answers 2


For the benefit of those running into this post here is the solution that works - at least on Ubuntu 14.04. For the folders structure I have shown above issue

tar -zcf pack.tgz --exclude-from=/path/to/excludes.list pack

excludes.list contains, in my case


...essentially a list of files to exclude. Points to note

  • Using an absolute path to excludes.list file makes it easier to compose the command to build the archive via a shell command executed in a scripting language.
  • The excluded files are in the form folder/file.ext, relative to the folder being archived.
  • There should be a newline char immediately after each filename entry. In a text editor it is easy to get a space in just after the filename in which case it will not get ignored.

Just use "another/index.php" as the pattern (or even "rest/another/index.php"):

$ mkdir -p pack/rest/vendor
$ mkdir -p pack/rest/another
$ touch pack/rest/{vendor,another}/index.php
$ tar --create --verbose --file  xx.tar --exclude "another/index.php"  pack/

I found that for more complex selections of tar file contents it is easier to use find with pruning, and, or, etc., and until you can generate the exact list of input. And then pipe that into cpio and its --format=ustar output mode to generate a tar file.

  • Not sure on which distro you tried this. On Ubuntu 14.04 I find two issues - firstly the exclude switch does nothing unless it is followed by an = sign. Secondly - with the = - the exclusiion is applied to the parent directory and its child folders which is not what I want.
    – DroidOS
    Commented Jun 26, 2015 at 12:03
  • @DroidOS This was copy and pasted from a Linux Mint 17 session. The only thing I noticed is that there was an extra return inserted during pasting on the tar line so that the final argument pack was not on the correct line. I tried it once more, without problems or = and verified the xx.tar. tar --version gives: tar (GNU tar) 1.27.1
    – Anthon
    Commented Jun 26, 2015 at 12:32
  • Thanks. On Ubuntu 14.04, no = causes the exclude switch to be simply ignored. However, I have found that --exclude-from=excludesfilelist works as expected. I'll put that in an answer in a minute
    – DroidOS
    Commented Jun 26, 2015 at 12:38

You must log in to answer this question.

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