cp

Overview

The cp (copy) command copies files and directories. It can preserve file attributes, handle recursive copying, and create backups.

Syntax

cp [options] source... destination

Common Options

Option Description
-r, -R Copy directories recursively
-i Interactive (prompt before overwrite)
-f Force copy (no prompting)
-p Preserve attributes
-a Archive mode (same as -dR –preserve=all)
-u Update (copy only newer files)
-v Verbose mode
-n No overwrite
-l Create hard links
-s Create symbolic links

Key Use Cases

  1. Copy files
  2. Copy directories
  3. Backup files
  4. Preserve attributes
  5. Create links

Examples with Explanations

Example 1: Basic File Copy

cp file1 file2

Copy file1 to file2

Example 2: Recursive Directory Copy

cp -r dir1 dir2

Copy directory dir1 and contents to dir2

Example 3: Preserve Attributes

cp -a source dest

Copy with all attributes preserved

Understanding Output

  • No output by default
  • With -v:
    • ‘file1 -> file2’ format
  • Error messages for:
    • Permission denied
    • No space
    • File exists
    • Source not found

Common Usage Patterns

  1. Safe copy (interactive):

    cp -i source dest
  2. Update existing files:

    cp -u source/* dest/
  3. Backup with timestamp:

    cp file{,.bak}

Performance Analysis

  • File size impact
  • Disk I/O considerations
  • Network transfer (if applicable)
  • Attribute preservation overhead
  • Hard link vs copy trade-offs

Additional Resources