Compressing Files With 7zip CLI
7zip is a really useful tool when you want to send .zip
or .7z
archives to other users. If the other side does not know what 7zip is, then you can still use it to create zip files.
I also prefer to use the 7zip files instead of zip files for encrypted archives because it forces the other side to use the 7zip tool, which will automatically recognize if the archive is password protected.
For some reason the default Windows tool will not notice if the archive is password protected, and you have to manually tell it.
Steps
Create Archive (Basic)
7z a \
output-filename.7z \
input-folder
Extract Archive (Basic)
7z x my-file.7z
e
instead of x
when Googling "extraction", but you probably want x
which takes into account full-paths, which is what you are probably used to.
Specify Compression Level
You can specify the compression level with -mx
. It takes a value between 0-9. E.g. to compress the most, but take longer, use 9, and to compress the least, but do the job quickly, use 0.
7z a \
-mx=9 \
output-filename.7z \
input-folder
Fast Encrypt (Including Filenames)
The following command will create an encrypted 7zip file as fast as it can (least compression), encrypting not only the files, but the filenames as well. It will prompt you to enter a password securely, rather than you passing it in the commmand.
7z a \
-t7z \
-mx=0 \
-mhe \
-p \
output-filename.7z \
input-folder
-p
parameter tells 7zip that we are encrypting the archive with a password.-mhe
switch is what tells 7zip to also encrypt the filenames.
References
- Tech Republic - How to use 7zip to encrypt files
- Ask Ubuntu - 7z command line with highest encryption: AES-256 + Encrypting the Filenames
- Tecmint - 10 7zip (File Archive) Command Examples in Linux
First published: 23rd December 2020