Copying a file from a terminal means choosing a source path and a destination, which can have a different name. The cp command copies files and, with a recursive option, copies directories and their contents.
In this piece, I’ll walk through copying files and directories with cp, then explain how to choose an overwrite option.
TL;DR
Use cp SOURCE DESTINATION to copy a file, and add -R when the source is a directory.
- Copy or rename one file: cp report.txt archive/report-old.txt.
- Copy several files into a directory by listing each source before the destination.
- Use -i to confirm before overwriting, or -a when preserving directory attributes matters.
What Is the cp Command?
cp copies files from a source path to a destination path. Add a recursive option when the source is a directory.
The command form is cp SOURCE DEST. With one source, DEST can be a filename or an existing directory. With several sources, DEST must be a directory.
Unlike mv, cp leaves each source in place. Paths can be relative to the current working directory or absolute.
A trailing slash makes a destination directory clear, but does not create a missing parent. Create that directory separately with mkdir.
cp [options] source... destination
How to Copy Files and Directories with cp
Choose the source path first, then decide whether the destination is a renamed file or a directory that receives the original filename. Add one option only when the task calls for it, which keeps the command’s effect clear.
Step 1: Copy or Rename One File
Pass the source and destination filenames to copy a file into a new file. The destination name can differ, which makes this the standard way to create a renamed copy.
cp -v report.txt archive/report-old.txt
I ran cp -v report.txt archive/report-old.txt. It returned this source-to-destination mapping:
'report.txt' -> 'archive/report-old.txt'

The output names the source and destination, while the screenshot shows the same command in the terminal. To copy the file into another directory while keeping its name, use that directory as the destination, for example cp notes.txt ~/archive/. Check the target path before running the command.
Step 2: Copy Several Files or Matching Names
The shell expands a wildcard such as *.txt into matching filenames before cp receives the arguments. Quote the wildcard only when you want to pass the literal asterisk. A wildcard is evaluated before cp runs, so an unmatched name can produce a shell-specific result.
cp chapter1.txt chapter2.txt ~/book-drafts/
cp *.log ~/log-archive/
Review the matches with ls first if the wildcard could select unintended files. For exclusions, use the site’s file-extension filtering guide.
Step 3: Copy a Directory Recursively
Directories need recursive copying because cp otherwise handles files, not the directory tree beneath them. Use -R to copy the directory and its contents into the destination.
cp -R project/ ~/backups/
This creates a project directory under ~/backups/. If you instead name a new destination path such as ~/backups/project-copy, that becomes the copied tree’s name. Inspect the resulting paths with find when a nested directory structure matters.
Step 4: Preserve Attributes When Needed
Use -p to ask cp to preserve mode, ownership and timestamps where the system permits. For a directory tree, -a selects archive behavior, including recursion and preservation of a broader set of attributes and symbolic links.
cp -p config.ini config.ini.bak
cp -a project/ ~/backups/
For copies between locations with different permissions, inspect the resulting owner and access settings with ls -l. The related chmod and chown guide explains Linux access settings.
What Should You Check Before Copying?
Check whether the destination exists, whether overwriting is acceptable and whether the source includes symbolic links. These choices matter more than adding a long list of options to every cp command.
| Situation | Option or check | Effect |
|---|---|---|
| Ask before replacing a file | -i | Prompts before an overwrite |
| Skip existing destination files | -n | Does not overwrite existing files in GNU cp |
| Keep a backup of replaced files | -b | Creates a backup before replacement |
| Copy directory contents and attributes | -a | Uses archive-style recursive copying |
Choose one overwrite policy. Interactive mode suits an uncertain destination. GNU documentation notes that option precedence can matter when overwrite flags are combined, so avoid conflicting choices.
A symbolic link is a path that refers to another path. GNU cp generally follows a source link for a single-file copy, while recursive and archive copies use different link handling. Use -P to copy the link itself or -L to follow it, and see creating symbolic links in Linux for details.
cp has no built-in undo command. An overwrite replaces the destination, so use -i or -b and keep a backup when the original matters. For a reported error, compare source and destination paths with diff for text files.
A normal cp command has no general progress display. For a long transfer, choose a tool such as rsync and check its options and destination behavior first. Verify that the expected destination exists and has the right content.
ls -l source.txt destination.txt
cmp source.txt destination.txt
Conclusion: Choose the Destination Carefully
For valuable files, choose the destination deliberately and select an overwrite policy before copying. The GNU Coreutils cp manual documents additional options.
FAQ
How do I copy a folder in Linux?
Use cp -R source-directory destination-directory to copy a directory tree. Use cp -a when archive-style attribute and symbolic-link preservation is also needed.
Does cp overwrite files by default?
Yes. cp normally replaces an existing destination file. Use -i to prompt before replacement or -n with GNU cp to skip existing files.
Can I undo a cp command?
cp has no built-in undo. If the command overwrote a file, restore it from a backup or another copy. Options such as -i and -b can reduce that risk before copying.
