7z file archiver

File archiver with a high compression ratio. I use it to extract and create archives in various formats.

NOTE

DO NOT USE the 7-zip format for backup purpose on Linux/Unix. 7-zip does not store the owner/group of the file, instead you can use tar.

  • to back up a directory: tar cf - directory | 7z a -si directory.tar.7z
  • to restore your backup: 7z x -so directory.tar.7z | tar xf -

Usage:

7z [Args] [archive.7z] [files / folders to archive]

Args:
    a       add
    l       list
    x       extract with full paths, preffered method
    e       extract, this command copies all extracted files to one directory
    d       delete
    t       test
    u       update

7z exit codes:

0       normal (no errors or warnings)
1       warning (non-fatal errors)
2       fatal error
7       bad cli arguments
8       not enough memory for operation
255     process was interrupted

Common usage examples

Add all files in dir1 ti archive.7z with maximum compression

7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z dir1`
# -t7z        7z archive
# -m0=lzma    lzma method
# -mx=9       level of compression = 9 (ultra)
# -mfb=64     number of fast bytes for lzma = 64
# -md=32m     dictionary size = 32 Mb
# -ms=on      solid archive = on

Add (store) all files in dir1 to archive.7z with No Compression

7z a -m0=copy archive.7z dir1

Add a file or directory to a new or existing archive:
7z a archive.7z file_or_directory

Encrypt an existing archive (including filenames):
7z a encrypted.7z -ppassword -mhe=on archive.7z

Extract an archive preserving the original directory structure:
7z x archive.7z

Extract an archive to a specific directory:
7z x archive.7z -opath/to/output

Extract an archive to stdout:
7z x archive.7z -so

Archive using a specific archive type:
7z a -t7z|bzip2|gzip|lzip|tar|zip archive.7z file_or_directory

List the contents of an archive:
7z l archive.7z

List available archive types:
7z i