13

How do I exclude hidden directories when creating an archive using 7zip's command line version?

I tried -x!".*", but that didn't work.

nQk
  • 165
  • 1
  • 2
  • 6
  • If you know the pattern of hidden file then its very easy like .svn or .git etc.. but if there is no pattern in that case you must go for staging. – prashant thakre Apr 20 '15 at 14:01

4 Answers4

19

You need to add the r ("recurse") flag to the -x option if you want it to match files inside subdirectories.

For example, the following creates an archive from the whole directory tree under folder/ except for any files that start with a dot:

7z a -xr'!.*' archive.7z folder/
smls
  • 5,738
  • 24
  • 29
12

I had the same problem on windows 7 64bit 7zip.

After doing some research I found the following points:

1) single/double quotes ' " does not work on windows - 7zip says incorrect wirdcard

2) excluding based on file/folder attributes is not possible - only option is either to exclude with wild cards or make an exclude list.

3) in -x option, file is denoted as < path>\< filename.ext> and a folder as < path>\< folder>/ (with a slash at the end)

4) format 1: with ! mark (pattern directly with command) you can give something like:

  a) 7z a -xr!<path>\<folder to exclude>/ archive.7z <zip folder>/

This excludes .svn folder in any path from zip folder recursively

  b) 7z a -xr!*\.svn/ archive.7z <folder>/  

5) format 2: with @ symbol you can give exclude list like this:

  a) 7z a -xr@<7z exclude list file> <archive name>.7z <folder>/

where an exclude list file can have:

  *\.svn/
  *\output/
  *\Document/
  *\Measurements/
  *.xlsx
  *.bak

my favorite option is to use an exclude list

  • Single quotes worked for me inside a PowerShell script. – Sébastien Mar 25 '19 at 16:00
  • Thanks for the detailed explanation and examples! When doing a `7z a -xr!*\node_modules/ archive.7z d:\Workspace\my-project` using the trailing slash for node_modules folder or not (your point 3) ) did not make a difference though. – hennzen Jan 24 '20 at 09:19
  • Thanks for the info and appreciation. The newer versions may have modified -x option and interpretation. I will check again and modify my answer. – Prasad Narasimha May 26 '20 at 22:27
3

this would work for ignoring hidden files ... 7z.exe a -xr!.git\ -xr!*~ ".zip"

Pavan Nayakanti
  • 301
  • 3
  • 3
2

I had a problem on Win64 and an exclude file. I couldn't get the .git folder excluded. The simple ".git\" didn't work, neither any other usual patterns. In the end excluding "*git\" worked (note: no dot).

Cornel Masson
  • 1,352
  • 2
  • 17
  • 33