How to make a self-extracting archive on Linux
A self-extracting archive is a compressed file that can extract its contents with no special programs required. Many software developers use self-extracting archives to quickly and efficiently deliver their software. However, self-extracting archives have other uses, too, namely, allowing the decompression of files without having to deal with complicated tools like Tar, Zip, and others.
In this guide, we’ll show you how to create your self-extracting archive on Linux. We will also go over how to encrypt your self-extracting archives, for extra security.
Installing the Shar tool
The first thing we must do is install the Shar tool on Linux. The reason? Despite it being a widely used utility, Shar does not come pre-installed on very many Linux operating systems. To start the installation, open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, follow the command-line installation instructions outlined below.
Ubuntu
On Ubuntu Linux, you can get the Shar app with the following Apt command.
sudo apt install sharutils
Debian
Are you using Debian Linux? You’ll be able to get Shar working with the Apt-get command below.
sudo apt-get install sharutils
Arch Linux
Need to get the Shar up and running on your Arch Linux PC? Make use of the Pacman command down below.
sudo pacman -S sharutils
Fedora
On Fedora Linux, install the Shar application on your system with the following Dnf command.
sudo dnf install sharutils
OpenSUSE
Are you an OpenSUSE Linux user? Install the Shar program with the Zypper command below.
sudo zypper install sharutils
Generic Linux
Need to get your hands on the Shar application and using a release of Linux not covered in this guide? Head over to this page to learn how to download the source code.
Creating a Shar archive
Creating a Shar archive is done in the terminal, as the Shar program is a command-line only utility. To create your archive, open up a terminal window. Then, follow the step-by-step instructions below.
Step 1: Use the ls command to view the contents of your home directory.
ls
Step 2: Look through the output of the ls command and locate the folder in which you want to compress the contents. Then, enter it with the CD command. For example, if you want to compress all of the documents files inside of the “Documents” directory, you’d do cd Documents. To add the contents of your “Downloads” folder, you’d do cd Downloads, etc.
cd name-of-folder-in-home-directory
Step 3: Once inside of the folder, run the shar command to add all files inside (with the CD command in Step 2) to a new .shar archive.
Keep in mind that when running the shar command, it will add absolutely everything to the archive. If there are items in the folder you do not want to be added to the archive, move them out of it with the Linux file manager before executing the command below.
shar ./* > ../my-shar-archive.shar
Step 4: Upon executing the shar command in Step 3, the Shar application will begin creating your self-extracting archive. Be patient and allow the command to execute. When the process is complete, you will see my-shar-archive.shar
appear in your home directory (~).
Step 5: After creating your new self-extracting Shar archive, the process isn’t complete. Before being able to run it to extract the contents inside, the permissions of the file must be updated.
Updating permissions on Linux is done with the chmod utility. Using chmod +x change my-shar-archive.shar
so that anyone can execute it and extract the contents.
sudo chmod +x my-shar-archive.shar
Be sure to tell your friends that they must also update the permissions of my-shar-archive.shar
on their Linux system as well with the chmod command if they want to extract the contents of it.
Extract your self-extracting archive
To extract the contents of your new self-extracting Shar archive, execute the following command.
./my-shar-archive.shar
The contents of my-shar-archive.shar
will be placed in the exact directory it was extracted.
Encrypting your self-extracting archive
If you’re sending sensitive files in your self-extracting archive over the internet, you may want to encrypt it for security purposes. Thankfully, encrypting self-extracting archive files on Linux is very easy. To do it on your system, follow the step-by-step instructions below.
Step 1: Ensure that you have the GPG tool installed on your Linux PC by executing the gpg --help
command in a terminal. If GPG is installed, you will see the GPG help page. If it is not, nothing will happen.
Note: need to install GPG? Click here.
gpg --help
Step 2: Encrypt your self-extracting archive with the gpg command below. Be sure to use a secure password.
gpg -c my-shar-archive.shar
Once encrypted, the output file will be my-shar-archive.shar.gpg
.
Step 3: Delete the unencrypted Shar archive with rm.
rm my-shar-archive.shar
Step 4: Send out my-shar-archive.shar.gpg
to a friend, family member, or upload online for safekeeping.
To extract the file, execute the following commands.
gpg my-shar-archive.shar.gpg ./my-shar-archive.shar