Skip to content

Instantly share code, notes, and snippets.

@frosit
Created June 16, 2022 14:50
Show Gist options
  • Save frosit/21ae09002bf1d7c8db57c554c6220e9f to your computer and use it in GitHub Desktop.
Save frosit/21ae09002bf1d7c8db57c554c6220e9f to your computer and use it in GitHub Desktop.
make tar
#!/usr/bin/env bash
if [[ "$1" == "-h" || "$1" == "--help" || ! ${1+x} ]]; then cat <<HELP
Creates a tar file of specified file/folder
Usage: $(basename "$0") [folder-to-tar] [{save-as}.tar.gz]
This command will tar the file/folder of the first argument.
It will use the first argument als filename adding .tar.gz
If a second argument is supplied, this will be used as the location/name to save the tar.
Copyright (c) 2018 "FROSIT" Fabio Ros
Licensed under the MIT license.
http://frosit.nl
HELP
exit; fi
# Check if file or folder exists
if [[ ! -d ${1} && ! -f ${1} ]]; then
echo -e "File or folder ${1} not found."
exit;
fi
dest="${1%%/}.tar.gz"
if [ ${2+x} ]; then
dest="${2}.tar.gz"
fi
# If already exists, add timestamp
if [ -f $dest ]; then
if [ ${2+x} ]; then
dest="${2}-$(date '+%d-%m-%Y_%H-%M-%S').tar.gz";
else
dest="${1}-$(date '+%d-%m-%Y_%H-%M-%S').tar.gz";
fi
fi
echo -e "Creating tarball"
echo -e "Source: ${1}"
echo -e "Destination: ${dest}"
tar -cf - ${1} | pv -s $(du -sb ${1} | awk '{print $1}') | gzip > ${dest}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment