Created
July 24, 2018 07:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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