File sync

2 snippets across 2 stacks - Bash & Linux, Docker

Also written as sync files

SHBash & Linux

Efficient File Synchronization

SH · Networking
Syntax
rsync [options] source destination
Example
rsync -avz --progress ./build/ deploy@web:/var/www/app/
rsync -avz --delete --exclude='.git' src/ backup/src/
rsync -avz -e 'ssh -p 2222' data/ user@host:/data/

Note -a is archive mode (preserves permissions, timestamps, symlinks). -v is verbose. -z compresses during transfer. --delete removes files from the destination that no longer exist in the source (be careful). Trailing slash on source matters: dir/ syncs contents, dir syncs the directory itself.

DKDocker

Compose Watch (Hot Reload)

DK · Docker Compose
Syntax
docker compose watch
# or in compose.yaml:
services:
  api:
    develop:
      watch:
        - action: sync
          path: ./src
          target: /app/src
Example
services:
  api:
    build: .
    develop:
      watch:
        - action: sync
          path: ./src
          target: /app/src
        - action: rebuild
          path: package.json
# Then: docker compose watch

Note sync copies changed files into the container without rebuilding. rebuild triggers a full image rebuild when the specified file changes (e.g., when you add a dependency). This replaces many custom file-watching setups.

Frequently asked questions

How does Bash & Linux handle file sync?
This task is covered in 2 stacks on this page: Bash & Linux, Docker. The "Efficient File Synchronization" snippet in Bash & Linux uses `rsync [options] source destination`.
Which command does the Bash & Linux example use?
The "Efficient File Synchronization" snippet uses `rsync [options] source destination`, from the Networking section of the Bash & Linux cheat sheet.
Which stacks cover "file sync" on this page?
Bash & Linux, Docker. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Efficient File Synchronization": -a is archive mode (preserves permissions, timestamps, symlinks). -v is verbose. -z compresses during transfer. --delete removes files from the destination that no longer exist in the source (be careful). Trailing slash on source matters: dir/ syncs contents, dir syncs the directory itself.