Skip to content

Instantly share code, notes, and snippets.

@chouyang
Created July 24, 2018 07:27
Show Gist options
  • Save chouyang/011b60244c6e10aa50dad3c015ead577 to your computer and use it in GitHub Desktop.
Save chouyang/011b60244c6e10aa50dad3c015ead577 to your computer and use it in GitHub Desktop.
[compress and delete files] compress and delete files using tar command #tar #tgz
#!/bin/bash
if [ $# -eq 0 ]; then
echo "No arguments supplied."
exit
fi
if [ -n "$2" ]; then # if second argument exists
if [ -e "$2" ]; then # if second argument is a valid path
if [ -d "$2" ]; then # if second argument is a directory
tar -czf "$2/$1.tgz" "$1" && rm "$1"
else # if second argument is a file
echo "Target file exists."
fi
exit
else # if second argument does not exist
if [ -d "$(dirname $2)" ]; then # if base directory of second argument exists
tar -czf "$2" "$1" && rm "$1"
else
echo "Directory $(dirname $2) does not exist."
fi
fi
exit;
fi
tar -czf "$1.tgz" "$1" && rm "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment