Home Compression Working With 7z Files. How to grep 7z Files and More

Working With 7z Files. How to grep 7z Files and More

by The Linux Digest Guy
Robot trying to compress files into a box.

In a recent project, I faced a situation that might be common for many: I had a directory full of 7z files that I needed to search through. Extracting each file to search them seemed time-consuming and inefficient in terms of space. This challenge led me to look for a more direct solution, similar to tools available for other archive formats.

After some research, I have found practical methods to handle these files.

What Are 7z Files?

Before we start working with our 7z files, it might be good to understand them. 7z files are compressed archives created using the 7-Zip format, renowned for its high compression ratio. For Linux users, interacting with these files is typically done through p7zip, the port of 7-Zip for POSIX systems like Linux.

Key Characteristics of 7z Files for Linux Users:

  1. High Compression Ratio: The 7z format is known for its ability to compress files significantly, often more effectively than traditional formats like ZIP or RAR. This is particularly useful for Linux users dealing with large volumes of data.
  2. Support for Multiple Formats: 7z archives can contain a variety of file types, from text and images to executables, making them versatile for different use cases.
  3. Encryption Options: With p7zip, Linux users can utilize AES-256 encryption to secure their compressed files, adding a layer of security with password protection.
  4. Open-source and Free: p7zip, like its parent 7-Zip, is open-source. This aligns well with the Linux community’s preference for open-source solutions.
  5. Cross-Platform Compatibility: Although p7zip is a Linux-focused tool, the .7z format itself is cross-platform, meaning files compressed on Linux can be easily shared and opened on other operating systems with compatible software.

For Linux users, understanding these aspects of 7z can be useful. They provide a powerful tool for data compression, archiving, and secure file transfer. In the following sections, I will go into the specifics of how Linux users can effectively search within, compress, and extract 7z files using p7zip.

P7zip Is Not Like the Other Compression Tools

P7zip does not use the same parameters as most other CLI compression tools in Linux.

First of all the first argument after the name of the executable is always a single-character command. Like a to add a file to an archive or x to extract an archive. Optionally you can add some parameters after the command, preceded with a dash like we are used to from commands like tar.

One example of why this may be confusing is that the -x parameter is often used to extract an archive in other tools. In p7zip this parameter is used to exclude files. So it might be advisable to check the documentation before you assume what a parameter does.

How to Grep 7z Files

Unlike some other formats, there isn’t a direct command like zgrep or bzgrep for 7z files. At least no one I have found.. However, with a combination of tools available in Linux, you can effectively perform a grep search inside 7z files.

The easiest way to do this is to extract the file to standard output and pipe the output to grep. Here’s the basic command structure:

7z x -so my_archive.7z | grep 'my_search_pattern'
  • 7z is the p7zip program.
  • The x switch tells 7z to extract the file.
  • The -so parameter tells 7z to output the contents to StdOut
  • The | character redirects the StdOut output to grep

Extract 7z Files

Firstly, ensure that p7zip is installed on your Linux system. It’s a standard tool available in most Linux distributions, but if you don’t have it, you can easily install it. On Debian-based systems like Ubuntu, you would use the following command in the terminal:

sudo apt-get install p7zip-full

Once p7zip is installed, navigate in the terminal to the directory containing the 7z file you wish to extract. The basic command for extraction is quite simple. Just type 7z x followed by the name of your file. For example, to decompress a 7z file named ‘data.7z’, you would enter:

7z x data.7z

This command decompresses all the contents of ‘data.7z’ into the current directory. If you prefer to extract the files into a specific folder, p7zip allows you to specify an output directory. This is done by adding -o followed by the desired directory path. For example, to extract to a folder named ‘extracted_files’, you would use:

7z x data.7z -o/extracted_files/

List files in an archive

Before extracting, it’s often useful to view the contents of the archive without decompressing it. P7zip provides a handy way to do this with the l (list) command. To list all the files in the my_archive.7z archive you would enter:

7z l my_archive.7z

Create a 7zip archive

Creating a 7z file is straightforward with p7zip. You only need to use a for the add command, followed by the archive name and the files you want to compress. The basic syntax for the command is:

7z a archive_name.7z files_or_directories

Add files to an archive

If you want to add more files to the same archive, you can repeat the exact same process. 7z will detect that an archive by that name already exists and add the files to the existing archive. Let’s say you want to add new_file.txt to the archive above:

7z a archive_name.7z new_file.txt

Define Compression Levels

When creating large archives using p7zip you may want to define the compression level of your archive. Compression levels in p7zip determine the balance between file size reduction and the resources needed for compression, ranging from 0 to 9:

  1. Level 0 (-mx0): No compression. Files are just stored, offering quick bundling without size reduction. Ideal for when only file aggregation is needed.
  2. Level 1 to 3 (-mx1 to -mx3): Low compression levels. They provide moderate compression quickly, suitable when minimal size reduction is needed with limited time or processing power.
  3. Level 4 to 6 (-mx4 to -mx6): Default or intermediate levels. They offer a good balance, yielding reasonable compression without being too slow. Optimal for regular use where both time and file size are considerations.
  4. Level 7 to 9 (-mx7 to -mx9): High compression levels. Level 9 achieves the maximum compression but is the slowest. Best used when minimizing file size is paramount, and time or resource constraints are less of a concern.

The effectiveness of each level varies based on the data type. Lower levels are quick and sufficient for simple tasks, intermediate levels provide a good balance for everyday use, and higher levels are ideal for achieving the smallest file size where compression time is not an issue. The choice of level should align with your specific needs regarding compression speed and file size reduction.

Password Protect Your 7z file

To password-protect a 7z file during the compression process, you simply need to include the -p option in your command. Here’s how you can do it:

7z a -p my_archive.7z my_file

After you hit enter, p7zip will prompt you for a password and then again to verify it. The contents of your archive should be pretty well protected now. Since the file contents are now encrypted with AES-256 encryption using a secret derived from the password you chose.

Encrypt Your 7z File Names

That’s not all. In addition to password-protecting your 7z file, you can also encrypt the filenames within the file. This way a password is also required to list the files within the archive. To do this you use the -mhe parameter:

7z a -p -mhe=on my_archive.7z my_file

Wrapping up

I think this just about covers the basics of working with 7z files. Hopefully, you have found what you were looking for.


Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy