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