How to simplify 7z file compression with Bash Alias

7-Zip wins the No. 1 position in the hearts of most users, thanks to a unique combination of performance and great compression level at zero cost.

There are many ways and tools you can use to shrink your files, to reduce the space they take up or send them as packets to a contact. Of these, 7-Zip won the No. 1 position in the hearts of most users, thanks to a unique combination of performance and great compression level at zero cost.

All the forms of compression in this format are actually more complex than the approach we will see here. At least most users use compression to package everything in a directory and its subfolders into compressed packages.

So let's see how Bash combines alias support with the command line version 7z to create packages for files with a simple command.

Using Bash Alias ​​simply compresses the file

  1. Install p7zip
  2. Create Bash alias
  3. Make 7-Zip information handy
  4. Create super compression alias
  5. Replicate and cut down the alias
  6. Activate your alias

Install p7zip

As the most popular compression tool, 7-Zip is most likely already installed in your Linux distribution. To test it, try the command 7z in a terminal. If not already installed, you can install it in Ubuntu / Debian-based distributions using the command:

 sudo apt-get install p7zip-full 

Create Bash alias

Bash aliases allow you to map multiple commands or even strings of commands into single words. An easy way to add aliases to Bash is to insert them at the end of the '~ / .bashrc' file. For the sake of convenience and to keep things organized, it's best to put them in a separate file.

It is possible that this file already exists and the Bash configuration includes a reference to it. Check if that is true by opening '~ / .bashrc' in your favorite word processor and search:

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

If not, add it to the end of the file after everything is available.

How to simplify 7z file compression with Bash Alias Picture 1How to simplify 7z file compression with Bash Alias Picture 1

Make 7-Zip information handy

You can run 7-Zip in a terminal to see a list of its command line options. For comprehensive information, visit the user guide: https://sevenzip.osdn.jp/chm/cmdline/index.htm or the program's man page with:

 man 7z 

These will help you set up your own compression commands. Most likely, the ones mentioned here will also meet your needs. So you can copy and paste them into your own .bash_aliases file.

How to simplify 7z file compression with Bash Alias Picture 2How to simplify 7z file compression with Bash Alias Picture 2

Create super compression alias

Open the file '~ / .bash_aliases' in your favorite word processor (article using nano ). Enter the following command:

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

How to simplify 7z file compression with Bash Alias Picture 3How to simplify 7z file compression with Bash Alias Picture 3

alias 75 says we want to create the '75' command, which we will use from now on to compress all files and folders in a folder into a 7-Zip package. The reason the example applies this special name to the command is because it is a brief version of '7zip compression level 5' , so easy to remember.

7z is the compression itself. a follows it means that we want to add the files to a new compressed package. -r indicates that p7zip is not bound to the directory where it runs, but should include the final zipped package and all its subfolders, along with all its contents.

-t7z -m0 = lzma2 indicates that we want to create 7zip packages mainly using the LZMA2 algorithm to compress files. This usually yields the best compression result for almost any file type.

-mx=9 -myx=9 corresponds to the level of compression and the "effort level" 7-Zip will take into account the content analysis of the compressed files to find the best compression strategy. The higher their value, the greater the compression and the smaller the output package.

Finally, -mqs=on -ms=on determines that we want the level of hard compression (solid). This means that p7zip compresses similar files in a single block of data, achieving even better results. The reason they exist as an option you can disable is because they are useful, but they also come with 2 drawbacks. They extend the compression time and make it impossible for users to extract files independently from the final package created by the compression tool. With the level of hard compression, you can not perform the extraction of a single file from the zip file, and must extract the entire package to access its contents.

Replicate and cut down the alias

The article started with the command to achieve the maximum possible compression level instead of expanding it with more elements, for example being able to go back, remove parameters, and reduce the values ​​set.

Start by copying the existing order 5 more times, placing each command in a separate line. Leave the first line, as it has reached the highest possible compression level. Modify the rest, in order, by removing additional options and reducing the level of compression as you see below. Remember to change their alias to accommodate lower compression levels.

How to simplify 7z file compression with Bash Alias Picture 4How to simplify 7z file compression with Bash Alias Picture 4

You can always copy the following items and paste it into your ~ / .bash_aliases instead.

 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 the file and return to the terminal. To load and activate new aliases, use the following command:

 source ~/.bashrc 

New compression commands are active. P7zip works by default on all files in the directory where it runs, if you don't explicitly specify 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 the folder into package 7z is to enter the command:

 75 archive_name 

Here, " 75" is an alias for the highest compression level from before and 'archive_name' is the name of the compressed package. You can replace 75 with one of the other aliases (from 75 to 70 ) for gradual but faster compression and use whatever name you want for the final file.

How to simplify 7z file compression with Bash Alias Picture 5How to simplify 7z file compression with Bash Alias Picture 5

If you have any questions, feel free to leave your comments in the comment section below!

Hope you are succesful.

4.3 ★ | 8 Vote