Skip to content

Instantly share code, notes, and snippets.

@shelbyKiraM
Last active January 24, 2019 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelbyKiraM/94012572633064e0f18cdd9f36c0247b to your computer and use it in GitHub Desktop.
Save shelbyKiraM/94012572633064e0f18cdd9f36c0247b to your computer and use it in GitHub Desktop.
ZSH function overwriting the shell built-in `cp`, using `rsync` with a progress bar.
function cp() {
if [ -f "$1" ];then
SIZE="$(stat -f '%z' "$1")"
if [ -e "$2" ];then
echo "File exists. Overwrite (Y/n)? "
read todo
if [ "$todo" = "y" ] || [ "$todo" = "Y" ] || [ "$todo" = "Yes" ] \
|| [ "$todo" = "yes" ] || [ "$todo" = "" ];then
echo "Overwriting $2 with $1"
else
return
fi
fi
pv -s $SIZE "$1" > "$2"
elif [ -d "$1" ];then
if [ ! -e "$2" ];then
type="Copying"
elif [ -d "$2" ];then
read todo?"File exists. Merge (Mm) or Overwrite [Oo]? "
if [ "$todo" = "M" ] || [ "$todo" = "m" ];then
echo "Merging $1 to $2"
elif [ "$todo" = "O" ] || [ "$todo" = "o" ];then
echo "Overwriting $2 with $1"
rm "$2"
else
echo "Please choose M or O"
return
fi
fi
rsync -ahvPu "$1" "$2"
elif [ $1 != '' ];then
echo "Something went wrong. Please use \`cpp $1 $2\`"
else
echo 'Usage: cp input output'
return
fi
say 'done'
}
@shelbyKiraM
Copy link
Author

One issue, it doesn't allow wildcard copying? Yeah, it just deals with $1 and $2... Idk how to implement all that. Probably gonna be a loop…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment