#!/usr/bin/env bash
if [ "`echo $1 |grep '/$'`" = "$1" ]
then
fullpath=$(echo $1 |sed "s/\/$//")
else
fullpath=$1
fi
if [ "`echo $fullpath |grep '/'`" != "$fullpath" ]
then
fullpath="./$fullpath"
fi
targetName=$(echo $fullpath |awk 'BEGIN{FS="/"} {print $NF}')
path=$(echo $fullpath |sed "s/$targetName//")
echo
echo "Checking Hierachy ... "
echo
if [ -d "$path" ]
then
cd $path
if [ -f "$targetName" ]
then
tar -zcvf $targetNamenew.tar.gz $targetName
else
if [ -d "$targetName" ]
then
tar -zcvf $targetNamenew.tar.gz $targetName
else
echo
echo "Target Does NOT Exist ! Exiting ..."
echo
fi
fi
if [ -f $targetNamenew.tar.gz ]
then
time=$(date "+%Y%m%d_%H%M%S")
# the condition for 'if' in bash should have space after '[' and before ']'
if [ -f "$targetName.tar.gz" ]
then
mv $targetName.tar.gz $targetName.tar.gz_bckedAt_$time
mv $targetNamenew.tar.gz $targetName.tar.gz
else
mv $targetNamenew.tar.gz $targetName.tar.gz
fi
fi
else
echo "Path Not Found ! Exiting ... "
fi
echo