On a Saturday afternoon I had just finished exporting images for my website and was getting ready to move them into the publishing folder. As usual, I asked Cowork to "sync the exported files into the public folder," and a few seconds later a short draft script came back.
I read it before running it, and stopped. The destination was written as "$OUT/site" — everything depended on one variable. What went into OUT was decided a few lines earlier. If that failed and OUT stayed empty, where would the files go? I couldn't answer right away.
The day before, Claude Code issue #113960 had been filed. Someone on Windows ran a sync script and more than 300GB of data was overwritten. The analysis from the reporter's own session points to a likely cause: if S in "$S/c" is empty, the destination becomes /c — the root of the C drive in Git Bash. (The script itself was lost, so this isn't confirmed.) Different tool, I know. But it had the same shape as the draft on my screen.
Here is the line I ended up drawing: I decide where things go; Cowork carries them. Below are the three questions I now ask before any copy or sync runs, and a small script that checks all three at once.
Question 1: If the destination came out empty, where would it write?
A shell variable with nothing in it doesn't raise an error. It is simply dropped into the line as an empty string.
S=""
echo "$S/c" # → /c (top of the filesystem)
echo "$S/" # → / (the root itself)To a human, "$S/c" reads as "c, under somewhere." With S empty, only the leading / survives, and the destination moves somewhere else entirely.
There are two ways to close this. set -u at the top of a script stops on any variable that was never defined — but S="", defined yet empty, still slips through. ${VAR:?message} stops on both undefined and empty, so that is what I use on the line that receives the destination.
DEST="${2:?destination is empty}" # exits right here if emptyQuestion 2: Can it show me what will happen before it moves anything?
The second question is whether I can see what will be added and what will be overwritten before anything is written. rsync has --dry-run, and with --itemize-changes it prints one line per file with a short code.
| Line starts with | Meaning |
|---|---|
>f+++++++++ | a new file the destination doesn't have yet |
>f.s....... and similar | an existing file that will be overwritten (s = size differs, t = time differs) |
cd+++++++++ | a new folder |
rsync has one quirk worth knowing here: src/ with a trailing slash means "the contents of src," while src without it means "the folder src itself." Forget the slash and you get an extra src level inside the destination. That once left me with the same images split across two levels and no quick way to tell which set was newer. The dry-run list shows the deeper path immediately.
Question 3: Will the overwritten files be kept?
The third question is whether I can undo an overwrite. rsync --backup --backup-dir=<folder> moves each file that is about to be replaced into that folder first.
I also leave --delete out of sync requests. It removes anything in the destination that isn't in the source, and the moment the destination is wrong, it deletes from the side you meant to protect. If the destination needs cleaning, I decide that separately, after reading the list.
A script that checks all three
This is copy_guard.sh, which I run in Cowork's working environment. It stops if the destination is outside a parent folder I allow, only prints the plan by default, and writes only when RUN=yes is set.
#!/usr/bin/env bash
# copy_guard.sh — confirm the destination, keep what gets overwritten, then copy
# usage: bash copy_guard.sh <source> <destination> <allowed parent folder>
# prints the plan only; prefix with RUN=yes to actually write
set -euo pipefail
SRC="${1:?source is empty}"
DEST="${2:?destination is empty}"
ROOT="${3:?allowed parent is empty}"
src_abs="$(cd "$SRC" && pwd -P)"
root_abs="$(cd "$ROOT" && pwd -P)"
# the destination itself may not exist yet, but its parent must
dest_parent="$(cd "$(dirname "$DEST")" && pwd -P)"
dest_abs="${dest_parent%/}/$(basename "$DEST")"
stop() { echo "Stopped: $1" >&2; exit 2; }
case "$dest_abs/" in "$root_abs"/*) ;; *) stop "destination $dest_abs is outside $root_abs";; esac
[ "$dest_abs" = "$root_abs" ] && stop "won't write into the allowed parent itself"
case "$dest_abs/" in "$src_abs"/*) stop "destination is inside the source";; esac
case "$src_abs/" in "$dest_abs"/*) stop "source is inside the destination";; esac
# trailing / means "the contents of"; without it you get one level deeper
plan="$(rsync -a --dry-run --itemize-changes "$src_abs/" "$dest_abs/")"
new=$(printf '%s\n' "$plan" | grep -c '^>f+++++++++' || true)
over=$(printf '%s\n' "$plan" | grep '^>f' | grep -vc '+++++++++' || true)
echo "destination: $dest_abs"
echo "new: ${new} / overwrite: ${over}"
printf '%s\n' "$plan" | grep '^>f' | grep -v '+++++++++' | head -20 || true
[ "${RUN:-}" = "yes" ] || { echo "(plan only — set RUN=yes to write)"; exit 0; }
keep="$dest_abs/.before_$(date +%Y%m%d-%H%M%S)"
rsync -a --backup --backup-dir="$keep" "$src_abs/" "$dest_abs/"
echo "Done. Files from before the overwrite are in $keep"A few notes on why it's written this way:
- It resolves real paths with
pwd -Pbefore comparing. With../or a symlink in the way, plain string comparison can mix up inside and outside. - It stops if the destination's parent doesn't exist. There is deliberately no
mkdir -p; I don't want it creating places I never asked for. - The
casecomparisons add a trailing/. Otherwisesite-oldwould count as "inside"sitejust because the names start the same.
Here is what it did when I tried it. The call that built the destination from an empty variable stopped before writing anything.
$ S=""; bash copy_guard.sh work/src "$S/c" work/out
Stopped: destination /c is outside /…/work/out
$ bash copy_guard.sh work/src work/out/site work/out
new: 1 / overwrite: 1
>f.s....... a.txt
(plan only — set RUN=yes to write)After running it with RUN=yes, the original contents of the overwritten a.txt were sitting in the .before_<date> folder.
I changed how I phrase the request, too
Even with the script in place, a request that just says "sync it" can lead Cowork to carry files some other way. Now I pin down the destination and the order in plain words:
Move the contents of export/ into public/site/.
Only destinations under public/ are allowed.
First run copy_guard.sh without RUN, show me the new/overwrite counts
and the overwrite list, and stop there.
Don't use --delete. I'll tell you to run it after I've checked the list.The important part is that stopping after the list is written into the request. When the count looks wrong — hundreds of overwrites where I expected a handful — that's usually where a wrong destination shows up.
What this script can't protect
It isn't a cure-all, so let me be clear about its edges. The analysis in issue #113960 also raised the possibility that a Windows program received a path like /c/Users/… unconverted and wrote under a different drive. How programs outside the shell interpret paths is beyond what these comparisons catch.
It also only covers copies that go through copy_guard.sh. It does nothing when Cowork writes files some other way. That's why, as an indie developer whose source files and exports all pile up on the same machine, I treat the wording of the request as just as important as the script.
Next time you ask Cowork to copy or sync something, try asking one thing first: "If the destination were empty, where would this write?" If the answer hesitates, I'd say that script doesn't need to run yet.