1

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

pack
  |
  rest
    |
    vendor (folder)
    |
    ----index.php
    |
    ----info.log
    | 
    another (folder)
    |
    ---index.php
    |
    ---info.log
    |
    ---somefile.someext 
   |
   somefolder1
   |
   somefoldern
   |
   somefiles.somexts

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.

0

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/
pack/
pack/rest/
pack/rest/another/
pack/rest/vendor/
pack/rest/vendor/index.php

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.

| improve this answer | |
  • 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 Jun 26 '15 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 Jun 26 '15 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 Jun 26 '15 at 12:38
0

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

rest/index.php
rest/info.log
rest/filetoexclude.ext

...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.
| improve this answer | |

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.