Reputation: 41
Regarding zip, I'm looking for a way to ensure a file is added to an archive independent of what has been passed to the archive creation exclusion list.
In my instance, I've developed an application that allows users to specify their own exclusion filter when creating a zip.
However, I need to ensure a couple custom files are always added to the archive, independent of what was specified in the filter.
For example:
So if I execute
zip -rq -i foo.bar -x "*.bar"
foo.bar will not be included in the archive.
So it comes down to:
A solution I've come up with is to take two passes at it - first create an archive with the exclusion list and then grow the archive by foo.bar, but I'm looking for a way to do it in a single shot.
Thanks
Upvotes: 4
Views: 1342
Reputation: 166
The -i
option is used to include only the specified files. With pattern "foo.bar" we only include the file named foo.bar leaving others.
The -x
option is used to exclude files. With pattern *.bar
we exclude the foo.bar
file that we included before.
Also you must consider that the patterns will be resolved as pathname expansion not as POSIX or Perl regular expresion. If you want do patterns more complex you can use extended patterns.
By example:
shopt -s extglob
zip -rq foo.zip . -i ?(foo.bar|!(*.bar))
shopt -u extglob
Or:
shopt -s extglob
zip -rq foo.zip . -x !(!(*.bar)|foo.bar)
shopt -u extglob
But the last commands aren't recursive because the patterns doesn't contemplate subdirectories. Although we use -r
option we include only the files in pattern. The manual page tell us about write \
before of *
for apply recursively, but not work with all patterns.
Also you can use other commands to get list of files to include.
By example:
zip -rq foo.zip . -i `find ./ -not -name "*.bar" -o -name "foo.bar"`
Or:
zip -r foo.zip . -x `find ./ -name "*.bar" -a -not -name "foo.bar"`
Upvotes: 1