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 -r dir/. /dest/ | Copy directory contents only (not the directory itself) |
Overwrite Behavior
Control what happens when the destination already exists.
| Command | Description |
|---|---|
cp -i file.txt /dest/ | Prompt before overwrite |
cp -n file.txt /dest/ | Never overwrite existing file |
cp -f file.txt /dest/ | Force overwrite without prompt |
cp -u file.txt /dest/ | Copy only if source is newer than destination |
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 --preserve=timestamps file.txt /dest/ | Preserve only timestamps |
cp --preserve=mode file.txt /dest/ | Preserve only permissions |
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 |
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 -n to protect existing files |
| Attributes not preserved | Use -a or -p to preserve ownership and timestamps |
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 |