How to Create Self-Extracting Archives with shar in Linux

Auto Extracting With Shar Featured

Isn’t it annoying, even tiresome, having to explain to a contact what it is and how to unpack it after sending them an archive? If you answered “yes,” you’ll love shar in Linux.

With shar, you can “pack” many files into a single one. If you send it to a contact of yours, they’ll just have to make sure it’s executable and run it to extract it. There is no complicated commands and no need to guide them through the process.

Let’s see how you can use it to pack a group of files into a single “shar” file.

Install Shar

Shar is not included in most Linux distro by default, so you will have to install it first to be able to create auto-extracting Shar file archives. You won’t find it in the Software Center, though, nor on its own through apt. Instead, you will have to install the larger “sharutils” package that contains it. To do that, fire up your terminal and use the command:

sudo apt install sharutils
Auto Extracting With Shar Installation

Find and Prepare Your Files

Shar is a command-line tool and acts on a bunch of files at once, placing them in a single archive. Thus, for convenience and ease of use, create a temporary folder and move or copy all the files you’d like to include in a shar archive to the temporary folder.

With your terminal still active, cd to your newly-made directory.

Auto Extracting With Shar Ls

Pack Your Files

To create your shar archive, run the following command:

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

Change the “archive-filename” to your preferred filename.

Auto Extracting With Shar Pack Files

Let’s “disassemble” this to make sense of how you can use it for your files.

  • The shar at the very beginning is, of course, the program itself.
  • The ./* is the input, and in this specific case, it means “all the files in the directory we are into.”
  • > is the splitter between the input and the output of the command. The program understands it as “take every input on the left of the bracket and combine it into the single file defined on the right of the bracket.”
  • ../archive-filename.shar is the path and the name of the output file. You can change it to anything you wish.

The process is quite fast and usually doesn’t take more than seconds (depending on your PC’s performance).

Auto Extracting With Shar Pack Complete

Once you have created the archive, you can share your new file with your contacts. Although they, too, will need to install sharutils for the auto-extraction to work, as you will see in the next step that everything will be simpler than dealing with typical archives.

Extracting the shar Archive

When your friend receives the shar archive, all they need to do is make it executable and then run it.

Assuming your friend already has sharutils installed, they can extract the archive with the following commands:

chmod +x archive.shar
./archive.shar
Auto Extracting With Shar Make Exe
Auto Extracting With Shar Run Shar

And that was it – the files it contained were extracted right next to it in the same folder. We cam now remove the original file they sent us.

Auto Extracting With Shar Extracted Files

Most Linux desktops provide comprehensive support for compression formats like tar, gz, zip, etc., so shar is not very useful in this case. However, if you are using Linux in a server (or headless) environment, shar will be very useful, as you can easily extract an archive without having to remember the various commands – there are no extra steps, no flags and switches, and no other programs to install. In addition to shar, Windows, macOS and Linux all come with a native app to create self-extracting archive. Check it out!

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox