Edited 3 weeks ago by ExtremeHow Editorial Team
7-ZipLinuxCommand LineSoftwareFile ManagementArchivingTerminalPCUser GuideProductivity
This content is available in 7 different language
Compression tools are useful for reducing the size of files and folders, which can be beneficial for storage and transfer purposes. One of the most popular tools for this is 7-Zip, a free and open-source software for file compression and decompression. Although 7-Zip is commonly associated with Windows, it is also available for Linux, where it is called `p7zip`. In this guide, we will explore the use of 7-Zip from the command line in Linux, covering everything from installation to advanced usage. We will also dive deep into the essential commands, options, and parameters that can be applied to effectively use this powerful tool.
7-Zip is a file archiver with a high compression ratio. It supports many file formats, including its own 7z format, which is known for offering high compression capabilities. The command-line version of 7-Zip is often used due to its ability to automate tasks and integrate into scripts.
Before you can use 7-Zip on Linux, you need to install it. Most Linux distributions have p7zip available in their default repositories. The package name may vary slightly depending on the distribution, but generally, you can install it using the following command:
sudo apt update
sudo apt install p7zip-full
sudo yum install p7zip
sudo dnf install p7zip
To list the contents of an archive, use the `7z l` command followed by the name of the archive file:
7z l example.7z
This command lists all the files contained in `example.7z`, and provides basic information such as file size and timestamp.
Creating an archive using 7-Zip is simple. The `7z a` command is used to add files to the archive:
7z a archive.7z file1.txt file2.txt
This will create an archive named `archive.7z` containing `file1.txt` and `file2.txt`. You can use wildcard characters like `*` to add multiple files with the same pattern.
To extract files from an archive, the `7z x` command is used. If you want to extract all files in an archive, just specify the archive file:
7z x archive.7z
This will extract all files in the archive to the current directory. You can specify a different directory with the `-o` option:
7z x archive.7z -o/home/user/extracted
You can password protect your archives using the `-p` option. This is important to maintain the confidentiality of archived content:
7z a -pYourPassword archive.7z file1.txt
7-Zip allows you to choose the compression ratio. Higher ratios mean smaller archives but more processing time. You can set the compression level using the `-mx` switch:
7z a -mx=9 archive.7z file1.txt
Compression levels range from 1 to 9, with 9 being the highest.
You may want to split large archives into smaller, more manageable parts. You can specify the size of each part using the `-v` option:
7z a -v500m archive.7z file1.txt
This will split the archive into 500 MB chunks.
Sometimes, you might not want to include certain files in your archive. The `-x` option allows you to exclude files or patterns:
7z a archive.7z * -x!temp*
This command archives all files except those starting with "temp".
The biggest advantage of using 7-Zip from the command line is the ability to automate processes using scripts. A script can regularly back up directories with the desired compression settings.
#!/bin/bash
# Backup /home/user directory
backup_date=$(date +%Y-%m-%d)
backup_dir="/backup/$backup_date"
mkdir -p "$backup_dir"
7z a "$backup_dir/home_backup.7z" /home/user -mx=9
This script creates a date-stamped directory and backs up the user's home directory using 7-Zip with maximum compression.
If you run into any problems using 7-Zip on the command line, here are a few points you can consider:
Make sure the file path is correct. Use the full path when in doubt.
This error usually indicates a lack of necessary permissions to read or write files. Use `sudo` if necessary, but be mindful of the security implications.
If you suspect corruption, use the `-t` switch to test the archive:
7z t archive.7z
This command will check the integrity and notify you of any problems.
Using 7-Zip from the command line in Linux provides extensive flexibility and control over compression tasks. With its robust set of commands and options, it serves as a powerful utility for both simple and complex archiving needs. Whether you are creating simple archives for personal use or automating backup processes, understanding the command line syntax and options for 7-Zip can significantly optimize your workflow in a Linux environment.
Mastering the command line tools provided by 7-Zip can save time and improve efficiency, providing nearly endless possibilities for handling files in the Linux operating system. Remember, practice and exploring its many features will deepen your understanding of this versatile compression tool.
Now, armed with the knowledge of how to install, create, extract, and manage archives using 7-Zip on your Linux system, you can handle various compression tasks with ease and confidence.
If you find anything wrong with the article content, you can