How to create self-extracting archives with shar in Linux

It's annoying, even tiring, to explain to someone how to unzip after you send them an archive. If so then you will definitely love shar in Linux.

It's annoying, even tiring, to explain to someone how to unzip after you send them an archive. If so then you will definitely love shar in Linux.

With shar, you can 'bundle' multiple files together. If you send it to someone, they just need to make sure it is executable and run to extract it. No need for complex commands or instructions during the process.

Let's see how you can use shar to group files through the following article!

Install shar

Shar is not included in most Linux distributions by default, so you will have to install it first in order to create Shar file archives automatically extracted. However, you won't find it in Software Center or via apt. Instead, you will have to install larger sharutils packages that contain shar. To do that, activate Terminal and use the command:

 sudo apt install sharutils 

Shar is a command line tool and works on multiple files at once, putting them into a single archive. Therefore, for convenience and ease of use, create a temporary directory and move or copy all the files you want to put into the shar archive to that temporary directory.

With the Terminal still active, use the cd command to move the file into the newly created directory.

Picture 1 of How to create self-extracting archives with shar in Linux
Use the cd command to move the file into the newly created directory

Group files

To create a shar archive, run the following command:

 shar ./* > ./archive-filename.shar 

Change archive-filename to the preferred file name.

Feel free to dig deeper to understand how you can use your files.

  1. Of course, shar is the program.
  2. ./* is the input, and in this particular case, it means all the files in the directory.
  3. > separates the input and the output of the command. The program understands it as follows: Take all the input on the left of the> and combine it into a single file identified on the right of the>.
  4. ./archive-filename.shar is the path and name of the output file. You can change it to whatever you want.

This process is quite fast and usually does not take more than a few seconds (depending on PC performance).

Picture 2 of How to create self-extracting archives with shar in Linux
Group files

Once you've created the archive, you can share your new files with others. Although they will also need sharutils installed to automatically extract operations, as you will see in the next step, things will be simpler than dealing with regular repositories.

Extract shar archive

When the other person receives the shar repository, all they need to do is make it workable, then run it.

Assuming the other person has sharutils installed, they can extract the archive using the following commands:

 chmod +x archive.shar ./archive.shar 

The files contained therein are extracted right next to, in the same directory.

Picture 3 of How to create self-extracting archives with shar in Linux
Extract shar archive
Update 08 April 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile