Skip to content

Moving Files

Use core.move and core.copy to restructure files during synchronization.

Move or rename files and directories:

core.move("source_path", "destination_path")
# Move entire directory
core.move("src/", "lib/")
# src/foo.py → lib/foo.py
# src/bar/baz.py → lib/bar/baz.py
# Flatten a subdirectory to root
core.move("project/src/", "")
# project/src/main.py → main.py
# project/src/lib/util.py → lib/util.py
# Rename a single file
core.move("README.internal.md", "README.md")
# Only move matching files
core.move("src/", "lib/", paths = glob(["**/*.py"]))
# src/main.py → lib/main.py
# src/config.json → stays at src/config.json

Copy files (keeping originals):

core.copy("source", "destination")
# Copy a file
core.copy("LICENSE", "docs/LICENSE")
# Copy directory
core.copy("templates/", "examples/")

By default, moves fail if the destination exists:

# Fail if destination exists
core.move("src/", "lib/")
# Overwrite if destination exists
core.move("src/", "lib/", overwrite = True)
# packages/my-lib/src/* → src/*
core.move("packages/my-lib/", "")
transformations = [
# Move source to lib
core.move("src/", "lib/"),
# Move internal docs to public
core.move("docs/public/", "docs/"),
# Remove internal docs
core.remove(glob(["docs/internal/**"])),
]
transformations = [
core.move("README.internal.md", "README.md"),
core.move("CHANGELOG.internal.md", "CHANGELOG.md"),
]
# Only move if file matches pattern
core.move(
"config/",
"settings/",
paths = glob(["**/*.yaml", "**/*.yml"]),
)

When combining moves, order matters:

# CORRECT: nested moves from deepest to shallowest
transformations = [
core.move("a/b/c/", "x/y/z/"),
core.move("a/b/", "x/y/"),
core.move("a/", "x/"),
]
# INCORRECT: parent moved first, children paths no longer exist
transformations = [
core.move("a/", "x/"), # a/b/c/ is now x/b/c/
core.move("a/b/", "x/y/"), # ERROR: a/b/ doesn't exist
]

Trailing slashes indicate directories:

# Move directory contents
core.move("src/", "lib/")
# src/a.py → lib/a.py
# Move directory itself (rename)
core.move("src", "lib")
# src → lib (same effect for directories)
# Move file
core.move("src/main.py", "lib/app.py")
# src/main.py → lib/app.py

Test with folder destination:

Terminal window
java -jar copybara.jar migrate copy.bara.sky test \
--folder-origin /path/to/source \
--folder-destination /tmp/output
# Check the structure
find /tmp/output -type f