How to Simplify 7z Compression with Bash Aliases

7zip Bash Aliases Featured

There are many ways and many tools you can use to shrink your files, either to reduce the space they take up or send them as a package to a contact. Of these, 7-Zip has ascended to the top spot for most users, thanks to its unique combination of great performance and compression levels with zero cost.

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.

7zip Bash Aliases Check Bashrc

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.

7zip Bash Aliases Commandline Options

Create the ultra-compression alias

Open the file “~/.bash_aliases” in your favorite word processor – we use nano. Enter:

7zip Bash Aliases First Alias

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.

7zip Bash Aliases Compression Variants

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.

7zip Bash Aliases Using The Aliases

If you have any questions, let us know in the comments below.

Odysseas Kourafalos Odysseas Kourafalos

OK's real life started at around 10, when he got his first computer - a Commodore 128. Since then, he's been melting keycaps by typing 24/7, trying to spread The Word Of Tech to anyone interested enough to listen. Or, rather, read.

2 comments

  1. Bash aliases are great when one spends a lot of time using CLI. For occasional CLI users, they are mostly a waste of time. It is faster to look up the command syntax and then enter the command than it is to try to remember what alias represents what command and options. There also is a point of diminishing returns when your alias list starts to rival the Bash command set in number of commands.

    Having said that, I can see using aliases to replace commands with complicated/convoluted syntax such as the 7z examples given in the article. I have read quite a few articles on using aliases. The vast majority of them offer simplistic examples in which it is almost as easy to enter the command/options as it is to enter the alias. Yours is the first article that I can remember that takes a command with complex set of options and makes it into a very simple alias.

    1. Thanks for the kind words. I should note that this follows my approach to Using Tech and a pattern of some of my past articles here, where I’m trying to simplify/make quicker even already relatively simple procedures. Like, it’s not HARD nor takes a lot of time to run VLC and “tell it to open an Internet stream”, but why repeat the same stuff when you could have it auto-detect “you copied a YouTube URL to the clipboard” and “intercept” it?

      I love stuff like that :-)

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.