Tutoriel Linux

Create a 7z archive on Linux when a ZIP becomes too small

Débutant3 min de lecture

A ZIP is enough to exchange a few documents. As soon as a folder becomes large, when it’s necessary to limit the file size or hide the names it contains, the 7z format becomes more interesting. I would use it to prepare a batch for transfer or to archive a working copy, not as the sole backup.

The procedure consists of four checks: identify the installed command, create the archive without deleting the sources, read its content, and then test it before deleting or sending anything.

Tux organizes folders into a secure archive to illustrate the creation of a 7z archive under Linux
A 7z archive should be verified before transferring or deleting the source files.

Check the command and prepare the directory

On recent Debian or Ubuntu systems, the 7zip package typically provides 7zz. Older installations sometimes use p7zip-full and the 7z command. Start by checking what’s actually available:

command -v 7zz || command -v 7z
7zz i 2>/dev/null || 7z i

If no command responds, install the package suitable for your distribution. On Debian or Ubuntu, start by updating the index, then install 7zip:

sudo apt update
sudo apt install 7zip

Work from the parent directory of the folder to archive. This will prevent an archive filled with absolute paths and you will immediately see the concerned volume. Also check the available space on the destination:

du -sh project
 df -h .

Create a 7z archive while keeping the source files

Navigate to the folder that contains project, then create the archive. The a command adds the files. It does not delete the source folder unless you explicitly request another behavior.

7zz a backup-project.7z project/

With the older command, simply replace 7zz with 7z. The default level works in most cases. To prioritize speed on a server already under load, add -mx=3. The level -mx=9 takes more CPU and does not always reduce the size enough to justify the wait.

To split the archive into volumes of 2 GiB intended for a limited medium or shipment, use -v2g:

7zz a -v2g backup-project.7z project/

Keep all .001, .002 files and so on in the same folder. The first volume is used for testing and extraction.

Encrypt the archive without exposing the password

Avoid -psecret directly in the command line: Bash often saves it in the history and it may appear in the process list. Pass -p without a value so that 7-Zip asks for the secret interactively. Add -mhe=on to also encrypt the file names.

7zz a -p -mhe=on backup-project-private.7z project/

An encrypted archive does not replace a backup. Lose the password or the storage key, and the content becomes unusable. For company secrets, maintain a separate encryption and recovery policy from this one-time command.

List, test then extract into a copy

Before a transfer or a cleanup, first check the content. l lists the files, t checks the integrity of the archive. These two commands prevent discovering an incomplete archive after deleting the original folder.

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

Then test an extraction into an empty directory. Keep the source folder intact until this step:

mkdir -p /tmp/test-7z
7zz x backup-project.7z -o/tmp/test-7z
find /tmp/test-7z -maxdepth 2 -type f | head

For a significant transfer, also compute a checksum of the archive and compare it after the copy:

sha256sum backup-project.7z > backup-project.7z.sha256
sha256sum -c backup-project.7z.sha256

The 7z format is used to package and compress. For regular backups with tested restoration, you also need an independent destination and a rotation. If you simply need to create or extract a standard Unix archive, the guide tar on Linux is still more suitable for common workflows.

sudo apt update && sudo apt upgrade