Module 2 · Files and directories
Copying with cp
The three shapes of cp - file to file, files into a directory, directory tree with -r - and the overwrite that happens silently unless you ask it not to.
What you will learn
- Copy a file under a new name or into a directory.
- Copy whole directories with `-r`.
- Avoid silent overwrites with `-i` and keep metadata with `-p`.
cp copies. Its usage line is cp [OPTIONS] SOURCE... DEST, and the whole command is about how it interprets DEST. There are three cases, and once you can tell them apart you will never be surprised by where a copy landed.
| Shape | DEST is | Result |
|---|---|---|
cp notes.txt notes.bak | A file name that does not exist (or an existing file) | A copy with that name. An existing file is overwritten. |
cp report.txt backup/ | An existing directory | backup/report.txt, same name inside the directory. |
cp a.txt b.txt c.txt backup/ | An existing directory (required with several sources) | All of them inside backup/. |
~% cd lab/l13
l13% cp notes.txt notes.bak
l13% cp report.txt backup/
l13% ls backup
report.txt
l13% cp site site-copy
cp: omitting directory 'site'
l13% cp -r site site-copy
l13% ls -R site-copy
site-copy:
css index.html
site-copy/css:
style.css
Directories need -r
Asked to copy a directory, plain cp refuses with omitting directory. Add -r (or -R; *recursive*) and it copies the directory with everything inside, at every depth. If the destination does not exist, it becomes the copy: cp -r site site-copy creates site-copy as a twin of site. If the destination does exist as a directory, the source is copied *into* it: the same command run a second time produces site-copy/site. That second behaviour catches everyone once.
Overwriting
If the destination file exists, cp replaces its contents without asking. There is no recycle bin. -i (*interactive*) makes it ask overwrite 'x'? first; answer y or n. -n never overwrites, silently. Many systems alias cp to cp -i for root; this one does not, so the protection is yours to type. A quick ls DEST before a copy is the cheapest insurance there is.
What a copy keeps
By default a copy is a brand-new file: same contents, but the modification time is now and the owner is you. -p (*preserve*) carries over the timestamp, permissions and owner, which matters for backups and for anything that sorts by date. -a (*archive*) is -r plus -p plus keeping symbolic links as links: the right default for copying a whole tree faithfully. -v prints each copy as it happens, useful with many files.
Commands in this lesson
| Command | What it does |
|---|---|
cp SRC DEST | Copy a file to a new name (overwrites an existing DEST). |
cp SRC... DIR/ | Copy one or more files into a directory. |
cp -r SRCDIR DEST | Copy a directory and everything below it. |
cp -i | Ask before overwriting. |
cp -p / cp -a | Keep timestamps and permissions / full faithful tree copy. |
Quiz
`cp a.txt b.txt` when `b.txt` already exists…
- Asks for confirmation
- Overwrites `b.txt` silently
- Fails with `File exists`
What does `cp site backup` print if `site` is a directory?
- Nothing; it copies the directory
- `cp: omitting directory 'site'`
- `Permission denied`
`backup/` is an existing directory. Where does `cp -r site backup/` put the copy?
- It replaces `backup` with a copy of `site`
- In `backup/site`
- It fails
Which option keeps the original's timestamp and permissions?
- `-i`
- `-p`
- `-v`
You run `cp a.txt b.txt c.txt dest` and `dest` does not exist. What happens?
- The three files are merged into `dest`
- An error: with several sources the destination must be an existing directory
- `dest` is created as a directory automatically
Practice
In /root/lab/l13, copy `notes.txt` to a new file called `notes.bak`.
Copy /root/lab/l13/report.txt into the existing directory /root/lab/l13/backup, keeping its name.
Copy the whole directory /root/lab/l13/site, including its `css` subdirectory, to /root/lab/l13/site-copy.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.