Linux for Developers Course
Linux for Developers
/
Beginner

cp

Definition

Copy. The command used to copy files or directories from a source location to a destination location.

Explain Like I'm New

Copy and Paste. You duplicate a file, leaving the original perfectly intact.

Real World Example

Before editing a dangerous system file, you make a backup by typing `cp config.txt config.txt.backup`. Now you have two identical files.

Common Use Cases

  • •Backing up files
  • •Duplicating templates

Crucial Command Flags

Flag / OptionDescription
-r / -RRecursive. Copy directories and their contents.
-iInteractive. Prompt before overwrite.
-vVerbose. Explain what is being done.
-pPreserve. Keep original file attributes (time, ownership).

Interview Questions

basic

  • What happens if the destination file you are copying to already exists?

intermediate

  • Why does `cp folder1 folder2` throw an 'omitting directory' error?

Flash Cards

Question

Destination exists?

Click to reveal answer
Answer

By default, `cp` will silently overwrite the destination file, destroying whatever was previously there.

Question

Why error on folders?

Click to reveal answer
Answer

Because `cp` only copies single files by default. To copy an entire directory and all the files inside it, you must use the Recursive flag: `cp -r folder1 folder2`.