Linux cp Command: Copy Files and Directories

This guide explains how to copy files and directories with Linux cp, control overwrites, preserve attributes, handle symlinks, and verify important copies. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.

The examples use GNU Coreutils behavior. Check cp --help, man cp, and your distribution documentation when portability or a version-specific option matters.

What Is the cp Command?

cp copies files and, with a recursive option, directory trees. A copied regular file is independent of the original and normally has a different inode. The exact destination layout depends on whether the final operand already exists as a directory.

Basic Syntax

cp [OPTION]... [-T] SOURCE DESTINATION
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.

Basic cp Usage

Copy a file to a new name

With two file paths, cp reads the first and writes a copy at the second.

cp report.txt report-backup.txt

Copy several files into a directory

When multiple sources are given, the final operand must be an existing destination directory.

cp index.html style.css public/

Copy a directory tree

Use -R for recursion. Consider -a when links and metadata should be preserved as far as the platform allows.

cp -R src src-copy
cp -a app/ app-backup/

Checking if the copied file is the same

cp usually does not output anything even if it succeeds. For important copy operations, check not only the existence of the target file, but also its contents.

printf 'demo\n' > original.txt
cp original.txt copy.txt
cmp -s original.txt copy.txt
echo $?

The example output is 0, which means the byte contents of the two files are the same. However, cmp does not verify metadata such as ownership, permissions, or extended attributes, so check stat as well if necessary.

Key Options

Option Purpose
-R, -r Copy directories recursively.
-a Archive mode: recurse and preserve links and attributes where possible.
-p Preserve mode, ownership, and timestamps where possible.
-i Prompt before overwriting an existing destination.
-n Do not overwrite existing destination files.
-u Copy according to update conditions such as a newer source.
-T Treat the destination as an exact path, not a directory.
-t DIR Specify the target directory before the source list.
--reflink[=WHEN] Request copy-on-write cloning on supported filesystems.

Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.

Practical Examples

Control overwriting

Use -i for an interactive decision. Use -n when automation must keep existing files, and verify the outcome afterward.

cp -iv config.ini backup/
cp -nv *.conf backup/

Preserve attributes for a backup copy

Archive mode attempts to preserve symlinks, permissions, timestamps, and other supported attributes. ACLs, xattrs, SELinux labels, and ownership still depend on privileges and filesystem support.

sudo cp -a /etc/myapp/ /backup/myapp/
cp -a --preserve=all source/ destination/

Verify important copies

For critical files, compare hashes rather than only names or sizes. A live, changing dataset may require a snapshot or an application-aware backup method.

sha256sum source.img copied.img
diff -qr source-dir copied-dir

cp and Related Commands

Command or method Primary role Best fit
cp Local file and directory copying Straightforward copies
mv Move or rename paths The source should not remain
rsync Incremental and remote synchronization Large repeated or network transfers
install Copy while setting attributes Program installation and deployment

Important Cautions

The destination type changes the result

If the final operand exists as a directory, the source is placed inside it. Use -T or -t when a script should make that intent explicit.

Archive mode is not a complete backup system

Open files, live databases, permissions, and unsupported filesystem features can prevent a consistent copy. Test restoration with an appropriate backup workflow.

Shell globs can omit hidden entries

A pattern such as source/* normally excludes dotfiles. Review path syntax and inclusion rules before copying a directory's contents.

FAQ

Should I use -R or -a for a directory?

Use -R for basic recursion and -a when preserving links and supported metadata matters.

How do I prevent overwriting existing files?

GNU cp provides -n. Confirm its exact behavior on the installed version.

Why does a copied directory appear at a different depth than expected?

The result depends on whether the destination exists and whether it is treated as a directory. Use -T or -t to remove ambiguity.

How do I verify a copy?

Compare hashes for files and use tools such as diff -qr for directory trees. A real backup also needs a restore test.

Related Resources

More in This Category
Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Learn how to remove or count adjacent duplicate lines with the Linux uniq command, including practical examples, key options, and important precautions.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux stat Command: Display Detailed File Metadata

Linux stat Command: Display Detailed File Metadata

Learn how to use the Linux stat command to inspect file size, permissions, ownership, timestamps, inode, and filesystem metadata, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux comm Command: Compare Two Sorted Files

Linux comm Command: Compare Two Sorted Files

Learn how to compare Two Sorted Files with the Linux comm command, including practical examples, key options, and important precautions.

Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux cat Command: Display and Concatenate File Contents

Linux cat Command: Display and Concatenate File Contents

Learn how to use the Linux cat command to display text files, concatenate multiple files, number lines, reveal hidden characters, and avoid unsafe redirection mistakes.

Linux fsck Command: Check and Repair Filesystems

Linux fsck Command: Check and Repair Filesystems

Learn how to check and Repair Filesystems with the Linux fsck command, including practical examples, key options, and important precautions.

Linux timedatectl Command: Check Time Zone and Time Synchronization

Linux timedatectl Command: Check Time Zone and Time Synchronization

Learn how to check Time Zone and Time Synchronization with the Linux timedatectl command, including practical examples, key options, and important precautions.

Linux realpath Command: Resolve Absolute and Canonical Paths

Linux realpath Command: Resolve Absolute and Canonical Paths

Learn how to use the Linux realpath command to resolve relative paths, symbolic links, and redundant path components, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux head Command: Display the Beginning of a File

Linux head Command: Display the Beginning of a File

Learn how Linux head prints the first lines or bytes of files and pipelines, labels multiple inputs, excludes a trailing portion, and handles multibyte data.