Back to Blog
Arnošt Havelka

Copying Files and Directories

Master cp to copy files and recursive directory structures.

Start Interactive Lesson
Copying Files and Directories

The cp command copies files and directories. It's essential for backing up and duplicating your work.

Copy a Single File

Use cp source destination to copy a file.

Try the command

Copy a file!

Build the command
cpsourcedestination
cpCopy file - duplicates a file to a new location or name.
Terminal
C:\Users\User>cp source destination

Duplicate a file:

Command Prompt
original.txt
C:\Users\User>ls

Recursive Copy: cp -r

The -r flag copies entire directories with all their contents.

Try the command

Copy entire directories!

Build the command
cp-rsource_dirdest_dir
-rRecursive - copies directories and all files within them.
Terminal
C:\Users\User>cp -r source_dir dest_dir

Duplicate folder structures:

Command Prompt
project/
C:\Users\User>ls

Copy to Directory

Copy a file into an existing directory by using the directory as the destination.

Try the command

Copy file into a folder!

Build the command
cpfile.txtdirectory/
directory/Target directory - keeps original filename inside the directory.
Terminal
C:\Users\User>cp file.txt directory/

Place file in folder:

Command Prompt
file.txt Archive/ ls Archive/
C:\Users\User>ls

Knowledge Check

1 / 3

What does cp do?

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

Moving and Renaming Files

Use mv to move files between directories and rename them.