WindowsMacSoftwareSettingsSecurityAndroidProductivityLinuxPerformanceAppleDevice Manageme.. All

How to use 7-Zip from the Command Line in Linux

Edited 3 weeks ago by ExtremeHow Editorial Team

7-ZipLinuxCommand LineSoftwareFile ManagementArchivingTerminalPCUser GuideProductivity

How to use 7-Zip from the Command Line in Linux

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.

1. Understanding 7-Zip

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.

2. Installing 7-Zip on Linux

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:

2.1 For APT-based distributions (Debian, Ubuntu)

sudo apt update
sudo apt install p7zip-full

2.2 For YUM-based distributions (CentOS, RHEL)

sudo yum install p7zip

2.3 For DNF-based distributions (Fedora)

sudo dnf install p7zip

3. Basic 7-Zip commands

3.1 Cataloguing archive material

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.

3.2 Creating archives

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.

3.3 Extracting files

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

4. Advanced 7-Zip usage

4.1 Using password protection

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

4.2 Compression ratio

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.

4.3 Partitioning of archives

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.

4.4 Exclude files

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".

5. Automation and scripting

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.

5.1 Example of a backup script

#!/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.

6. Troubleshooting common problems

If you run into any problems using 7-Zip on the command line, here are a few points you can consider:

6.1 Error: File not found

Make sure the file path is correct. Use the full path when in doubt.

6.2 Error: Permission denied

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.

6.3 Records corruption

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.

7. Conclusions

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


Comments