Copy files or directories from source to destination.
medium risk
cp copies files and directories. Without -r it refuses to copy directories. -p preserves timestamps and permissions — critical for backups. -u only copies when source is newer. The -i flag adds interactive prompts before overwriting.
| Flag | Meaning |
|---|---|
-r --recursive | Copy directories and all their contents recursively |
-p --preserve | Preserve timestamps, permissions, and ownership |
-i --interactive | Prompt before overwriting existing files |
-u --update | Copy only when source is newer than destination |
-v --verbose | Print each file as it is copied |
-a --archive | Same as -dR --preserve=all — full archive copy |
cp -rp /etc/nginx /etc/nginx.bakBackup nginx config recursively with preserved permissions · explain & risk-check →cp -i config.yml /etc/app/Copy config with confirmation prompt if destination exists · explain & risk-check →cp -av /src/* /dst/Archive-copy all files verbosely showing each transfer · explain & risk-check →Prefer `rsync -a` over `cp -r` for large directories — rsync is resumable, shows progress, and handles edge cases better.
Reference text is hand-written and may be incomplete — check man cp for your version. Test destructive commands on a non-production system first.