How to Simplify 7z Compression with Bash Aliases

There are several methods and tools you can use to shrink your files to reduce the space they take up or send them as a package to your contacts. Among them, with its unique combination of excellent performance and compression level and zero cost, 7-Zip has risen to the top of most users.

All compressed forms of this format are more complicated than the methods we will see here. At least for most users who use compression to pack everything in a folder and its subfolders into a compressed “bundle.”

So let’s see how to combine Bash’s support for aliases with the command line version of 7z to create a package 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 the terminal. If it is not installed, you can install it in an Ubuntu / Debian-based distribution using the following command:

sudo apt-get install p7zip-full

Ceate Bash alias file

Bash’s alias allows mapping of commands or even multiple command sequences into a single word. An easy way to add an alias to Bash is to insert the alias at the end of the “~ / .bashrc” file. For convenience and organization, it is best to keep them in separate files.

The file may already exist, and Bash’s configuration includes a reference to the file. Check for the existence by opening “~ / .bashrc” in your favorite word processor, and search for:

if [-f ~/.bash_aliases]; then
. ~/.bash_aliases

If not, add it to the end of the file.

7 zip codes available

You can run 7-Zip in a terminal to see a summary of its command line options. For more comprehensive information, please visit This user guide Or the program’s man page contains:

man 7z

These will help you set up your own compression commands. We will most likely see products here that meet your needs. So you can copy and paste them into your own .bash_aliases file.

7zip Bash alias command line options

Create a super-compressed alias

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

alias 75='7z a -r -t7z -m0=lzma2 -mx=9 -myx=9 -mqs=on -ms=on'

7zip Bash alias first alias

alias 75 Say we are going to create the command “75”, from now on it will be used to compress all files and folders in the directory into a packed 7-Zip bundle. The reason we use this special name for the command is that, as a short version of “7zip compression level 5”, it is easy to remember.

7z It is the compression command itself. of a What follows is “We want to add the file to a new compressed package.” -r Indicates that p7zip should not be “limited to the folder where it is run”, but should include the final compressed package and all its subfolders and all its contents.

-t7z -m0=lzma2 It means that we want to create a 7zip software package that mainly uses the LZMA2 algorithm for file compression. In general, this will produce the best compression for most file types.

-mx=9 -myx=9 Corresponding to the compression level, “7-Zip” will use “workload” to analyze the contents of the files to be compressed to find the best compression strategy. The higher their value, the greater the compression and the smaller the resulting beam.

At last, -mqs=on -ms=on Define that we want to “fix” the compression. This means that p7zip will compress similar files into a single block of data for better results. The reason they are an option you can turn off is that they are useful, but they also have two negative effects. They lengthen compression time and cannot decompress stand-alone files from the final bundle generated by the compressor. When using solid-state compression, you cannot equate to “extracting a single file from a zip”, and you must unzip the entire bundle to access its contents.

Duplicate and “cut” aliases

We start with the command to achieve maximum compression so that instead of expanding more elements backwards, we can move backwards, delete parameters, and decrease the set value.

First copy the existing commands five times, and then place each command on its own line. Leave the first line unchanged as it has reached the highest possible compression level. Delete the remaining options in order and lower the compression level, and modify the rest in order, as shown below. Remember to change its alias to match a lower compression level.

7zip Bash alias compression variant

You can always copy and paste the following into ~ / .bash_aliases.

alias 75='7z a -r -t7z -m0=lzma2 -mx=9 -myx=9 -mqs=on -ms=on'
alias 74='7z a -r -t7z -m0=lzma2 -mx=9'
alias 73='7z a -r -t7z -m0=lzma2 -mx=7'
alias 72='7z a -r -t7z -m0=lzma2 -mx=5'
alias 71= '7z a -r -t7z -m0=lzma2 -mx=3'
alias 70='7z a -r -t7z -m0=lzma2 -mx=1'

Activate your alias

Save the changes to a file and return to the terminal. To load and activate the new alias, use the following command:

source ~/.bashrc

Your new compression command is active. If you do not define “what to compress” as a parameter, P7zip runs on all files in the run directory by default. So from now on, to compress the contents of the folder into a 7z package, all you need to do is enter the following command:

75 archive_name

Here “75” is an alias for the highest compression level in earlier versions, and “archive_name” is the name of the archive file. You can replace “75” with one of the other aliases (from 75 to 70) to gradually reduce the compression speed and use the desired name for the final file.

7zip Bash aliases using aliases

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