cp: copy files and directories
cp copies files. With -r it copies whole directory trees, and with -a it keeps permissions, owners and times exactly as they were.
cp [options] source target
cp [options] source ... directory
Common options
| Option | Meaning |
|---|---|
-r, -R | Copy directories recursively |
-a | Archive: recursive, keep permissions, owners, times and links (GNU, BSD, macOS) |
-p | Keep mode, owner and timestamps |
-i | Ask before overwriting |
-n | Never overwrite an existing file |
-u | Copy only when the source is newer (GNU) |
-v | Show each file as it is copied |
-L | Follow symbolic links and copy what they point to |
Examples
cp notes.txt notes.bak # copy a file
cp report.pdf ~/Documents/ # copy into a directory
cp *.jpg /srv/photos/ # many files into a directory
cp -r project project-copy # copy a directory tree
cp -a /etc /root/etc-backup # exact copy, keeping owners and permissions
cp -i config.yml /etc/app/ # ask before overwriting
cp --backup=numbered app.conf /etc/app/ # keep the old file as app.conf.~1~ (GNU)
Things to know
cpsilently overwrites existing files unless you use-ior-n. Many distributions aliascptocp -ifor root.cp -r src destbehaves differently depending on whetherdestexists: if it does, you getdest/src. To copy the contents of a directory, usecp -r src/. dest/.- Copying as a normal user makes you the owner of the copies. Only root can keep the original owners with
-aor-p. - For large trees, network copies, or copying only what changed, use
rsync. To move instead of copy, usemv. - On file systems that support it (btrfs, XFS, ZFS),
cp --reflink=automakes instant copy-on-write copies that share data until one is changed.