Copying with cp

Copy a file

cp takes two arguments: what to copy (the source) and where to put it (the destination):

cp notes.txt notes-copy.txt
ls

In the terminal

student@vulntech:~$ cp notes.txt notes-copy.txt
student@vulntech:~$ ls
Documents  backup  notes-copy.txt  notes.txt  report.txt

Copy into a directory

If the destination is a directory, the copy goes inside it and keeps its name. You can copy several files into a directory at once:

cp notes.txt backup/
cp notes.txt report.txt backup/
ls backup

In the terminal

student@vulntech:~$ cp notes.txt backup/
student@vulntech:~$ cp notes.txt report.txt backup/
student@vulntech:~$ ls backup
notes.txt  report.txt

Copy a whole directory with -r

To copy a directory and everything in it, add -r (recursive). Without it, cp refuses:

cp Documents Documents-old
cp -r Documents Documents-old
ls Documents-old

In the terminal

student@vulntech:~$ cp Documents Documents-old
cp: -r not specified; omitting directory 'Documents'
student@vulntech:~$ cp -r Documents Documents-old
student@vulntech:~$ ls Documents-old
book.txt

Useful options

OptionWhat it does
-rCopy directories and everything inside them
-iInteractive: ask before overwriting an existing file
-vVerbose: print each file as it's copied
-pKeep the original's modified time and permissions
cp -v notes.txt report.txt backup/

In the terminal

student@vulntech:~$ cp -v notes.txt report.txt backup/
'notes.txt' -> 'backup/notes.txt'
'report.txt' -> 'backup/report.txt'

Common mistake: cp overwrites the destination without asking if it already exists. When in doubt, use cp -i.

Remember: cp source destination. Add -r for directories.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…