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.
cp [OPTION]... SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY
Try it
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.
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
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 dstcreatesdstas a copy ofsrcifdstdoes not exist, but createsdst/srcifdstalready exists. Usecp -rT src dstto get the first behavior every time. - Copy the contents of a directory with
cp -a src/. dst/; the.also catches hidden files, whichsrc/*misses. - Plain
cpdoes not preserve ownership, timestamps or all permission bits; the copy belongs to you and is dated now. Use-afor backups. - Symlinks: with
-ralone,cpcopies the target of command-line symlinks and the links themselves inside the tree.-akeeps 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:
cpdetects holes by default (--sparse=auto). On filesystems with copy-on-write (Btrfs, XFS),--reflink=automakes copies instant. - Many files at once:
find ... -print0 | xargs -0 cp -t dest/copes with huge argument lists and odd names. cpdoes not verify the result. For critical data compare withcmpor usersync --checksum.- A dash-prefixed name needs
--:cp -- -odd.txt copy.txt.
Related
mv,rm,ln,mkdir,rsync- Lessons: Copying, moving and deleting, Wildcards and globbing