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

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

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

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
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 /backupKeep the source path under the destination
cp --reflink=always disk.img clone.imgFail unless the filesystem can clone the data

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 --update=none to protect existing files
Attributes not preservedUse -a or -p to preserve ownership and timestamps
Copy owned by root after sudoAdd -p, or set the owner with install -o
failed to clone ...: Operation not supportedThe 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.

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