All forms of compression in this format turn out more complicated than the approach we will see here. At least for most users who use compression to pack everything in a folder and its sub-folders into compressed “bundles.” So let’s see how we can combine Bash’s support for aliases with the command-line version of 7z to create packages of our files with a simple command.
Install p7zip
As the most popular compression tool, 7-Zip is most likely already installed in your Linux distribution. To test it, try the “7z” command in a terminal. If not installed, you can install it in Ubuntu/Debian-based distributions with the command:
Ceate Bash aliases file
Bash’s aliases allow mapping of commands or even multi-command sequences into single words. The easy way to add aliases to Bash is by inserting them at the end of the “~/.bashrc” file. For the sake of convenience and organization, it’s considered best to place them in a separate file. It is likely that this file already exists, and Bash’s configuration includes a reference to it. Check if it is by opening “~/.bashrc” in your favorite word processor, and search for: If not, add it at the end of the file after everything that is already there.
Have 7-Zip’s info handy
You can run 7-Zip in a terminal to view a rundown of its command-line options. For more comprehensive information, visit this user guide or the program’s man page with: These will aid you in setting up your own compression commands. Most likely, the ones we’ll see here will cover your needs, too. So you can copy-paste them into your own .bash_aliases file.
Create the ultra-compression alias
Open the file “~/.bash_aliases” in your favorite word processor – we use nano. Enter:
alias 75 says we want to create the command “75” that we will use from now on to compress all files and folders in a directory into a packaged 7-Zip bundle. The reason we adopted this peculiar name for our command is because it’s easy to recall as a short version of “7zip compression level 5.” 7z is the compression command itself. The a following it means “we want to add files to a new compressed package.” -r indicates that p7zip should not be “constrained to the folder where it ran” but should include the final compressed package and all sub-folders in it, with all its contents. -t7z -m0=lzma2 indicates that we want to create 7zip packages that primarily use the LZMA2 algorithm for file compression. This usually yields the best compression for most file types. -mx=9 -myx=9 correspond to the level of compression and the “amount of effort” 7-Zip will put into analyzing the contents of the files to be compressed to find the best compression strategy. The higher their values, the greater the compression and the smaller the produced bundle. Finally, -mqs=on -ms=on defines that we want “solid” compression. This means that p7zip will compress similar files as single chunks of data, achieving even better results. The reason they exist as an option you can turn off is that they are useful but also come with two negatives. They prolong the compression time and make it impossible to decompress independent files from the final bundle the compressor produces. With solid compression, you can’t do the equivalent of “extracting a single file from a zip,” and you have to decompress the whole bundle to access its contents.
Replicate and “cut-down” the alias
We started with the command that achieves the maximum compression possible so that instead of having to extend it with more elements, we can move backward, removing parameters and reducing the set values. Begin by copying the existing command five more times, placing each command in its own line. Leave the first line as it is, as it already achieves the highest compression level possible. Modify the rest, in order, by removing the extra options and reducing the compression levels as you see below. Remember to change their aliases to match the lower compression levels.
You can always copy of the following and paste it into your ~/.bash_aliases instead.
Activate your aliases
Save the changes to the file and return to the terminal. To load and activate your new aliases, use the command: Your new compression commands are active. P7zip operates by default on all files in the directory where it runs if you don’t define “what you want to compress” as a parameter. So all you need to do from now on when you want to compress the contents of a folder into a 7z package is enter the command: Here “75” is the alias for the highest compression level from earlier, and “archive_name” is the name of the compressed file package. You can replace “75” with one of the other aliases (from 75 to 70) for gradually smaller but faster compression, and use whatever name you want for the final file.
If you have any questions, let us know in the comments below.