How to use the command `7zr` (with examples)
7zr
is a file archiver with a high compression ratio. It is similar to the 7z
command, but it only supports .7z
files.
Use case 1: Archive a file or directory
Code:
7zr a path/to/archive.7z path/to/file_or_directory
Motivation: Archiving files or directories is useful when you want to compress multiple files into a single archive file, reducing their overall size and making them easier to transfer or store.
Explanation:
7zr
is the command itself.a
is the command option used to specify that we want to create an archive.path/to/archive.7z
is the path and filename of the archive file that will be created.path/to/file_or_directory
is the path to the file or directory that you want to include in the archive.
Example output:
path/to/file_or_directory
Use case 2: Encrypt an existing archive (including file names)
Code:
7zr a path/to/encrypted.7z -ppassword -mhe=on path/to/archive.7z
Motivation: Encrypting an archive adds an additional layer of security to prevent unauthorized access to the files within the archive. This can be useful when the files contain sensitive information that needs to be protected.
Explanation:
7zr
is the command itself.a
is the command option used to specify that we want to create an archive.path/to/encrypted.7z
is the path and filename of the encrypted archive that will be created.-ppassword
is the password used for encryption. Replacepassword
with your desired password.-mhe=on
enables the encryption of file names within the archive.path/to/archive.7z
is the path and filename of the archive that will be encrypted.
Example output:
path/to/archive.7z
Use case 3: Extract an archive preserving the original directory structure
Code:
7zr x path/to/archive.7z
Motivation: Extracting an archive while preserving the original directory structure is useful when you want to restore the files to their original locations on your system. This ensures that any file dependencies or relative paths are maintained.
Explanation:
7zr
is the command itself.x
is the command option used to specify that we want to extract an archive.path/to/archive.7z
is the path and filename of the archive that will be extracted.
Example output:
Extracting archive: path/to/archive.7z
Use case 4: Extract an archive to a specific directory
Code:
7zr x path/to/archive.7z -opath/to/output
Motivation: Extracting an archive to a specific directory allows you to choose where the extracted files will be placed. This can be useful when you want to extract the files to a different location than the original archive or when you want to organize the extracted files in a particular way.
Explanation:
7zr
is the command itself.x
is the command option used to specify that we want to extract an archive.path/to/archive.7z
is the path and filename of the archive that will be extracted.-opath/to/output
is the option used to specify the directory where the extracted files should be placed. Replacepath/to/output
with your desired output directory.
Example output:
Extracting archive: path/to/archive.7z
Use case 5: Extract an archive to stdout
Code:
7zr x path/to/archive.7z -so
Motivation: Extracting an archive to stdout
allows you to view the contents of the archive without extracting the files to disk. This can be useful when you only need to peek inside the archive or when you want to pipe the output to another command for further processing.
Explanation:
7zr
is the command itself.x
is the command option used to specify that we want to extract an archive.path/to/archive.7z
is the path and filename of the archive that will be extracted.-so
is the option used to extract the archive tostdout
.
Example output:
Content of the archive: path/to/archive.7z
Use case 6: List the contents of an archive
Code:
7zr l path/to/archive.7z
Motivation: Listing the contents of an archive provides you with information about the files included within it. This can be useful when you want to quickly check what files are contained in an archive or verify the presence of specific files.
Explanation:
7zr
is the command itself.l
is the command option used to specify that we want to list the contents of an archive.path/to/archive.7z
is the path and filename of the archive whose contents we want to list.
Example output:
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2022-01-01 12:00:00 ..... 1234 5678 file.txt
Use case 7: Set the level of compression
Code:
7zr a path/to/archive.7z -mx=0|1|3|5|7|9 path/to/file_or_directory
Motivation: Adjusting the level of compression allows you to balance between the compression ratio and the time it takes to compress the files. Higher compression levels result in smaller archive sizes but require more time to compress.
Explanation:
7zr
is the command itself.a
is the command option used to specify that we want to create an archive.path/to/archive.7z
is the path and filename of the archive that will be created.-mx=0|1|3|5|7|9
specifies the level of compression. Use a number between 0 and 9, where 0 disables compression and 9 provides the highest compression ratio.path/to/file_or_directory
is the path to the file or directory that you want to include in the archive.
Example output:
path/to/file_or_directory
Conclusion:
In this article, we have explored various use cases of the 7zr
command. From archiving and encrypting files to extracting archives and listing their contents, 7zr
provides a range of functionalities for managing .7z
files. By understanding the different options and arguments available, you can effectively use 7zr
to compress and extract files in a way that meets your needs.