cp
Files and directories
Usage
cp -r [source] [dest]Copies files or whole trees; whether the destination already exists decides if the source becomes that name or lands inside it.
Common flags
| Flag | Meaning |
|---|---|
| -r | Copy directories and their contents |
| -a | Archive: -r plus permissions, times and symlinks kept |
| -p | Keep mode, owner and timestamps |
| -i | Ask before overwriting |
| -n | Never overwrite a file that already exists |
| -v | Name each file as it is copied |
| -u | GNU: copy only when the source is newer |
Examples
cp -a site/ backup/Tree copied with permissions and times intact.
cp config.yml config.yml.bakQuick backup beside the original.
cp -r src/. dest/Copies the contents of src into an existing dest.
Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does cp do?
Copies files or whole trees; whether the destination already exists decides if the source becomes that name or lands inside it.
Q. How do I type it?
cp -r [source] [dest] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
7 are listed here; the full set is in man cp. This command sits under Files and directories.
Related commands
man page: man cp