Here's how to use the 7zip linux command to create and extract 7z files. Though it's not as widely used as zip, tar, gzip or bzip2 in Linux or other UNIX like systems.
So when most beginners got a 7z archive, they may be thinking about how to extract 7z in Linux properly.
Well, the man page is not exactly very helpful. So I'm writing this tutorial, covering how to crate, extract and edit 7zip archives.
Install Linux 7zip extracting and creating tools
To extract 7z files, you're going to need an proper tool to do that, if not installed already. In Linux, the 7zip extracting or creating utility is provided by p7zip, so you're going to need that.
sudo apt-get install p7zip-full
To install p7zip in Fedora, use DNF command.
sudo dnf install p7zip
Install in Arch Linux, use PacMan command.
sudo pacman -S p7zip
The 7z command can extract almost anything, like a deb or rpm package, ISO files, exe files, cab archives and more.
Extract 7zip Linux
Simply use the
7z command to extract the archives, use the e argument to extract everything inside that archive to a single folder, or use the x argument to extract with full path. Example below,
7z e sample_archive.7z
Using the x argument with 7z command will extract the content with their full path.
7z x sample_archive.7z
Extracting the 7z file to a specific folder
7z x sample_archive.7z -o/path/to/output/folder
Carefully note that there is no space after the -o agrument, an imaginary folder path used above, don't forget to modify it according to yours.
Another useful option is l, list contents of 7z file without exracting it.
7z l sample_archive.7z
Create 7zip linux
A 7zip archive could be created with the a argument with the 7z command. Example below,
7z a output_archive.7z /path/to/folders/ /path/to/files
7z a diagram_backup.7z /home/b00m/circuits/ 555_pwm2.gif 555_pwm.jpg
Note: It's clearly stated that 7zip archives are not fit for backup purpose in Linux if you want to preserve owner/group and permission of the files.
In such cases, using tar command is recommanded. Exmaple - Creating a tar.7z archive.
tar cf - /var/backup_folder/ | 7za a -si ~/my_backup.tar.7z
Restoring the archive,
7za x -so my_backup.tar.7z | tar xf -
Update and edit a 7z archive
Use the l option to get and overview of the 7z archive without extracting it.
You can delete one or many files of the archive using the d option, example :-
7z d archive_name.7z files-to-delete /path/of/files/to/delete
Another real world example
7z d diagram_backup.7z 555_pwm2.gif ups_circuit/5v_UPS.png
Updating an archive: -
You can update an existing archive to add some extra files of folders. A typical example:-
7z u sample_archive.7z /path/to/new/files/to/include
A real world example:-
7z u diagram_backup.7z ../../__b00m/esp_doc/
As always, check the archive with 7z l to make sure the changes are applied properly.
Another thing you might want to do with 7z is integrity checking with the t option, to make sure that the 7z file is OK .
7z t /path/to/sample_archive.7z