How to use the command 7z (with examples)
7z is a file archiver with a high compression ratio. It allows users to create and manipulate compressed archive files. This article will illustrate several use cases of the 7z command.
Use case 1: Add a file or directory to a new or existing archive
Code:
7z a path/to/archive.7z path/to/file_or_directory
Motivation: This use case is helpful when you want to compress a file or directory into an archive for storage or sharing purposes.
Explanation:
7z
is the command itself.a
specifies the operation to add files or directories to an archive.path/to/archive.7z
is the path where the newly created or existing archive will be stored.path/to/file_or_directory
is the path of the file or directory that you want to add to the archive.
Example output:
Compressing path/to/file_or_directory
Everything is Ok
Use case 2: Encrypt an existing archive (including filenames)
Code:
7z a path/to/encrypted.7z -ppassword -mhe=on path/to/archive.7z
Motivation: Encrypting an archive can provide an extra layer of security, ensuring that only authorized users can access the contents.
Explanation:
-ppassword
specifies the password for encryption. Replacepassword
with the desired password.-mhe=on
enables the encrypt headers mode, which encrypts not only the files but also the archive metadata.
Example output:
Creating archive path/to/encrypted.7z
Compressing path/to/archive.7z
Everything is Ok
Use case 3: Extract an archive preserving the original directory structure
Code:
7z x path/to/archive.7z
Motivation: Extracting an archive while preserving the original directory structure is helpful when you want to restore the files to their original locations.
Explanation:
x
specifies the operation to extract files from an archive.path/to/archive.7z
is the path to the archive file that you want to extract.
Example output:
Extracting archive: path/to/archive.7z
Everything is Ok
Folders: 1
Files: 3
Size: 25189
Compressed: 4503
Use case 4: Extract an archive to a specific directory
Code:
7z x path/to/archive.7z -opath/to/output
Motivation: Extracting an archive to a specific directory allows you to control where the extracted files will be placed.
Explanation:
-o
specifies the output directory where the extracted files should be placed.path/to/output
is the path to the directory where you want to extract the files.
Example output:
Extracting archive: path/to/archive.7z
Everything is Ok
Folders: 1
Files: 3
Size: 25189
Compressed: 4503
Use case 5: Extract an archive to stdout
Code:
7z x path/to/archive.7z -so
Motivation: Extracting an archive to stdout
allows you to view the contents of the archive without extracting them to the filesystem.
Explanation:
-so
redirects the output tostdout
.
Example output:
Contents of path/to/archive.7z:
folder/
folder/file1.txt
folder/file2.txt
folder/subfolder/
folder/subfolder/file3.txt
Use case 6: Archive using a specific archive type
Code:
7z a -t7z|bzip2|gzip|lzip|tar|zip path/to/archive path/to/file_or_directory
Motivation: Archiving files in different formats can provide compatibility with different tools or platforms.
Explanation:
-t
specifies the archive type to use. Replace7z|bzip2|gzip|lzip|tar|zip
with the desired archive format.path/to/archive
is the path where the newly created archive will be stored.
Example output:
Compressing path/to/file_or_directory
Everything is Ok
Use case 7: List the contents of an archive
Code:
7z l path/to/archive.7z
Motivation: Listing the contents of an archive can help you quickly check what files are included before or after extraction.
Explanation:
l
specifies the operation to list the contents of an archive.path/to/archive.7z
is the path to the archive file that you want to list.
Example output:
Date Time Attr Size Size. Packing Name
------------------- ----- ------------ ------------ ------------------------
YYYY-MM-DD HH:MM:SS ....A 0 0 folder/
YYYY-MM-DD HH:MM:SS ....A 1000 1000 folder/file1.txt
YYYY-MM-DD HH:MM:SS ....A 2000 2000 folder/file2.txt
YYYY-MM-DD HH:MM:SS ....A 0 0 folder/subfolder/
YYYY-MM-DD HH:MM:SS ....A 3000 3000 folder/subfolder/file3.txt
------------------- ----- ------------ ------------ ------------------------
6000 6000 5 files, 1 folders
Use case 8: Set the level of compression
Code:
7z a path/to/archive.7z -mx=0|1|3|5|7|9 path/to/file_or_directory
Motivation: Being able to adjust the level of compression allows you to find a balance between file size and compression time.
Explanation:
-mx
specifies the level of compression. Replace0|1|3|5|7|9
with the desired compression level.0
is no compression.1
is fastest compression.9
is best compression.
path/to/archive.7z
is the path where the newly created archive will be stored.
Example output:
Compressing path/to/file_or_directory
Everything is Ok
Conclusion:
The 7z command provides a versatile and powerful tool for file compression and archiving. With its various options and arguments, you can effectively manage your files and save storage space. Whether you need to create an archive, extract its contents, or manipulate the compression settings, the 7z command has you covered.