Create a 7z Archive with 7-Zip Command Line

This guide provides a straightforward walkthrough on creating a basic .7z archive using the 7-Zip command-line interface. You will learn the core command syntax, how to archive individual files, and how to compress entire folders quickly on both Windows and Linux environments.

The Basic Command Syntax

The primary command used to create or add files to an archive in 7-Zip is the a (add) command. The base syntax requires the executable, the command, the desired archive name, and the target files or folders:

7z a <archive_name.7z> <target_files_or_folders>

(Note: Depending on your operating system or installation, the executable name might be 7z, 7za, or 7zr.)

Compressing a Single File

To compress a single file into a .7z archive, specify the output archive name followed by the target file:

7z a archive.7z document.txt

This creates a new archive named archive.7z containing document.txt in the current working directory.

Compressing Multiple Files

To compress multiple specific files into one archive, list them with spaces separating each filename:

7z a documents.7z file1.txt file2.pdf image.png

You can also use wildcards to compress all files of a specific type:

7z a textfiles.7z *.txt

Compressing an Entire Folder

To compress an entire folder along with all of its contents and subdirectories, provide the folder path as the target:

7z a backup.7z ./my_folder

If you only want to include the contents of the folder without creating the root folder structure inside the archive, append an asterisk:

7z a backup.7z ./my_folder/*

Specifying Compression Levels

By default, 7-Zip uses standard compression (level 5). You can adjust this using the -mx switch followed by a number from 0 to 9:

  • -mx0: No compression (Store)
  • -mx1: Fastest
  • -mx5: Normal (Default)
  • -mx9: Ultra (Maximum compression)

Example creating an ultra-compressed archive:

7z a -mx9 project_archive.7z ./project_files