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
mv Command Reference for CI Scriptsmv moves or renames files in CI for atomic artifact swaps. Reference for -f, -n, -t, and cross-filesystem beh…
rsync Command Reference for CI Scriptsrsync synchronizes files efficiently in CI deploys and cache restores. Reference for -a, -z, --delete, and th…
mkdir -p Command Reference for CI Scriptsmkdir -p creates directory trees idempotently in CI. Reference for -p, -m, and parent creation, plus why -p n…
chmod Command Reference for CI Scriptschmod changes file permissions in CI. Reference for +x, octal modes, -R, and symbolic modes, plus the 600-on-…