7zip: How to exclude file types?

Solution 1:

From man 7z:

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

To exclude file (or types) you can use the following command:

7z a [email protected] backup.7z /whatever/dirs/or/files

Notice -xr instead of -x. The r indicates recursive so it can match excluded files in deep folder hierarchies

The file exclude.txt is a list separated by carriage returns like this:

*.epub
*.pdf
*.html 
*.HTML
*.azw3
*.mobi
*.opf
*.txt

Solution 2:

7z only accepts a single archive within its arguments, but you're passing a wildcard which expands to the full content of the current working directory; anothe issue is that also the wildcards within the arguments will expand as well, either if non-quoted or double-quoted.

So you should only extract a single archive per command; you should remove the wildcard at the end, specify a single archive and single-quote the arguments:

7z e '-x!*.epub' '-x!*.pdf' '-x!*.html' '-x!*.azw3' '-x!*.mobi' '-x!*.txt' '-x!*.HTML' '-x!*.opf' archive.7z

To extract multiple archives at once however you can use multiple methods:

  • bash:
for archive in *.7z; do 7z e '-x!*.epub' '-x!*.pdf' '-x!*.html' '-x!*.azw3' '-x!*.mobi' '-x!*.txt' '-x!*.HTML' '-x!*.opf' "$archive"; done
  • find:
find . -maxdepth 1 -type f -iname "*.7z" -exec 7z e '-x!*.epub' '-x!*.pdf' '-x!*.html' '-x!*.azw3' '-x!*.mobi' '-x!*.txt' '-x!*.HTML' '-x!*.opf' {} \;