Command
cpExplanation
The `cp` (copy) command duplicates files or directories. The source and destination can be absolute or relative paths. The `-r` or `--recursive` flag allows copying directories. The `-p` flag preserves file attributes like timestamps and permissions. Use `-v` to display verbose output during copying.
Common Use Cases
- •Backup files or directories
- •Duplicate configuration files before editing
- •Copy project folders to a new environment
Best Practices
- ✓Use `-i` to prompt before overwriting existing files
- ✓Use `-v` for verbose output during copy operations
- ✓Combine with `-p` to preserve timestamps and permissions
Common Mistakes to Avoid
- ⚠Forgetting `-r` when copying directories
- ⚠Overwriting files unintentionally without confirmation
- ⚠Not preserving file permissions with `-p`
Troubleshooting
Problem: Permission denied when copying files
Solution: Use `sudo cp` if elevated privileges are needed.
Problem: Files not copied recursively
Solution: Add the `-r` flag when copying directories.
Examples
Copy file to directory
cp file.txt backup/Recursively copy directory and its contents
cp -r dir1/ dir2/