Copy files and directories.
The cp command copies files and directories from a source to a destination. By default, it copies a single file to a new location or overwrites an existing file.
When copying directories, the -r (recursive) option is required. cp preserves the file structure within the source directory and recreates it at the destination.
cp supports options for preserving file attributes, creating backups, interactive prompting before overwriting, and following or not following symbolic links.
| Name | Description | Optional |
|---|---|---|
source |
The file or directory to copy. | No |
dest |
The destination file or directory. | No |
-r |
Copy directories recursively. | Yes |
-i |
Prompt before overwriting an existing file. | Yes |
-p |
Preserve file mode, ownership, and timestamps. | Yes |
cp file.txt backup.txt
cp -r src/ dest/
cp -i important.conf /etc/