Skip to content

Instantly share code, notes, and snippets.

@wraithmonster
Created March 4, 2012 22:04
Show Gist options
  • Save wraithmonster/1975025 to your computer and use it in GitHub Desktop.
Save wraithmonster/1975025 to your computer and use it in GitHub Desktop.
Archive the specified directory, then remove it
#!/bin/bash
################################################################################
#
# Title: marmdir.sh
# Description: Archive the specified directory, then remove it
# Author: wraithmonster
# Reference: http://www.apl.jhu.edu/Misc/Unix-info/tar/tar_28.html
# http://bit.ly/bash-string-length
# http://www.linux.com/archive/feature/120291
#
################################################################################
# Provide standard messages
verbose=false
#Provide messages of all commands in this script
veryVerbose=false
# Message when a given directory does not exist
noDirMessage="No DIRECTORY specified. Try '$(basename $0) --help' for help."
################################################################################
# Functions
################################################################################
function checkDirs() {
# Strip whitespace
archive=`expr "$1" : '[[:space:]]*\(.*\)[[:space:]]*$'`
archiveLoc=`expr "$2" : '[[:space:]]*\(.*\)[[:space:]]*$'`
# Check for null and whitespace
if [ -z "$archive" ] || [ ! -n "$archive" ]; then
errorMessage "$noDirMessage"
fi
if [ -z "$archiveLoc" ] || [ ! -n "$archiveLoc" ]; then
warningMessage "No TARGET DIRECTORY specified. Using current directory ($(basename `pwd`))."
archiveLoc="`pwd`"
fi
# Do the specified directories exist?
if [ ! -d $archive ]; then
errorMessage "$archive is not a directory. Try '$(basename $0) --help' for help."
fi
if [ ! -d $archiveLoc ]; then
errorMessage "$archiveLoc is not a directory. Try '$(basename $0) --help' for help."
fi
}
function checkParamStr() {
# If there are no parameters, throw error
if [ "${#1}" -eq "0" ]; then
errorMessage "$noDirMessage"
fi
}
function errorMessage() {
echo -e "Error: $1"
exit 1
}
function warningMessage() {
if [ $verbose == true ]; then
echo -e "Warning: $1"
fi
}
function outputUsage() {
echo "marmdir - Archive & remove a dir"
echo "Usage: `basename $0` [options...] [DIRECTORY] [TARGET DIRECTORY]"
echo "Options:"
echo " -v/--verbose Not yet implemented"
echo " -h/--help Output this message"
exit 1
}
################################################################################
# Command line processing
################################################################################
# This script requires at least one argument, so let's check for that first
checkParamStr "$@"
# Parse the command line arguments
while [ "$#" -gt "0" ]; do
case "$1" in
-v|--verbose)
verbose=true
shift 1
checkParamStr "$@"
;;
-V|--veryverbose)
verbose=true
veryVerbose=true
shift 1
checkParamStr "$@"
;;
-h|--help)
outputUsage
;;
-*|--*)
# Unknown option
errorMessage "Unknown option $1."
;;
*)
archive="$1"
shift 1
archiveLoc="$1"
shift 1
# If we have any more parameters left, the usage is incorrect
if [ "$#" -gt "0" ]; then
errorMessage "Unknown parameter $@."
fi
checkDirs $archive $archiveLoc
break
;;
esac
done
################################################################################
# Main
################################################################################
# Archive & remove the given directory
# Give messages if verbose
if [ $verbose == true ]; then
echo "Archiving '$(basename $archive)' to '$(basename $archiveLoc)'..."
if [ $veryVerbose == true ]; then
tar -cvzf "$archiveLoc/$(basename $archive).$$.bak.tar.gz" $archive
else
tar -czf "$archiveLoc/$(basename $archive).$$.bak.tar.gz" $archive
fi
echo "Done."
echo "Removing '$(basename $archive)'..."
if [ $veryVerbose == true ]; then
rm -frv $archive
else
rm -fr $archive
fi
echo "Done."
# If not verbose, perform actions silently
else
tar -czf "$archiveLoc/$(basename $archive).$$.bak.tar.gz" $archive
rm -fr $archive
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment