Skip to main content
remove TEST word which is just for the 6 char edit restriction ... lol
Source Link
Archemar
  • 32.3k
  • 18
  • 75
  • 107

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 )

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 )

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.

remove TEST word which is just for the 6 char edit restriction ... lol
Source Link

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 (!!!).

gunzipdirungzipdir /home/MyDirectory.tgz

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

( EDIT: fix ungzipdir typo )

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 (!!!).

gunzipdir /home/MyDirectory.tgz

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

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 )

add space protection
Source Link
lepe
  • 411
  • 5
  • 9

I scripted these 2 commands:

gzipdir:

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

ungzipdir:

#!/bin/bash
if [[ -f $1 ]]; then
    base=${1%%.*}
    file=$(basename $1"$1");
    dir=$(basename $base"$base");
    if [[ ! -d $base ]]; then
        mkdir $base"$base"
        cd $base"$base"
        cd ..
        tar -xvf $file"$file"
        if [[ $? == 0 && -d $dir"$dir" ]]; then
            rm -f $file"$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 (!!!).

gunzipdir /home/MyDirectory.tgz

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

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 (!!!).

gunzipdir /home/MyDirectory.tgz

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

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 (!!!).

gunzipdir /home/MyDirectory.tgz

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

Source Link
lepe
  • 411
  • 5
  • 9
Loading