cp copies files. Given a source and a destination it writes a duplicate; given multiple sources it requires the destination to be a directory.
cp file1.txt file2.txt # copy, overwriting file2 if present
cp file.txt backup/ # copy into directory
cp -r src/ dest/ # recurse into directories
cp -i file.txt dest/ # prompt before overwriting
cp -p file.txt dest/ # preserve permissions and timestamps
cp -a src/ dest/ # archive: recurse + preserve everything
cp -u src dest # update: only if source is newer
By default cp follows symbolic links and copies the target, not the link. Use -P to copy links themselves. For copying across machines, reach for scp or rsync.
Discussed in:
- Chapter 6: Files, Directories, and Pathnames — Copying, Moving, and Removing