cp Cheatsheet
Quick reference for copying files and directories with cp in Linux
The `cp` command copies files and directories in Linux. This cheatsheet covers copy syntax, recursive copy, overwrite control, attribute preservation, and practical patterns.
Basic Syntax
Core command forms for copy operations.
| Command | Description |
|---|---|
cp [OPTIONS] SOURCE DEST | Copy one file to destination |
cp [OPTIONS] SOURCE... DIRECTORY | Copy multiple sources into a directory |
cp -r [OPTIONS] SOURCE DEST | Copy directory recursively |
cp -- FILE DEST | Copy file whose name starts with - |
Copy Files
Common file copy commands.
| Command | Description |
|---|---|
cp file.txt /tmp/ | Copy file to another directory |
cp file.txt newname.txt | Copy and rename in same directory |
cp file1 file2 /backup/ | Copy multiple files to a directory |
cp *.log /var/log/archive/ | Copy files matching pattern |
cp /src/file.txt /dest/newname.txt | Copy and rename to another directory |
Copy Directories
Copy entire directory trees.
| Command | Description |
|---|---|
cp -r dir/ /dest/ | Copy directory recursively |
cp -r dir1 dir2 /dest/ | Copy multiple directories |
cp -r /src/dir /dest/dir-new | Copy and rename directory |
cp -a dir/. /dest/ | Copy directory contents into an existing directory |
cp -RT dir/ /dest/ | Treat the destination as a path, not a container |
Overwrite Behavior
Control what happens when the destination already exists.
| Command | Description |
|---|---|
cp -i file.txt /dest/ | Prompt before overwrite |
cp --update=none file.txt /dest/ | Skip existing files without an error |
cp --update=none-fail file.txt /dest/ | Skip existing files and exit with a failure |
cp -n file.txt /dest/ | Deprecated spelling of --update=none |
cp -u file.txt /dest/ | Copy only if source is newer than destination |
cp -f file.txt /dest/ | Remove the destination and retry when it cannot be opened |
Note: cp overwrites the destination by default, so -f is not needed for a normal overwrite. --update=UPDATE needs GNU coreutils 9.3 or later, and none-fail needs 9.5 or later.
Preserve Attributes
Keep timestamps, ownership, and permissions when copying.
| Command | Description |
|---|---|
cp -p file.txt /dest/ | Preserve mode, ownership, and timestamps |
cp -a dir/ /dest/ | Archive mode: preserve all attributes, copy recursively |
cp -pr dir/ /dest/ | Recursive copy preserving mode, ownership, and timestamps |
cp --preserve=timestamps file.txt /dest/ | Preserve only timestamps |
cp --preserve=mode file.txt /dest/ | Preserve only permissions |
cp -d file.txt /dest/ | Copy links as links and keep hard links |
cp -RL dir/ /dest/ | Resolve symbolic links into real files |
Useful Patterns
Common real-world cp command combinations.
| Command | Description |
|---|---|
cp file.txt{,.bak} | Quick backup via brace expansion |
cp -v file.txt /dest/ | Verbose output |
cp -rv dir/ /dest/ | Verbose recursive copy output |
find . -name '*.conf' -exec cp -t /backup/ {} + | Copy matched files with find |
cp -a /src/. /dest/ | Mirror directory preserving all attributes |
cp -b file.txt /dest/ | Back up the destination before overwriting |
cp --backup=numbered file.txt /dest/ | Keep numbered backups of the destination |
cp --parents /etc/app/app.conf /backup | Keep the source path under the destination |
cp --reflink=always disk.img clone.img | Fail unless the filesystem can clone the data |
Troubleshooting
Quick checks for common copy errors.
| Issue | Check |
|---|---|
omitting directory | Add -r to copy directories |
Permission denied | Check source read permission and destination write permission |
No such file or directory | Verify source path with ls -l source |
| Destination file overwritten | Use -i or --update=none to protect existing files |
| Attributes not preserved | Use -a or -p to preserve ownership and timestamps |
Copy owned by root after sudo | Add -p, or set the owner with install -o |
failed to clone ...: Operation not supported | The filesystem has no CoW support; drop --reflink=always |
Note: cp -t in the find example is GNU-specific and may not be available on non-GNU systems.
Related Guides
Use these guides for detailed copy workflows.
| Guide | Description |
|---|---|
Linux cp Command: Copy Files and Directories | Full cp guide with examples |
How to Copy Files and Directories in Linux | Overview of all copy tools |
mv Cheatsheet | Move and rename files |
rsync Cheatsheet | Remote and local sync with progress |