7zip: How to exclude file types?

18,374

Solution 1

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. Another issue is that 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' {} \;
    

Solution 2

From man 7z:

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

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

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

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

The exclude.txt file is a list of files or file types, separated by carriage returns, like this:

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

Solution 3

Maybe extracting from multiple archives wasn't possible at the time of the question.

As of 2020, 7z can extract from multiple archives , but you need to specify them with a special flag:

       -ai[r[-|0]]{@listfile|!wildcard}
              Include archives

docs

Note that when you use this you no longer need to specify the archive_name on the command line (where you passed *), so you need to disable it with the -an flag.

The rest of your flags work as you put them, with the exception of the quotes: they are necessary to avoid interpretation of ! and * by your shell. In bash, double quotes ("..") still allow expansions, so need to use single quotes ('...'). The alternative is to escape these characters, e.g. -x\!\*.html.

So your command becomes:

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

My recommendation is to make the inclusion flag a bit more specific than *, because it will select all the files in the directory. And since you used the e command (not x) and you did not specify an output directory (with -o flag), the current directory will be populated with all the files from your archive. This means that if you run the same command again, the * selector will now select not only the archives, but also all the extracted files.

Share:
18,374

Related videos on Youtube

daka
Author by

daka

Updated on September 18, 2022

Comments

  • daka
    daka over 1 year

    I want to exclude the following file types:

    • epub
    • pdf
    • html (upper case too)
    • azw3
    • mobi
    • opf
    • txt

    I have this so far which doesn't seem to work, i get an error saying "Incorrect Command Line".

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

    I also tried the above command without double quotes.

    I created the above command using info from here (for windows) but it doesn't seem to work under Linux.

  • daka
    daka almost 9 years
    I have a lot of archives which need extracting, hence the use of the wildcard at the end to extract ALL archives.
  • kos
    kos almost 9 years
    @sudoman Are you sure this is supported by 7z? Mind to try with a single archive?
  • kos
    kos almost 9 years
    @Ron I agree, but the problem here is that OP can't extract multiple archive at once; however upvoted your post
  • kos
    kos almost 9 years
    @sudoman I've found another flaw in your original command, plus I added a couple of way to extract multiple archives at once
  • muru
    muru over 6 years
    Why sudo? And what does this add to the existing answers?