cp -r Command

The cp -r command copies directories recursively, including all subdirectories and files within them. The -r flag stands for "recursive".

Syntax

cp -r SOURCE_DIRECTORY DESTINATION_DIRECTORY

Description

The -r (or -R) option tells cp to copy directories recursively. Without this option, cp cannot copy directories - it will give an error when you try to copy a directory.

What gets copied:

  • The directory itself
  • All files in the directory
  • All subdirectories
  • All files in subdirectories (recursively)
  • File permissions and timestamps (depending on other options)

Examples

Copy directory to new location

cp -r /home/user/Documents /backup/Documents

Copies the entire Documents directory to /backup/

Copy directory with new name

cp -r project_old project_backup

Creates a copy of project_old named project_backup

Copy multiple directories

cp -r dir1 dir2 dir3 /destination/

Copies multiple directories to destination folder

Verbose recursive copy

cp -rv source_dir destination_dir

Shows each file being copied during the recursive operation

Interactive recursive copy

cp -ri source_dir destination_dir

Prompts before overwriting any existing files

Preserve attributes during recursive copy

cp -rp source_dir destination_dir

Preserves file permissions, ownership, and timestamps

⚠️ Important Notes

  • Always use -r when copying directories
  • Be careful with destination paths to avoid overwriting
  • Large directories may take significant time to copy
  • Ensure sufficient disk space at destination
  • Use -i flag to prompt before overwriting files

See also