Hone

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.txt
A file with one line.
cp a.txt b.txt
A copy.
cat b.txt
hello: the same contents.
ls
a.txt and b.txt both exist.
mkdir d && cp -r d d2 && ls -d d2
A folder copied whole: d2.

Your turn

Copy the whole photos folder to photos-backup.

cp  photos photos-backup

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.