Working with files
Linux commands: cp
Learn how the Linux cp command copies files from one place to another, and how the -r recursive option lets you copy entire folders and their contents.
8 minute lesson
cp creates a new directory entry with copied contents:
cp report.txt report-backup.txt
If the destination is a directory, the copy keeps the source filename:
cp report.txt backups/
Copy directories recursively with -R:
cp -R site site-backup
The most important question is what already exists at the destination. Plain cp can overwrite a file without a prompt. During manual work, cp -i asks before overwriting and cp -n skips existing destinations on implementations that support it. Check the exit status rather than assuming every file copied.
Metadata behavior matters too. A basic copy may give the destination new timestamps and ownership. cp -p preserves selected metadata; archive options vary between systems. For a portable lesson, inspect the files after copying instead of assuming Linux and macOS flags are identical.
Always quote paths that can contain spaces:
cp "Quarterly report.txt" "backups/Quarterly report.txt"
Before a large recursive copy, list the source and destination, verify available disk space, and consider whether a synchronization tool such as rsync better matches incremental updates.
Try it: copy a file, modify the original, and compare them with cmp. The copy is independent; later changes to the source do not update it.
Lesson completed