how to compress a files inside a shell script file?

5,620

Use shar for Shell Archives

Create shell archives using the shar utility. For example:

# Create some fixture data to archive.
cd /tmp
mkdir -p foo/bar/baz
touch foo/bar/baz/quux

# Create the shell archive.
find foo/ -exec shar {} + > quux.sh

# Remove fixture data.
rm -rf foo/

# Extract archive to recreate directories and files.
sh quux.sh

You can validate that this recreated your data with ls -R foo. In addition, if you inspect quux.sh, you'll see that it's composed of a series of simple shell commands. You can insert those commands directly into another shell script, if desired, or call the archive from another script to unpack it.

Caveats

The BSD version of shar on macOS doesn't handle binary data without a separate encoding step, while the GNU version has various encoding options available to it. If your version of shar doesn't handle binary encoding, you may be better off with a more robust archiving option like tar. Your mileage may vary.

Share:
5,620

Related videos on Youtube

Pradyumna Sagar
Author by

Pradyumna Sagar

Updated on September 18, 2022

Comments

  • Pradyumna Sagar
    Pradyumna Sagar over 1 year

    I have seen a .sh file on execution in terminal it extracts some jar files inside the sh file, how to create such sh files? i could not open that sh file in normal gedit to check the content. Is there any way to do that?

    • Admin
      Admin over 7 years
    • Admin
      Admin over 7 years
      The trick to a self extracting shell file is to simply create a normal archive using standard tools such as tar (or zip, cpio etc.) and then wrap the resulting .tar file in a couple lines of shell script that will separate out the archive from the script, extract it again and execute any additional setup steps. Decoding that should be trivial, but an editor attempting to do syntax highlighting or matching braces on the binary contents of such a file might be quite slow. Try /bin/vi instead of gedit.
    • Admin
      Admin over 7 years
      Maybe you're thinking of shar for shell archives? See man shar for details.
  • Pradyumna Sagar
    Pradyumna Sagar over 7 years
    that was not the answer to my question