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

20,158

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.

Share:
20,158
penguinsource
Author by

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, 2020

Comments

  • penguinsource
    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
    Samaursa over 9 years
    For future reference: -x!*.extension doesn't work (at least for recursive sub-directories). -x!*extension does work.
  • Chris Kolenko
    Chris Kolenko about 9 years
    In powershell use "-xr!*.extension" for ignoring all *.extension recursively. Pay close attention to the quotes
  • craig
    craig over 8 years
    To exclude Git-related files and folders in PowerShell, add "-xr!.git\" and "-xr!.gitignore". Note the double quotes (") as mentioned by @ChrisKolenko.
  • mrhotroad
    mrhotroad over 3 years
    I used 7z command in GitLab CI and it was stuck. "-xr^^!*.xml" helped. Thank you for the solution!
  • johny why
    johny why over 2 years
    Your 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
    nenea over 2 years
    I 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"