Menu
Coddy logo textTech

Copy A File

Part of the Fundamentals section of Coddy's Terminal journey — lesson 12 of 82.

The cp command stands for copy. It lets you create a copy of a file in a new location or with a new name.

The basic format is:

cp source destination

For example, to copy readme.txt and name the copy backup.txt:

cp readme.txt backup.txt

The original file stays in place and a new copy is created.

Copy a file into a folder:

You can copy a file directly into another directory:

cp readme.txt documents/readme.txt

Or simply pass the folder as the destination and the filename will be kept:

cp readme.txt documents/

Copy a folder and all its contents:

To copy an entire directory, use the -r flag (recursive):

cp -r documents backup_documents

Note: cp will overwrite the destination file if it already exists.

Use the -i flag to get a confirmation prompt before overwriting:

cp -i readme.txt backup.txt
challenge icon

Challenge

Beginner

Copy readme.txt to a new file called backup.txt, then use ls to confirm both files exist.

Hint: The format is cp source destination.

Cheat sheet

The cp command copies files or directories.

Basic syntax:

cp source destination

Copy a file with a new name:

cp readme.txt backup.txt

Copy a file into a directory:

cp readme.txt documents/

Copy a directory recursively:

cp -r documents backup_documents

Prompt before overwriting:

cp -i readme.txt backup.txt

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals