cp — copy with care
cp source dest copies one file. Common flags:
-r— recursive, for directories-i— interactive, ask before overwrite-n— never overwrite (silently skips)-v— verbose, list each file copied-p— preserve mode, ownership, mtime-a(GNU) — archive:-rpplus symlinks etc.
Trailing slash matters: cp foo/ bar copies contents of foo into bar (if bar exists). Without the slash, cp foo bar renames or creates a child. Always test on a throwaway directory first.
mv — move and rename
mv is the same operation as rename when source and destination are on the same filesystem — it just relinks the inode. Across filesystems it copies then deletes, which is slower and not atomic.
Same flags as cp where they apply: -i, -n, -v. There is no -r because mv always moves whatever you point at, recursive or not.
When to reach for rsync instead
For anything bigger than a few files, switch to rsync -av source/ dest/. It can resume, skip unchanged files, preserve permissions, and tell you exactly what it did. We dig into it in the network-remote track.
The trailing-slash quiz
cp -r dir1 dir2— ifdir2exists, createsdir2/dir1. If not, createsdir2as a copy.cp -r dir1/ dir2/— copies the contents ofdir1intodir2.cp -r dir1/. dir2/— same as above, more explicit.
Don't memorize. Always run cp -nv first, see the output, then drop the -n.
Overwrite policy is central to copy and move
Replacing an existing target, nesting inside it, or refusing can depend on the command, options, and operating system. Inspect target existence and type first, and rehearse bulk work against the same structure in a temporary directory.
Cross-volume moves fail differently
A rename within one filesystem can be fast and atomic, while a move across volumes becomes copy then delete. Allow for partial failure and free-space exhaustion, verify the copy, and confirm when the source is removed.