Linux cp Command: Copy Files and Directories

cp is a command-line utility for copying files and directories on Linux and Unix systems. It is one of the most frequently used commands when working in the terminal.
This guide explains how to use the cp command with practical examples. For an overview of all tools available for copying files, see How to Copy Files and Directories in Linux
.
Syntax
The general syntax for the cp command is:
cp [OPTIONS] SOURCE... DESTINATIONSOURCE can be one or more files or directories. DESTINATION is a single file or directory. The behavior depends on what you pass:
- When
SOURCEandDESTINATIONare both file paths,cpcopies the source file to the destination. If the destination file does not exist, it is created. - When
SOURCEcontains multiple arguments,DESTINATIONmust be a directory. Each source file or directory is copied into it. - When
SOURCEis a directory, use the-Roption to copy it recursively.
To copy files and directories you must have at least read permission on the source and write permission on the destination directory. To learn more about file permissions, see Understanding Linux File Permissions .
Copy a File
To copy a file in the current working directory , specify the source file and the destination file name:
cp file.txt file_backup.txtBash brace expansion offers a shorter form for the same operation:
cp file.txt{,_backup}Both commands produce file_backup.txt as a copy of file.txt.
To copy a file to another directory, provide the destination directory path. When only a directory is given, the copied file keeps its original name:
cp file.txt /backupTo copy the file to a different directory under a new name, include the file name in the destination path:
cp file.txt /backup/new_file.txtOverwrite Behavior
By default, cp overwrites the destination file if it already exists. Use the -n (--no-clobber) option to skip existing files:
cp -n file.txt file_backup.txtTo prompt for confirmation before overwriting, use the -i (--interactive) option:
cp -i file.txt file_backup.txtcp: overwrite 'file_backup.txt'?To copy only when the source is newer than the destination, use -u (--update):
cp -u file.txt file_backup.txtPreserve File Attributes
When cp creates a new file, it is owned by the user running the command with a timestamp of the current time. Use the -p (--preserve) option to preserve the file mode, ownership
, and timestamps
from the source:
cp -p file.txt file_backup.txtVerbose Output
Use the -v (--verbose) option to print each file as it is copied:
cp -v file.txt file_backup.txt'file.txt' -> 'file_backup.txt'Copy a Directory
To copy a directory and all its contents recursively, use the -R or -r option:
cp -R Pictures Pictures_backupIf Pictures_backup does not exist, cp creates it and copies the contents of Pictures into it. If Pictures_backup already exists, Pictures itself is copied inside it, resulting in Pictures_backup/Pictures/.
To copy only the contents of a directory without the directory itself, use the -T option. This treats the destination as a regular target rather than a container:
cp -RT Pictures Pictures_backupYou can also use a wildcard to copy directory contents, though note that this approach does not copy hidden files and directories (those starting with .):
cp -R Pictures/* Pictures_backup/All file-copy options such as -p, -i, -u, and -v work the same way when copying directories. The -R flag is always required for directory copies.
Copy Multiple Files
To copy multiple files into a directory, list the sources followed by the destination directory as the last argument:
cp file.txt file1.txt dir/ /backupWhen copying multiple sources, the destination must be a directory.
Quick Reference
| Task | Command |
|---|---|
| Copy a file | cp source.txt dest.txt |
| Copy to a directory | cp file.txt /backup/ |
| Copy without overwriting | cp -n source.txt dest.txt |
| Prompt before overwriting | cp -i source.txt dest.txt |
| Copy only if source is newer | cp -u source.txt dest.txt |
| Preserve permissions and timestamps | cp -p source.txt dest.txt |
| Verbose output | cp -v source.txt dest.txt |
| Copy directory recursively | cp -R sourcedir/ destdir/ |
| Copy directory contents only | cp -RT sourcedir/ destdir/ |
| Copy multiple files to directory | cp file1 file2 dir/ /dest/ |
Troubleshooting
cp: cannot stat 'source': No such file or directory
The source file or directory does not exist. Check the path for typos and confirm the file is present with ls -l source.
cp: omitting directory 'dirname'
You tried to copy a directory without the -R option. Add -R to copy recursively: cp -R sourcedir/ destdir/.
cp: cannot create regular file 'dest': Permission denied
You do not have write permission on the destination directory. Check permissions with ls -l and use sudo
if required, or change ownership with chown
.
cp: cannot open 'source' for reading: Permission denied
You do not have read permission on the source file. See Understanding Linux File Permissions
for how to inspect and adjust permissions.
FAQ
How do I copy a directory and all its contents?
Use the -R (recursive) option: cp -R sourcedir/ destdir/. Without -R, cp will refuse to copy a directory and print a warning.
How do I copy files without overwriting existing ones?
Use the -n (--no-clobber) option: cp -n source.txt dest.txt. Files that already exist at the destination are silently skipped.
How do I preserve file permissions and timestamps when copying?
Use the -p option: cp -p source.txt dest.txt. This preserves the file mode, ownership, and timestamps from the original file.
What is the difference between cp and rsync?cp is the standard tool for local copies. rsync
is better for large transfers or keeping directories in sync — it skips files that have not changed, supports remote transfers over SSH, and can resume interrupted operations.
How do I copy hidden files with a wildcard?
The glob * does not match files starting with .. To include hidden files, use cp -R sourcedir/. destdir/ (note the trailing /.), or list them explicitly.
What is the difference between cp and mv?cp creates a copy while leaving the original in place. mv
moves the file, removing it from the source location.
Conclusion
The cp command is the standard way to copy files and directories in Linux. Use -R for directories, -p to preserve attributes, and -n or -i to control overwrite behavior.
For copying files over a network, use the rsync
or scp
utilities.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page