How to Create and Extract 7-Zip Compressed Archives in Ubuntu Terminal

The 7-Zip (.7z) compression format is renowned for delivering significantly higher compression ratios than standard ZIP or GZIP formats. When transferring large datasets, backing up directory trees, or sending archives across bandwidth-constrained networks, using 7-Zip can reduce file sizes by thirty to fifty percent more than traditional compression tools. While desktop users can manage 7z archives using Ubuntu’s graphical Archive Manager, mastering the command-line utility provides unmatched speed, scriptability, and robust encryption options for everyday Linux users.

Why Use 7-Zip on Ubuntu?

While native Linux utilities like tar, gzip, and zip are universal, the open-source 7z format offers several distinct advantages:

  • Superior Compression Ratios: 7-Zip utilizes the advanced LZMA and LZMA2 compression algorithms, yielding substantially smaller archives than standard zip formats.
  • Strong AES-256 Encryption: 7z archives support military-grade AES-256 bit encryption, allowing you to encrypt both the files themselves and the archive file names.
  • Cross-Platform Compatibility: 7z archives can be opened effortlessly on Windows, macOS, Android, and other Linux distributions without compatibility hurdles.

Installing the p7zip Utility on Ubuntu

On Ubuntu, the 7-Zip command-line tool is packaged as p7zip-full, which provides complete support for creating, testing, and extracting 7z archives:

  1. Open your Terminal emulator by pressing Ctrl + Alt + T.
  2. Update your local package index:
    sudo apt update
  3. Install the p7zip-full package using APT:
    sudo apt install -y p7zip-full
  4. Verify the installation by checking the utility version:
    7z --help

How to Create a 7z Archive from Files or Folders

To compress files or entire folders into a new 7z archive, use the a (add) command:

  1. Compressing a Single File:
    7z a backup_document.7z financial_report.xlsx
  2. Compressing an Entire Directory: To compress a folder and all its contents recursively:
    7z a project_archive.7z ~/Documents/ProjectFiles/
  3. Compressing Multiple Specific Files: List the files separated by spaces:
    7z a photos.7z image1.jpg image2.png image3.jpg

Creating Password-Protected and Encrypted 7z Archives

If you are archiving sensitive personal documents, financial data, or server backups, you can protect the archive with AES-256 encryption using the -p flag:

  1. Execute the archive command with the password flag:
    7z a -p confidential.7z ~/Documents/TaxRecords/
  2. The terminal will prompt you to type your password twice. (Characters will not display on the screen for security).
  3. Encrypting Filenames (-mhe=on): By default, an encrypted archive protects the file contents, but anyone can still view the list of file names inside. To encrypt the header so nobody can even see what files exist without the password, add the header encryption switch:
    7z a -p -mhe=on secure_backup.7z ~/Documents/TaxRecords/

How to Extract 7z Archives in Ubuntu Terminal

Extracting a 7z archive is straightforward, but it is important to choose the correct extraction command:

  • Extract with Full Directory Structure (Recommended): Use the x command to preserve the original folder hierarchy:
    7z x project_archive.7z
  • Extract into a Specific Destination Folder: Use the -o flag (note: there is no space between -o and the directory path):
    7z x project_archive.7z -o/home/user/RestoredFiles/
  • Extracting a Password-Protected Archive: If an archive is encrypted, 7z will automatically prompt you for the password:
    7z x secure_backup.7z

Listing Archive Contents Without Extracting

If you want to verify what files are stored inside a large 7z archive without decompressing it to your drive, use the l (list) command:

7z l project_archive.7z

The terminal outputs a clean table showing the compressed size, uncompressed size, date modified, attributes, and path of every file stored inside.

Adding the 7-Zip command-line tool to your Ubuntu workflow provides a powerful, versatile utility for creating ultra-compact archives and securing sensitive files effortlessly.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.