Lessons · Terminal · copying a file
Copying
cp from to makes a copy and leaves the original alone. A folder needs cp -r, for recursive, or cp refuses it.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Back something up before you change it. A copy costs nothing; an undo usually does not exist.
How to think about it
Say what you are copying and where. If it is a folder, add -r. If the target is a folder that exists, the copy lands inside it.
Worked example
echo hello > a.txtA file with one line.
cp a.txt b.txtA copy.
cat b.txthello: the same contents.
lsa.txt and b.txt both exist.
mkdir d && cp -r d d2 && ls -d d2A folder copied whole: d2.
Your turn
Copy the whole photos folder to photos-backup.
cp photos photos-backup
Try it at a real prompt
The trap
cp onto a name that already exists replaces it without asking. Check the target first.
Practise copying a file on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.