Zipping everything in directory with 7z except one file or one file type
Solution 1
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:
7z a -tzip files.zip * -x!FILENAME -x!*.extension
Solution 2
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.
penguinsource
Founder of a fun start-up based in sunny Boca Raton, Florida Automated Employee Performance Reviews - GetSpeedBack.com Contact me at [email protected]
Updated on March 15, 2020Comments
-
penguinsource about 4 years
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 ?
-
Samaursa over 9 yearsFor future reference:
-x!*.extension
doesn't work (at least for recursive sub-directories).-x!*extension
does work. -
Chris Kolenko about 9 yearsIn powershell use "-xr!*.extension" for ignoring all *.extension recursively. Pay close attention to the quotes
-
craig over 8 yearsTo exclude Git-related files and folders in PowerShell, add
"-xr!.git\"
and"-xr!.gitignore"
. Note the double quotes ("
) as mentioned by @ChrisKolenko. -
mrhotroad over 3 yearsI used 7z command in GitLab CI and it was stuck. "-xr^^!*.xml" helped. Thank you for the solution!
-
johny why over 2 yearsYour example assumes the target dir is the current dir. How would you structure the command if your target dir isn't the current dir?
-
nenea over 2 yearsI think there is one too many ^ here...^^ didn't work for me, this worked for me: 7z a -xr^!*.xml "dest_dir.zip" "Source_dir"