7zip is a powerful open-source archiver – software package for packing and unpacking files and directories into archives. Unlike software packages, archives are containers aimed at compressing data, not storing files with additional metadata. This means that for installing software, you're better off using official software repositories and packages. But for quickly compressing a bunch of files or directories for transferring to another system, archivers are the best.
7zip is a universal archiver, meaning it supports a number of common archive packages like ZIP, ARJ, BZIP, TAR and even RAR. Naturally, 7zip offers and supports its own archival format called 7zip (it uses .7z extension for archive files).
p7zip software packages
7zip was created for Windows platform, but after some time a command line version for POSIX (and Linux) Unix systems was developed – it is now called p7zip (P is probably for POSIX) and distributed for most of common Linux operating systems.
Looking at Ubuntu for example, there are 2 packages available: p7zip and p7zip-full. They install different binaries and offer slightly different functionality.
p7zip package offers lighter versions of 7zip archiver:
- 7zr command only supports 7zip archives and doesn't support encrypted archives
- 7za command supports a limited number of additional archive types
p7zip-full package offers a modular 7zip archiver:
- 7z command that uses plugins for additional arhive types support
WARNING: 7zip does not handle Unix file ownership
Probably because this isn't a Unix native software, 7zip archiver doesn't support the file ownership correctly. This means if you archive files that belong to multiple different users, this ownership information will be lost – so when you unpack the archive you will not know which files belong to what user.
That's fine for quick and simple backup tasks, but is a major issue when you want to create complex backups of directories shared by multiple users. It's best that you use native Unix/Linux tools like tar then.
Create archive with 7zip
Go to the directory that your files are in. In this example I'm saving OS configuration files form /etc directory:
# cd /etc
Now archive the files like this:
# 7z a /tmp/etc.7z * 7-Zip  9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,1 CPU) Scanning Creating archive /tmp/etc.7z Compressing update-motd.d/00-header Compressing ppp/ip-down.d/000resolvconf Compressing ppp/ip-up.d/000resolvconf Compressing network/if-up.d/000resolvconf Compressing grub.d/00_header Compressing apt/apt.conf.d/01-vendor-ubuntu ...
Adding files to an existing 7zip archive
If you forgot or recently updated files in the original location, you can use the same a option to update files in the existing archive:
# cd /etc # 7z a /tmp/etc.7z network* 7-Zip  9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,1 CPU) Scanning Updating archive /tmp/etc.7z Compressing network/if-up.d/000resolvconf Compressing network/if-pre-up.d/ethtool Compressing network/if-up.d/ethtool Compressing network/if-up.d/ifenslave Compressing network/if-post-down.d/ifenslave Compressing network/if-pre-up.d/ifenslave Compressing network/interfaces Compressing network/if-up.d/ip Compressing networks Compressing network/if-up.d/openssh-server Compressing network/if-down.d/resolvconf Compressing network/if-down.d/upstart Compressing network/if-up.d/upstart Compressing network/if-post-down.d/vlan Compressing network/if-pre-up.d/vlan Compressing network/interfaces.d/50-cloud-init.cfg Everything is Ok
Listing files in a 7zip archive
Say you come across a bunch of old archives that you can't really remember. It's probably best to first list files in an archive than to unpack them. l option for 7zip helps you do that (I'm abridging the output for the purpose of this post):
# 7z l /tmp/etc.7z 7-Zip  9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,1 CPU) Listing archive: /tmp/etc.7z -- Path = /tmp/etc.7z Type = 7z Method = LZMA Solid = + Blocks = 2 Physical Size = 512438 Headers Size = 21038 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2015-10-22 19:15:21 ....A 1220 484437 update-motd.d/00-header 2015-06-03 22:58:21 ....A 413 ppp/ip-down.d/000resolvconf 2015-06-03 22:58:21 ....A 553 ppp/ip-up.d/000resolvconf 2017-12-12 23:48:41 ....A 9791 grub.d/00_header 2016-04-14 09:45:21 ....A 42 apt/apt.conf.d/01-vendor-ubuntu 2016-04-14 09:45:21 ....A 769 apt/apt.conf.d/01autoremove ... 2018-01-09 23:22:44 D.... 0 0 apm/event.d 2018-01-09 23:22:06 D.... 0 0 apm 2018-03-10 16:42:06 D.... 0 0 alternatives 2018-01-09 23:22:49 D.... 0 0 acpi/events 2018-01-09 23:22:49 D.... 0 0 acpi ------------------- ----- ------------ ------------ ------------------------ 2088855 491400 1565 files, 201 folders
Extracting files from 7zip archive
In this simple enough scenario, the files and directories you extract will be unpacked into the current directory you are in.
First, you create the destination directory or just change location to it:
# mkdir /home/greys/etc-try # cd /home/greys/etc/try
Now run the 7z command to extract files:
# 7z -e /tmp/etc.7z 7-Zip  9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,1 CPU) Processing archive: /tmp/etc.7z Extracting update-motd.d/00-header Extracting ppp/ip-up.d/000resolvconf Extracting grub.d/00_header Extracting apt/apt.conf.d/01-vendor-ubuntu Extracting apt/apt.conf.d/01autoremove Extracting apt/apt.conf.d/01autoremove-kernels Extracting grub.d/05_debian_theme Extracting update-motd.d/10-help-text ...
Extracting 7zip archive into a different directory
If you want the files to be extracted somewhere else, not in the current directory, then you should use the -o parameter. In this example below, I'm instructing 7zip to extrace /tmp/etc.7z archive into the /root/etc-new directory. If it doesn't exist, 7zip will create it:
# 7z -o/root/etc-new e /tmp/etc.7z
That's it for now! Hope you liked this brief introduction, let me know if you are using 7zip and want me to expand on any aspects.
Leave a Reply