files
cp
Copies files and directories from one location to another. Preserves or transforms metadata depending on flags.
cp [OPTION]... SOURCE... DEST Common flags
| Flag | Purpose |
|---|---|
| -r | Recursively copy directories |
| -a | Archive mode: preserve attributes, links, and recurse |
| -i | Prompt before overwriting an existing file |
| -u | Copy only when SOURCE is newer than DEST or DEST is missing |
| -p | Preserve mode, ownership, and timestamps |
| -v | Verbose: print each file as it is copied |
Examples
cp file.txt backup.txt Duplicate a single file
cp -a src/ dst/ Recursively copy a directory tree preserving attributes
cp -uv *.log /mnt/backup/ Sync only newer log files verbosely
cp -i important.conf important.conf.bak Interactively back up a config to avoid clobbering
Gotcha
Unlike rsync, a trailing slash on SOURCE does NOT change cp's behavior — `cp -a src/ dst` and `cp -a src dst` behave identically. To copy just a directory's contents, use `cp -a src/. dst/` or `cp -a src/* dst/` (the latter misses dotfiles).
Related commands
mv
Moves or renames files and directories. Within a filesystem it just relinks; across filesystems it copies then deletes.
rsync
Efficient file synchronizer that copies only the differences between source and destination. Works locally or over SSH.
ln
Creates hard or symbolic links between files. Symbolic links point to a path; hard links share the same inode.
ls
Lists directory contents including files and subdirectories. Supports detailed views, sorting, and filtering to inspect the filesystem quickly.
rm
Removes files and, with -r, directory trees. Deletions are immediate and not recoverable from the shell.