Ubuntu 7zip: Create and Extract Archives

Ubuntu 7zip: Create and Extract Archives

Ubuntu users can create or extract archives from the command line. The 7-Zip command-line utility creates and opens compressed archives, including .7z files.

In this piece, I’ll show how to check and install the Ubuntu package for your release, then use 7z to create and extract an archive.

TL;DR

Install the Ubuntu package that matches your release, then use 7z to create, inspect, test and extract archives.

  • Ubuntu 24.04 provides the 7zip package in Universe.
  • Use 7z a to create, 7z l to list, 7z t to test and 7z x to extract with paths.
  • Use 7z e only when you want to discard stored folder paths.

What Is 7-Zip on Ubuntu?

The 7-Zip utility creates and opens compressed archives, including files ending in .7z.

Ubuntu 24.04 Noble lists 7zip in Universe and provides /usr/bin/7z. Ubuntu 22.04 Jammy instead lists p7zip-full as the package for the traditional 7z command.

The command letters are compact: a adds files, l lists archive entries, and t checks archive data.

The Ubuntu command-line package supports .7z archives and formats such as ZIP. I use the release and package policy check first because it reveals which command package this system can install.

How to Install and Use 7zip on Ubuntu Step by Step

Use a test archive before applying the commands to important files.

Before starting, identify the files to include and the destination for the archive. A working directory with a small test file helps check command syntax without risking a backup or project copy.

Step 1: Check the Ubuntu release and package

. /etc/os-release
printf '%s\n' "$PRETTY_NAME"
apt-cache policy 7zip p7zip-full

On Ubuntu 24.04.5 LTS, apt-cache reported candidates 23.01+dfsg-11 for 7zip and 16.02+transitional.1 for p7zip-full.

The screenshot shows the full APT policy response.

APT policy output for 7zip and p7zip-full on Ubuntu

Step 2: Install the Ubuntu package

On Ubuntu 24.04, install the 7zip package from Universe with APT. This changes the system and needs administrator authorization, so run it on the Ubuntu computer you administer.

APT uses the software sources enabled for that release. If the package lookup shows no candidate, review the Ubuntu release and repository components before trying a package name from another distribution. The package search page can confirm whether the package belongs to Universe or another component, while the local policy output shows what the configured sources offer.

sudo apt update
sudo apt install 7zip

Run these documented commands on the Ubuntu machine you administer.

Step 3: Confirm the command is available

Check the executable path and version before processing an archive.

command -v 7z
7z

The version banner confirms that the executable starts.

Step 4: Create a 7z archive

Use the a subcommand followed by the archive name and the file or directory to add. Run the command from a directory where the named input exists, or provide its full path.

7z a project-backup.7z project/

The .7z extension selects the 7z format. Quote paths that contain spaces, such as “Project Files/”, so the shell passes the complete name as one argument.

Step 5: List and test the archive

List archive entries before extraction, then run the integrity test to check whether the stored data can be read.

7z l project-backup.7z
7z t project-backup.7z

The test I ran printed “Everything is Ok” and exited successfully.

Captured output from that test:

Testing archive: sample.7z
Everything is Ok
Size: 22
Compressed: 156
7z test reports Everything is Ok for sample.7z

Step 6: Extract while preserving folders

Use x to unpack an archive while keeping the folder structure. Set a destination with the -o switch, with no space between the switch and path.

mkdir -p unpacked
7z x project-backup.7z -ounpacked/

The output directory keeps extracted files separate from the archive and the current directory.

Step 7: Extract without stored paths

Use e when the extracted files should go into one directory.

mkdir -p flat
7z e project-backup.7z -oflat/

Use the e command when the archive layout is understood and file names will not conflict.

Before you start: choose a safe working directory

Keep the archive and the files you plan to compress in a directory you can identify.

Use pwd to identify your current location before running a relative-path command.

pwd
ls -l sample.7z

The first command identifies the shell location. The second confirms the named archive is present before 7z reads it.

Keep paths predictable when creating archives

Run the add command from the parent directory when the archive should contain a clean project folder.

Pass a directory as one input argument when you want to add its tree.

Inspect archive paths before extraction

Check for unexpected top-level names, duplicate filenames and an unusually broad collection of paths.

Review extracted files before moving them into a project.

Use the right archive extension

Match the filename extension to the archive format you create. This makes the file type clear when you or another tool opens it.

Understand archive size and updates

Archive size depends on the input and compression method, so the output will not always be smaller than the original files. Already-compressed images, video and downloaded archives often have little room to shrink.

Adding a file to an existing archive changes it. Verify the added entry before relying on that updated copy.

For a repeatable backup, use a clear archive name that includes the project or purpose. Avoid overwriting the prior known-good archive until the replacement has passed its integrity check.

Prefer the Ubuntu package repository for APT-managed updates. Avoid mixing installation sources for the same command.

When the executable starts but a particular archive fails, check whether it can list and test the archive before changing extraction options.

Choose a format for sharing

The 7z format suits a recipient with compatible 7-Zip software. ZIP is a common alternative when built-in tools on another operating system matter more. Choose the format before compressing the files.

If an archive contains private documents, share its password through a separate channel. Password protection depends on the recipient receiving the secret safely and on the format supporting the chosen encryption option.

Check source directories for hidden files before compressing configuration folders where hidden settings matter.

Large archives can take time to test because the program reads the stored data. Keep the terminal open until the command exits, and use the final status rather than assuming that a quiet interval means the operation finished. A nonzero status or an error line deserves investigation before you delete the source files.

Keep archive paths clear when sharing

Keep the archive name and extraction destination explicit in scripts or notes. Use a full path for automation and a quoted relative path for a quick interactive task.

Store a backup copy where intended before removing the source.

Fixing 7-Zip Edge Cases

Command failures can come from a package mismatch, a path typo, split filenames or damaged archive data. Compare the error with the stage that failed before retrying.

SymptomLikely causeFix
Command not foundPackage absent or wrong release-specific packageCheck release and APT candidate, then install its matching package
No more files or cannot open archiveWrong working directory or misspelled pathUse the archive’s full path or change to its directory
Filename with spaces failsShell split the unquoted nameQuote the complete path
CRC or data errorDamaged or incomplete archiveRe-copy or download it, then test again

A missing-file error such as “No such file or directory” means the command did not find the named archive. I ran 7z x nonexistent.7z -o/tmp/nope and received errno=2. Check spelling and the current directory, or pass an absolute archive path.

CaseSymptomFix
Path typoNo such file or directoryCheck the name and location, then use an absolute path
Spaces in a nameCommand treats words as separate argumentsQuote the full path, such as “Project Files/data.7z”
Incomplete or damaged archive7z t reports CRC or data errorObtain a complete copy and test it again
Package unavailableAPT shows no candidateCheck release and enabled repository components

For an encrypted archive, 7z can prompt for the password when the archive is opened. Avoid adding a secret directly to a command that may be stored in shell history. If a password prompt appears, make sure the keyboard layout and the exact password are correct before treating the archive as damaged.

RAR support is separate from the core package. Ubuntu lists a 7zip-rar plugin in Multiverse for Noble. Check the repository and licence terms for the Ubuntu release before depending on that plugin.

Conclusion

Choose the package based on the release and select the extraction mode that suits the archive. Check the Ubuntu package search and 7-Zip FAQ when package or format details need confirmation. For related Linux file operations, see the ls command guide and man command guide.

FAQ

These short answers cover common Ubuntu archive questions.

How do I extract a 7z file on Ubuntu?

Install the release-appropriate 7-Zip package, then run 7z x archive.7z to preserve its folder paths.

What is the difference between 7z x and 7z e?

The x command preserves archived directory paths. The e command extracts files without those paths.

Can Ubuntu open ZIP files with 7-Zip?

7-Zip supports ZIP archives. Use 7z l file.zip to inspect and 7z x file.zip to extract while retaining paths.