Skip to main content
3 of 4
remove TEST word which is just for the 6 char edit restriction ... lol

I scripted these 2 commands:

gzipdir:

#!/bin/bash
if [[ -d $1 ]]; then
    cd "$1"
    cd ..
    base=$(basename "$1")
    tar -zcvf "$base.tgz" "$base"
    if [[ $? == 0 && -f "$base.tgz" ]]; then
        rm -rf "$base"
    fi
else
    echo "Usage: $0 DIRECTORY";
fi

ungzipdir:

#!/bin/bash
if [[ -f $1 ]]; then
    base=${1%%.*}
    file=$(basename "$1");
    dir=$(basename "$base");
    if [[ ! -d $base ]]; then
        mkdir "$base"
        cd "$base"
        cd ..
        tar -xvf "$file"
        if [[ $? == 0 && -d "$dir" ]]; then
            rm -f "$file"
        fi
    else
        echo "Directory $base already exists. Nothing done."
    fi
else
    echo "Usage: $0 file.tgz";
fi

(!!!) Please test before use (as there is a 'rm -f' which could potentially remove important data if used in an uncommon way).

How to use:

cd /home/; gzipdir MyDirectory or gzipdir /home/MyDirectory

Will create /home/MyDirectory.tgz and remove MyDirectory on success (!!!).

ungzipdir /home/MyDirectory.tgz

Will create /home/MyDirectory and remove /home/MyDirectory.tgz on success.

( EDIT: fix ungzipdir typo )

lepe
  • 411
  • 5
  • 9