Copying things
You can make files and folders from scratch. Often, though, what you really want is a duplicate of something that already exists — a backup before you change a config file, a copy of a template to start a new project from, a snapshot of today’s notes.
The command is cp — copy.
Copying a file
The shape is always the same: cp source destination.
Make a test file:
echo "first draft" > draft.txt
Now copy it:
cp draft.txt draft-backup.txt
Run ls. You now have both — draft.txt (the original) and draft-backup.txt (the
identical copy). They start the same but are now independent. Change one and the other
doesn’t move.
Copying into a folder
If the destination is an existing folder, the file is copied inside that folder, keeping its original name:
mkdir backups
cp draft.txt backups
Now ls backups shows draft.txt. You moved a copy into the folder without renaming it.
You can do both at once — copy into a folder and give it a new name:
cp draft.txt backups/draft-2026-01-01.txt
The trap when copying a folder
Try this:
cp backups archive
The terminal refuses:
cp: backups is a directory (not copied).
By default, cp only copies files. Folders are blocked — because copying a folder secretly
means copying everything inside it, and that’s potentially a lot of data the terminal
doesn’t want to do without you saying so.
The fix: -r
Add -r (for recursive — meaning “and everything inside, and everything inside that, all
the way down”):
cp -r backups archive
Now you have an archive folder that’s an identical copy of backups, including every file
inside it. This is the only flag you’ll add to cp most days.
Copying many at once
Want to put a bunch of files into a single folder? List them all, with the destination last:
cp draft.txt notes.txt todo.txt backups/
The trailing / on backups/ is a small habit worth picking up — it makes your intent
obvious (“I mean the folder, not a file with that name”) and the terminal will catch you if
the folder doesn’t exist.
The footgun
Just like > for redirects, cp overwrites silently. If backups/draft.txt already
exists, the copy replaces it. No warning.
If you want a heads-up before overwriting, use -i (interactive):
cp -i draft.txt backups/
It’ll prompt before overwriting anything. Slower, safer — useful when copying into folders you care about.
What you’ve learned
cp source dest— copy a file.cp -r source dest— copy a folder and everything inside.cp file1 file2 file3 folder/— copy several files into a folder.cp -i ...— ask before overwriting.
Next: moving and renaming. (Spoiler: in the terminal, they’re the same operation.)