Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux cp command

Reference for the Linux cp command: copy files and directories, -r and -a, preserve attributes, avoid overwriting with -n, make backups and common mistakes.

cp copies files and directories. Given two names it copies the first to the second; given several sources and a directory as the last argument, it copies every source into that directory. The copy is a new, independent file with its own inode: changing one never changes the other.

Directories need -r (recursive). For a faithful copy of a tree, with permissions, timestamps, ownership and symlinks kept as they are, use -a (archive). By default cp silently overwrites an existing destination file, so -n and -i exist to protect you.

Terminal
cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY

Try it

Bash Copy files, then a directory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Overwriting, backups and attributes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
cp [-aipRrdfnuvTt] [--backup[=CONTROL]] SOURCE... DEST
Option Description
-r, -R Copy directories recursively.
-a Archive: same as -dR --preserve=all; keeps symlinks as links, plus mode, ownership and timestamps.
-p Preserve mode, ownership and timestamps.
-n Do not overwrite an existing file.
-i Ask before overwriting.
-f If the destination cannot be opened, remove it and try again.
-u Copy only when the source is newer than the destination, or the destination is missing.
-v Print each copy as it is made.
-b, --backup[=CONTROL] Rename an existing destination before overwriting (~ suffix; numbered gives .~1~).
-l Create hard links instead of copying data.
-s Create symbolic links instead of copying data.
-L, -P Always follow / never follow symlinks in the source.
-t DIR Name the target directory first (handy with xargs).
-T Treat the destination as a normal file, never as a directory.
--parents Use the full source path below the destination directory.

Returns: 0 if everything was copied, non-zero if any copy failed.

Usage notes

  • Trailing slash and existence. cp -r src dst creates dst as a copy of src if dst does not exist, but creates dst/src if dst already exists. Use cp -rT src dst to get the first behavior every time.
  • Copy the contents of a directory with cp -a src/. dst/; the . also catches hidden files, which src/* misses.
  • Plain cp does not preserve ownership, timestamps or all permission bits; the copy belongs to you and is dated now. Use -a for backups.
  • Symlinks: with -r alone, cp copies the target of command-line symlinks and the links themselves inside the tree. -a keeps links as links everywhere.
  • Copying onto itself is refused ("are the same file"). Copying a file onto a hard link of itself is also refused.
  • Sparse files and large files: cp detects holes by default (--sparse=auto). On filesystems with copy-on-write (Btrfs, XFS), --reflink=auto makes copies instant.
  • Many files at once: find ... -print0 | xargs -0 cp -t dest/ copes with huge argument lists and odd names.
  • cp does not verify the result. For critical data compare with cmp or use rsync --checksum.
  • A dash-prefixed name needs --: cp -- -odd.txt copy.txt.