keepcalculator

06 Numbers and Powers

Cp Command Examples: Copy Files and Directories

Card No. 06·87241 · Updated · Standard formula, shown below

The cp command copies files and directories in Linux and Unix-like systems. You give it a source and a destination, and it creates a duplicate at the destination, leaving the original untouched. This guide shows the most useful cp examples with explanations of what each option does.

Basic Syntax and Common Options

The basic form is cp [options] source destination. If the destination is an existing directory, the file is copied into it with the same name. If the destination is a filename, the copy is saved under that name.

  • -r or -R — recursive; needed to copy a directory and its contents.
  • -i — interactive; prompts before overwriting an existing file.
  • -u — update; copies only when the source is newer than the destination or the destination is missing.
  • -v — verbose; prints each file as it is copied.
  • -p — preserve; keeps permissions, timestamps, and ownership when possible.
  • -a — archive; equivalent to -r plus most attributes, useful for backups.
  • -n — no-clobber; never overwrites an existing file.

Copying a Single File

The simplest use is copying one file to another name or into a directory.

cp file.txt backup.txt
cp file.txt /home/user/documents/
cp file.txt /home/user/documents/renamed.txt

The first copies file.txt to backup.txt in the current directory. The second places a copy in the documents directory, keeping the name. The third copies and renames it in one step. If the destination directory does not exist, cp returns an error.

Copying Directories with -r

Directories require the recursive flag. Without it, cp fails with cp: -r not specified; omitting directory.

cp -r project/ /backup/project/
cp -r /home/user/photos /home/user/photos_backup

The first copies the entire project directory into /backup, creating /backup/project. The second copies the photos directory to a sibling named photos_backup. Use -r for most everyday directory copies; -a is better when you need to preserve symlinks and permissions exactly.

Copying Multiple Files at Once

You can list several sources before a final destination directory. All sources are copied into that directory, and they keep their original names.

cp file1.txt file2.txt file3.txt /tmp/
cp *.log /var/log/archive/

Wildcards are expanded by the shell, so cp *.log copies every file ending in .log. The destination must be a directory; if it is not, cp errors because it cannot copy multiple files onto a single file.

Preserving File Attributes with -p and -a

By default, cp creates the copy with the current user’s ownership and the current time. To keep the original modification time, permissions, and ownership, use -p. The -a option is a common shortcut for backups because it includes recursion and preserves more attributes.

cp -p notes.txt notes_backup.txt
cp -a /home/user/docs /backup/docs

With -p, the copy’s timestamp matches the original, which is useful for syncing. -a preserves symbolic links, permissions, timestamps, and ownership where permitted. Without -p, the copy shows the current time, which can confuse backup tools that rely on timestamps.

Avoiding Overwrites with -i, -n, and -u

When the destination already exists, cp silently overwrites it. To prevent data loss, use one of these options:

  • -i prompts before each overwrite. Type y to overwrite, n to skip.
  • -n never overwrites; it simply skips the copy if the destination exists.
  • -u copies only if the source is newer than the destination or if the destination is missing. This is handy for incremental backups.
cp -i report.pdf /tmp/
cp -n report.pdf /tmp/
cp -u report.pdf /tmp/

Many administrators set an alias alias cp='cp -i' to avoid accidental overwrites. Note that -i and -n are mutually exclusive; the last one given wins.

Common Mistakes and Troubleshooting

A frequent error is forgetting -r when copying a directory. Another is trying to copy a file onto a directory without specifying a new name; if you want to rename it, include the full path. Also, when copying across filesystems, cp works fine, but if you want to preserve hard links, use cp -l or cp -a depending on your needs.

If you see Permission denied, you lack write access to the destination directory. Use sudo only when you understand the security implications. If you copy a symlink without -a, the copy follows the link and copies the target file, not the link itself. Use -P (or -a) to preserve the symlink.

Alternatives and Related Commands

For moving files (deleting the source), use mv. For syncing directories efficiently, rsync is more powerful because it copies only changed parts and can work over a network. For creating hard links instead of copies, use ln. The cp command is part of the GNU coreutils on Linux and the BSD base system on macOS; the options described here are standard across both.

Frequently Asked Questions

How do I copy a file and keep the same permissions?

Use cp -p source destination. The -p flag preserves mode, ownership, and timestamps. If you are copying a whole directory tree and want to preserve symlinks and all attributes, use cp -a.

How do I copy multiple files to a different directory?

List all source files followed by the destination directory: cp file1.txt file2.txt /target/. You can use wildcards like cp *.txt /target/ to match many files. The destination must be a directory.

How do I copy a directory in Linux?

Use cp -r source_dir destination_dir. The -r (or -R) flag tells cp to copy recursively. If the destination directory does not exist, it is created.

What is the difference between cp and mv?

cp creates a duplicate and leaves the original in place. mv moves the file, removing the original. Use mv to rename or relocate a file; use cp when you need to keep the original.

How do I avoid overwriting files with cp?

Use -n to skip existing files, or -i to be prompted before each overwrite. For conditional copying based on timestamps, use -u to copy only when the source is newer.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →