cp: Copying Files
cp (copy) duplicates a file or directory. The original stays intact; you get a new copy at the destination.
Copying a file
cp notes.txt backup.txt
Both notes.txt and backup.txt now exist with identical contents.
Copying to a directory
If the destination is an existing directory, cp places the file inside it:
cp notes.txt documents/
This creates documents/notes.txt. The original /home/user/notes.txt still exists.
Copying multiple files into a directory
cp file1.txt file2.txt target-directory/
Copying a directory recursively
To copy an entire directory and its contents, you need the -r (recursive) flag:
cp -r documents documents-backup
Without -r, cp refuses to copy a directory.
Common mistakes
| Mistake | What happens |
|---|---|
cp notes.txt notes.txt | Copies a file onto itself — usually a no-op or error |
cp dir/ file.txt | Error: you need -r to copy a directory |
| Missing the destination | Error: missing destination operand |
Practice
Copy notes.txt to a new file called backup.txt.