简体   繁体   中英

Bash: Moving Files with Regex that Matches Pattern of Script Arugment and Fixed Text

I'm trying to write a backup script that takes a directory and directory/file name as arguments to the script. The problem is curating the target directory of the backups. For safety, I'm currently moving the files into MacOS ~/.Trash/ . The problem is that I want to support having spaces in the target directory's file name, but escaping the path in mv prevents shell expansion of * .

The script in question:

# Usage: backup-to-dropbox.sh "path/containing/target" "target dir"
cd "$1"
DATE=`date "+%Y%m%dT%H%M%S"`
SOURCE="$2"
mv "~/Dropbox/Backups/$SOURCE*.tgz" ~/.Trash/ ## Problem line here
tar -czf "$SOURCE $DATE.tgz" "$SOURCE/"
mv "$SOURCE $DATE.tgz" ~/Dropbox/Backups/

How can I match all the files with this known, arbitrary prefix and fixed extension?

Words can be partially quoted. Be sure not to quote anything expanded by the shell.

mv ~/"Dropbox/Backups/$SOURCE"*.tgz ~/.Trash/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM