- while (( $# > 0 )); do
- if islinux; then
- cp -a "$1" "$1_$(date --iso-8601=m)"
- elif isopenbsd; then
- cp -R "$1" "$1_$(date "+%FT%H:%M")"
- elif isfreebsd; then
- if [[ -d "$1" ]] && [[ "$1" == */ ]]; then
- echo "cowardly refusing to copy $1 's content; see cp(1)" >&2; return 1
+ local current_date=$(date -u "+%Y-%m-%dT%H:%M:%SZ")
+ local clean keep move verbose result
+ setopt extended_glob
+ usage() {
+ cat << EOT
+bk [-hcmv] FILE [FILE ...]
+bk -r [-v] [FILE [FILE ...]]
+Backup a file or folder in place and append the timestamp
+Remove backups of a file or folder, or all backups in the current directory
+
+Usage:
+-h Display this help text
+-c Keep the file/folder as is, create a copy backup using cp(1) (default)
+-m Move the file/folder, using mv(1)
+-r Remove backups of the specified file or directory, using rm(1). If none
+ is provided, remove all backups in the current directory.
+-v Verbose
+
+The -c, -r and -m options are mutually exclusive. If specified at the same time,
+the last one is used.
+
+The return code is the sum of all cp/mv/rm return codes.
+EOT
+ }
+ keep=1
+ while getopts ":hcmrv" opt; do
+ case $opt in
+ c) unset move clean && (( ++keep ));;
+ m) unset keep clean && (( ++move ));;
+ r) unset move keep && (( ++clean ));;
+ v) verbose="-v";;
+ h) usage;;
+ \?) usage >&2; return 1;;
+ esac
+ done
+ shift "$((OPTIND-1))"
+ if (( keep > 0 )); then
+ while (( $# > 0 )); do
+ if islinux || isfreebsd; then
+ cp $verbose -a "${1%/}" "${1%/}_$current_date"