Home
Biography
Professional
Planning Board
Links
Contact Info

Stuart
WELLS

Self-Extracting tar files

I've created an example of how to create a simple self extracting tar file for my reference.

1) First create/identify a tar file that you wish to become self extracting.

Assumption: A directory "example" exists that we wish to tar.

>ls -l example/
total 4
-rw-r--r-- 1 stuart users 35 2005-02-28 20:50 hello.txt
> tar -czvf example.tar.gz example/
example/
example/hello.txt

2) Create the self extracting script. I've called the script extract.sh
A sample script is shown below:

> cat extract.sh
#!/bin/bash

echo ""
echo "Self Extracting Tar File"
echo ""
echo "Example by Stuart Wells"
echo ""
echo "Extracting file into `pwd`"
SKIP=`awk '/^__TARFILE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`

#remember our file name
THIS=`pwd`/$0

# take the tarfile and pipe it into tar
tail -n +$SKIP $THIS | tar -xz

#
# place any bash script here you need.
# 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.
__TARFILE_FOLLOWS__

3) Concatenate The script and the tar file together.

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

4) Now test in another directory.
> cp example.sh /tmp
> cd /tmp
> ./example.sh

Self Extracting Tar File

Example by Stuart Wells

Extracting file into /tmp
Finished