How to generate a bash script with an embeeded tar.gz (self-extract)

Consider that you need to perform a routine in a remote server, where you need to decompress a and execute a list of commands on this data. One alternative is send the tar.gz file to the remote server throught a ftp or scp and then log in the remote server and run a shell script or run manually a list of commands. Recall Java JRE setup, they use script.bin that comes with an embeeded tar.gz, which is self-extracted in the beginning of script execution. To build the self-extraction script I follow a tutorial published by Stuart Wells, which consists in four steps:

1) Create/identify a tar.gz file that you wish to become self extracting.

2) Create the self extracting script. A sample script is shown below:

> cat
echo "Extracting file into `pwd`"
# searches for the line number where finish the script and start the tar.gz
SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`
#remember our file name
# take the tarfile and pipe it into tar
tail -n +$SKIP $THIS | tar -xz
# Any script here will happen after the tar file extract.
echo "Finished"
exit 0
# NOTE: Don't place any newline characters after the last line below.

3) Concatenate The script and the tar file together.

> cat example.tar.gz >
> chmod +x

4) Now test in another directory.

> cp /tmp
> cd /tmp
> ./

See ya!

5 Responses to How to generate a bash script with an embeeded tar.gz (self-extract)

  1. Pingback: Breves « Ubuntu Life

  2. inukaze says:

    Me gustaria primero comprimir los videos del Quake 2 , y luego generar nuevamente , el archivo de autoextraccion “” , supongo que funciona de esta manera , pero como genero en un archivo “.run” ???

    Ya que la utilidad que disponia , me pedia que tubiera listo un script para crear el archivo “.run” , me refiero al Quake 2 para Linux que incluye un Instalador Grafico , y las expansiones. y mejoras graficas. tambien como los videos.

    Alguien me puede ayudar con esto ???

  3. emanuelvianna says:

    Hi inukaze,
    Let me see if I understood your problem, you want to be able to decompress a pack with several files (Quake installer for Linux, extensions and videos) running a script (./ instead of manually decompress it (tar -xzf pack.tgz), right? Or, will you execute other commands using the decompressed pack files? Recall that this tutorial only applies to embed one compressed file, so you may generate a single compressed file with all your data. I suggest you (for testing) to compress a smaller directory (tar -czf directory.tgz directory) and append it to a script (cat directory.tgz >> containing the code above. Make script executable (chmod +x and run it. If you need to run other commands after decompress the directory (such enter in the directory, configure and execute a Quake installer) you may put your commands after the line 10. If you have other doubts feel free to ask.
    Best regards,

  4. gjyang says:

    #remember our file name

  5. Ehteshaam says:

    Thanks Emanuel.
    It is just fits my need.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: