Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

7zip: How to exclude file types?

Writer Matthew Martinez

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.

4 Answers

From man 7z:

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

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

7z a 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

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' {} \;
0

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.

If you want to exclude the full directory (you may have this extension file separated in directory)

 7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on ~/bkpFile_$(date +"%d_%b_%Y").1.7z /home/ubuntu/projectfile -mx0 '-xr!vendor' '-xr!view' '-xr!documents' 

Here, vendor , view, and documents directory has been excluded.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy