Skip to main content

cp Cheatsheet

By Dejan Panovski Updated on Download PDF

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.

CommandDescription
cp [OPTIONS] SOURCE DESTCopy one file to destination
cp [OPTIONS] SOURCE... DIRECTORYCopy multiple sources into a directory
cp -r [OPTIONS] SOURCE DESTCopy directory recursively
cp -- FILE DESTCopy file whose name starts with -

Copy Files

Common file copy commands.

CommandDescription
cp file.txt /tmp/Copy file to another directory
cp file.txt newname.txtCopy 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.txtCopy and rename to another directory

Copy Directories

Copy entire directory trees.

CommandDescription
cp -r dir/ /dest/Copy directory recursively
cp -r dir1 dir2 /dest/Copy multiple directories
cp -r /src/dir /dest/dir-newCopy 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.

CommandDescription
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.

CommandDescription
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.

CommandDescription
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.

IssueCheck
omitting directoryAdd -r to copy directories
Permission deniedCheck source read permission and destination write permission
No such file or directoryVerify source path with ls -l source
Destination file overwrittenUse -i or -n to protect existing files
Attributes not preservedUse -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.

Use these guides for detailed copy workflows.

GuideDescription
Linux cp Command: Copy Files and DirectoriesFull cp guide with examples
How to Copy Files and Directories in LinuxOverview of all copy tools
mv CheatsheetMove and rename files
rsync CheatsheetRemote and local sync with progress