7z Command

The 7z command is a powerful command-line file archiver that supports a high compression ratio. It is part of the 7-Zip open-source project and is widely used for creating and extracting archives.

Syntax

7z <command> [<switches>...] <archive_name> [<file_names>...]

Description

The 7z command is used for archiving files. It supports various compression methods and formats, with its native 7z format offering one of the best compression ratios. It can also handle other popular formats like ZIP, GZIP, BZIP2, and TAR.

Common uses include:

  • Creating new archives
  • Extracting files from archives
  • Listing contents of an archive
  • Updating existing archives
  • Testing archive integrity

Common Commands and Switches

Command/Switch Description
a Add files to archive
x Extract files with full paths
e Extract files without paths
l List contents of archive
u Update files in archive
t Test archive integrity
-o{dir} Set output directory (for extraction)
-p{password} Set password
-m{method} Set compression method

Examples

Create a 7z archive

7z a myarchive.7z file1.txt folder/

Creates myarchive.7z containing file1.txt and folder/.

Extract an archive with full paths

7z x myarchive.7z -o/home/user/extracted_files/

Extracts myarchive.7z to the specified directory, preserving the original directory structure.

Extract an archive to current directory (without paths)

7z e myarchive.7z

Extracts all files from myarchive.7z directly into the current working directory.

List contents of an archive

7z l myarchive.7z

Lists all files and folders within myarchive.7z.

Create a password-protected archive

7z a -pYourPassword secret.7z confidential.txt

Creates a password-protected archive named secret.7z.

See also