In the following bash
backup script:
PROJECT="testPrj"
BACKUP_DIR="~/Documents/backups/"
BACKUP_FILES="./*.sh ./*.h ./*.hpp ./*.c ./*.cc ./*.cpp ./*.md ./*.txt ./BUILD"
BACKUP_TIME=_`date +%Y%m%d_%H%M`
BACKUP_FILENAME=$BACKUP_DIR$PROJECT$BACKUP_TIME.tar.bz2
mkdir -p $BACKUP_DIR
echo "Created backup directory:" $BACKUP_DIR
echo $BACKUP_FILENAME
tar -cpjf $BACKUP_FILENAME $BACKUP_FILES
This is the output:
Created backup directory: ~/Documents/backups/ ~/Documents/backups/testPrj_20170206_1609.tar.bz2
I get the compressed file in the wrong path. Instead of being:
~/Documents/backups/
it goes in: \~/Documents/backups/
This destination directory effectively exists, and it is in the local path.
Running mkdir
on its own from the command line creates the directory in the right place.
~
won't be expanded to your home directory when it's in quotes. Leave it (and the following /
) unquoted, like this:
BACKUP_DIR=~/"Documents/backups/"
Also, it's safest to use lowercase or mixed case for variable names so you don't accidentally use a variable name that has special meaning to the shell or other programs (using $PATH
is the classic example).