Skip to main content

cp: Copying Files

cp (copy) duplicates a file or directory. The original stays intact; you get a new copy at the destination.

Copying a file

cp notes.txt backup.txt

Both notes.txt and backup.txt now exist with identical contents.

Copying to a directory

If the destination is an existing directory, cp places the file inside it:

cp notes.txt documents/

This creates documents/notes.txt. The original /home/user/notes.txt still exists.

Copying multiple files into a directory

cp file1.txt file2.txt target-directory/

Copying a directory recursively

To copy an entire directory and its contents, you need the -r (recursive) flag:

cp -r documents documents-backup

Without -r, cp refuses to copy a directory.

Common mistakes

MistakeWhat happens
cp notes.txt notes.txtCopies a file onto itself — usually a no-op or error
cp dir/ file.txtError: you need -r to copy a directory
Missing the destinationError: missing destination operand

Practice

Copy notes.txt to a new file called backup.txt.

Loading terminal…
Donate to this project