Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zipping everything in directory with 7z except one file or one file type

Tags:

cmd

zip

7zip

i'd like to zip everything except one file

7z a -tzip files.zip *

this will zip all the files in my current directory.. is there a way I can tell it to not zip one file or one file type ?

like image 898
penguinsource Avatar asked Nov 18 '12 23:11

penguinsource


People also ask

How do I exclude a zip file?

You can define -x multiple times in a single zip command to exclude multiple files and directories from zip archive.

What is the difference between a zip file and a 7z file?

The 7-zip application can read/write 7-zip format (usually an extension of . 7z) but it can also handle zip format. A zip file is the conventional ZIP compression format while the 7-zip format is made by 7zip and is supposedly better and more efficient than the standard zip format.

How do I zip an entire folder?

To zip (compress) a file or folder Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.


2 Answers

Per the 7za command-line help, you use the -x switch to do this:

-x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames

To exclude the file foo.txt you would add:

-x!foo.txt

To exclude all .html files (*.html) you would add:

-x!*.html

You can add multiple -x entries to exclude multiple filenames and/or wildcards in one zip command. Adding the following will exclude foo.txt and *.html:

-x!foo.txt -x!*.html

So with your example, this would add all files to files.zip EXCEPT files named "FILENAME" or that matched the *.extension wildcard:

7za a -tzip files.zip * -x!FILENAME -x!*.extension
like image 81
Joshua McKinnon Avatar answered Oct 21 '22 17:10

Joshua McKinnon


If you are using batch script do not forget to escape ! mark.

7z a -xr^^!*.xml "dest_dir.zip" "Source_dir"

Took me long time to find out:)

Thank you.

like image 28
shreesha Avatar answered Oct 21 '22 16:10

shreesha