10 7zip (File Archive) Command Examples in Linux


If you're a Linux user, you probably deal with file archives frequently. Whether you're sending files to someone, backing up data, or just organizing your files, compressing them into a single file archive can save a lot of space and make things easier to manage. One tool you can use for this purpose is 7zip. In this article, we'll cover 7zip command examples in Linux that you can use to compress and extract files in various ways.

What is 7zip?

7zip is a free and open-source file archiver, similar to WinZip or WinRAR on Windows. It was developed by Igor Pavlov and is available for Windows, Linux, and macOS. One of main advantages of 7zip is its ability to compress files to a high degree, which can save a lot of disk space. It also supports a wide range of file formats, including its own 7z format, as well as ZIP, TAR, and others.

Installing 7zip on Linux

Before we dive into examples, you need to make sure that 7zip is installed on your Linux system. Most Linux distributions include 7zip in their default repositories, so you can use your package manager to install it. For example, on Ubuntu, you can use following command −

sudo apt-get install p7zip-full

On CentOS or Fedora, you can use −

sudo yum install p7zip

If 7zip is not available in your distribution's repositories, you can download it from official website and compile it from source.

7zip Command Examples in Linux

Now that you have 7zip installed on your Linux system, let's look at some command examples that you can use to compress and extract files.

Creating a 7z Archive

To create a 7z archive, you can use "7z" command followed by "a" option, which stands for "add." Here's an example −

7z a archive.7z file1.txt file2.txt file3.txt

This will create a new file called "archive.7z" that contains files "file1.txt", "file2.txt", and "file3.txt". Note that you can add as many files as you want to archive.

Extracting a 7z Archive

To extract a 7z archive, you can use "7z" command followed by "x" option, which stands for "extract." Here's an example −

7z x archive.7z

This will extract contents of "archive.7z" file to current directory. If you want to extract files to a specific directory, you can use "-o" option followed by path to directory. For example −

7z x archive.7z -o /home/user/documents

This will extract files to "/home/user/documents" directory.

Compressing with Different Compression Levels

By default, 7zip compresses files with "normal" compression level, which is a good balance between compression ratio and speed. However, if you want to compress files more aggressively or less aggressively, you can use "-m" option followed by compression level. Here are some examples −

7z a -m0=lzma2 archive.7z file1.txt file2.txt file3.txt

This will use "lzma2" compression method with "fastest" compression level, which will compress files quickly but with a lower compression ratio.

7z a -m9=lzma2 archive.7z file1.txt file2

This will use "lzma2" compression method with "ultra" compression level, which will compress files more aggressively but with a slower speed. You can experiment with different compression levels to find one that suits your needs.

Compressing Directories

In addition to compressing individual files, you can also compress entire directories using 7zip. To do this, simply specify path to directory instead of file names. For example −

7z a -m0=lzma2 archive.7z /home/user/documents/

This will compress entire "documents" directory using "lzma2" compression method with "fastest" compression level.

Using Encryption

If you need to encrypt your archives for security reasons, 7zip provides several encryption options. most secure option is AES-256 encryption, which is considered unbreakable by current standards. To use AES-256 encryption, you can use "-p" option followed by a password, and "-mhe=on" option to enable header encryption. For example −

7z a -pMyPassword -mhe=on archive.7z file1.txt file2.txt file3.txt

This will create a new file called "archive.7z" that is encrypted with AES-256 and protected by password "MyPassword". Note that you should choose a strong password and keep it safe, as it cannot be recovered if you forget it.

Splitting Archives

If you need to split your archives into smaller parts for easier transfer or storage, you can use "-v" option followed by size of each part. For example −

7z a -v1m archive.7z file1.txt file2.txt file3.txt

This will create a new file called "archive.7z" that is split into 1 MB parts. To extract files from a split archive, you only need to extract first part (e.g. "archive.7z.001"), and 7zip will automatically detect other parts and extract files.

Adding Files to an Existing Archive

If you already have an existing archive and want to add more files to it, you can use "7z" command followed by "u" option, which stands for "update." Here's an example −

7z u archive.7z file4.txt file5.txt

This will add files "file4.txt" and "file5.txt" to existing "archive.7z" file.

Creating a Compressed Tar Archive

7zip can also be used to create compressed tar archives, which are commonly used in Linux. To create a compressed tar archive, you can use "7z" command followed by "a" option and "-ttar" option to specify archive format. For example −

7z a -ttar archive.tar.7z file1.txt file2.txt file3.txt

This will create a compressed tar archive called "archive.tar.7z" that contains files "file1.txt", "file2.txt", and "file3.txt".

Extracting a Specific File From an Archive

If you only need to extract a specific file from an archive, you can use "7z" command followed by "-o" option and path to file. For example −

7z x archive.7z -o /home/user/documents file2.txt

This will extract only file "file2.txt" from "archive.7z" file and save it to "/home/user/documents" directory.

Creating a Password-protected Archive

In addition to encryption, 7zip also allows you to create password-protected archives, which can be useful when you want to prevent unauthorized access to your files. To create a password-protected archive, you can use "-p" option followed by password. For example −

7z a -pMyPassword archive.7z file1.txt file2.txt file3.txt

This will create a new file called "archive.7z" that is password-protected with password "MyPassword".

Listing Contents of an Archive

If you want to see contents of an archive without extracting them, you can use "7z" command followed by "l" option, which stands for "list." For example −

7z l archive.7z

This will list contents of "archive.7z" file, including file names, sizes, and compression ratios.

Extracting an Archive with Progress Bar

If you want to see progress of an extraction operation, you can use "-bsp1" option to enable progress bar. For example −

7z x -bsp1 archive.7z

This will extract contents of "archive.7z" file with a progress bar that shows percentage of extraction progress.

Creating a Compressed Archive with Exclusion

If you want to create a compressed archive that excludes certain files or directories, you can use "-x" option followed by file or directory names. For example −

7z a archive.7z * -x!*.log -x!temp/

This will create a compressed archive called "archive.7z" that contains all files in current directory except for those with extension ".log" and directory "temp/".

Extracting an Archive with Exclusion

If you want to extract an archive while excluding certain files or directories, you can use "-x" option followed by file or directory names. For example −

7z x archive.7z -x!*.log -x!temp/

This will extract contents of "archive.7z" file while excluding files with extension ".log" and directory "temp/".

Creating a self-extracting Archive

If you want to create a self-extracting archive that can be run on any system without 7zip installed, you can use "-sfx" option followed by name of executable file to create. For example −

7z a -sfx archive.exe file1.txt file2.txt file3.txt

This will create a self-extracting archive called "archive.exe" that contains files "file1.txt", "file2.txt", and "file3.txt".

Extracting a self-extracting Archive

To extract a self-extracting archive, you simply need to run executable file. For example −

./archive.exe

This will extract contents of self-extracting archive to current directory.

These are just a few examples of many ways you can use 7zip commands in Linux. With its versatile features and powerful compression capabilities, 7zip is an essential tool for managing files and archives on your Linux system.

Conclusion

7zip is a powerful and versatile tool that can help you compress and extract files on your Linux system. With examples in this article, you should be able to use 7zip to manage your archives efficiently and securely. Whether you're sending files to someone or backing up your data, 7zip is a reliable and easy-to-use tool that can save you time and disk space.

Updated on: 02-May-2023

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements