cbrdy
cbrdy

Reputation: 812

How to zip files with exclude list in linux/unix

I have a list of files that I want to zip but I also have a list to exclude files and do not want them to be included in the zip archive.

so I have created a exclude.lst file and it has absolute path and filenames in it.

sample exclude file

/home/logs/apache/access.log
/home/logs/tomcat/catalina.out

but after using the below command, the zip command is not excluding the files rather archiving them.

zip archives.2012.zip /home/logs/ [email protected]

how can I overcome this ? and is there any other way to archive files by excluding the above files.

Upvotes: 10

Views: 14356

Answers (4)

Mars
Mars

Reputation: 2572

Old question, so zip may have changed since then, but from the man pages:

$man zip | grep -A2 exclude

Also possible:
zip -r foo foo [email protected]
which will include the contents of foo in foo.zip while excluding all the files that match the patterns in the file exclude.lst.

I've confirmed that this works in Ubuntu 18, with each pattern/filename on a separate line (haven't tried separated by spaces)

Upvotes: 2

Jonathon Hill
Jonathon Hill

Reputation: 3495

It is possible to do this in two steps:

zip archive.zip -r -@ < include.lst
zip archive.zip -d -@ < exclude.lst

Upvotes: 0

FullStack Alex
FullStack Alex

Reputation: 2093

Just a short addition to CBR's answer for the case you have a bunch of files to exclude (in my case files bigger than 10MB):

do_not_archive=$(find relative/path/to/directory -type f -size +10000000c)
zip -r backup_without_files_bigger_than_10mb.zip relative/path/to/directory -x $do_not_archive

Upvotes: 0

cbrdy
cbrdy

Reputation: 812

Instead of creating exclude.lst file, I'm assigning all the exclude files to a variable and passing those to the -x option in the zip.

For example

do_not_archive=/home/logs/apache/access.log /home/logs/tomcat/catalina.out

Then use zip as shown below

zip archives.2012.zip /home/logs/ -x $do_not_archive

Upvotes: 6

Related Questions