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
lsIn 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 backupIn 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-oldIn 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
| Option | What it does |
|---|---|
-r | Copy directories and everything inside them |
-i | Interactive: ask before overwriting an existing file |
-v | Verbose: print each file as it's copied |
-p | Keep 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:
cpoverwrites the destination without asking if it already exists. When in doubt, usecp -i.
Remember:
cp source destination. Add-rfor directories.