Skip to content
Latchkey

cp Command Reference for CI Scripts

cp copies files and, with -r, whole directory trees.

cp stages build outputs and config into place. Its quirks are around recursion, preserving attributes, and how a trailing slash and an existing destination change the result.

Common flags/usage

  • -r / -R: copy directories recursively
  • -p: preserve mode, ownership, and timestamps
  • -a / --archive: recursive plus preserve everything (incl. symlinks)
  • -n / -f: never overwrite / force overwrite
  • -v: verbose, list each file copied

Example

shell
mkdir -p staging
cp -a dist/. staging/                # copy contents, preserve attrs
cp -p "${CONFIG}" "/etc/app/${ENV}.conf"

In CI

Whether the destination exists and whether the source ends in /. changes whether you copy the directory or its contents; cp -a src/. dst/ copies contents reliably. Use -a (not plain -r) when symlinks and permissions matter, e.g. preserving an execute bit on a binary. cp does not create missing parent dirs, so mkdir -p first.

Key takeaways

  • cp -r copies directories; cp -a also preserves symlinks and permissions.
  • cp src/. dst/ copies contents; the trailing /. avoids nesting surprises.
  • cp will not create parent directories, so mkdir -p the destination first.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →