Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 the 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 the 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 the official website and compile it from source.
7zip Command Examples in Linux
1. Creating a 7z Archive
To create a 7z archive, you can use the 7z command followed by the 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 the files "file1.txt", "file2.txt", and "file3.txt". Note that you can add as many files as you want to the archive.
2. Extracting a 7z Archive
To extract a 7z archive, you can use the 7z command followed by the x option, which stands for "extract." Here's an example
7z x archive.7z
This will extract the contents of the "archive.7z" file to the current directory. If you want to extract files to a specific directory, you can use the -o option followed by the path to the directory. For example
7z x archive.7z -o/home/user/documents
This will extract files to the "/home/user/documents" directory.
3. 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 or less aggressively, you can use the -mx option followed by the compression level (0-9). Here are some examples
7z a -mx0 archive.7z file1.txt file2.txt file3.txt
This will use the fastest compression level (lowest compression ratio).
7z a -mx9 archive.7z file1.txt file2.txt file3.txt
This will use the "ultra" compression level, which will compress files more aggressively but with slower speed.
4. Compressing Directories
In addition to compressing individual files, you can also compress entire directories using 7zip. To do this, simply specify the path to the directory instead of file names. For example
7z a archive.7z /home/user/documents/
This will compress the entire "documents" directory and all its contents.
5. Using Encryption
If you need to encrypt your archives for security reasons, 7zip provides several encryption options. The most secure option is AES-256 encryption, which is considered unbreakable by current standards. To use AES-256 encryption, you can use the -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 the password "MyPassword". Note that you should choose a strong password and keep it safe, as it cannot be recovered if you forget it.
6. Splitting Archives
If you need to split your archives into smaller parts for easier transfer or storage, you can use the -v option followed by the 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 the first part (e.g. "archive.7z.001"), and 7zip will automatically detect other parts and extract the files.
7. Adding Files to an Existing Archive
If you already have an existing archive and want to add more files to it, you can use the 7z command followed by the u option, which stands for "update." Here's an example
7z u archive.7z file4.txt file5.txt
This will add the files "file4.txt" and "file5.txt" to the existing "archive.7z" file.
8. Listing Contents of an Archive
If you want to see the contents of an archive without extracting them, you can use the 7z command followed by the l option, which stands for "list." For example
7z l archive.7z
This will list the contents of the "archive.7z" file, including file names, sizes, and compression ratios.
9. Creating Archives with Exclusion
If you want to create a compressed archive that excludes certain files or directories, you can use the -x option followed by file or directory patterns. For example
7z a archive.7z * -x!*.log -x!temp/
This will create a compressed archive called "archive.7z" that contains all files in the current directory except for those with the ".log" extension and the "temp/" directory.
10. Extracting with Progress Display
If you want to see the progress of an extraction operation, you can use the -bsp1 option to enable a progress bar. For example
7z x -bsp1 archive.7z
This will extract the contents of the "archive.7z" file with a progress bar that shows the percentage of extraction progress.
Common 7zip Options
| Option | Description | Example |
|---|---|---|
a |
Add files to archive | 7z a archive.7z file.txt |
x |
Extract with full paths | 7z x archive.7z |
l |
List contents | 7z l archive.7z |
-mx[0-9] |
Compression level | 7z a -mx9 archive.7z file.txt |
-p[password] |
Password protection | 7z a -pSecret archive.7z file.txt |
-v[size] |
Split archive into volumes | 7z a -v10m archive.7z file.txt |
Conclusion
7zip is a powerful and versatile tool that can help you compress and extract files on your Linux system. With the 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 offers excellent compression ratios and comprehensive features for all your archiving needs.
