Vol. I · No. 46The command line sectionSat, 10 Oct 2026
Linux · file management

cp

a second copy

cp copies a source to a destination. For a directory it needs to be told to recurse, and the flag worth learning is -a: it recurses and keeps permissions, ownership, timestamps and links exactly as they were. By default cp will overwrite an existing file without asking.

A copy is only a backup if it keeps the metadata as well as the bytes — that is what -a is for.

Shape
cp SOURCE DEST
last argument is the target
Directories
-r or -a
-a also preserves metadata
Overwrites
Silently
use -i to be asked
See progress
-v
one line per file copied

At the terminal

Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.

user@netflow — bashcoreutils
$ cp -av config/ config.bak/
1'config/' -> 'config.bak/'
2'config/app.yaml' -> 'config.bak/app.yaml'
'config/tls' -> 'config.bak/tls'
3'config/tls/server.key' -> 'config.bak/tls/server.key'
$ ls -l config.bak/tls/server.key
4-rw------- 1 user user 1704 Sep 12 08:15 config.bak/tls/server.key
Illustrative session · names, sizes and dates are examples, and every address is from a documentation or private range
Reading the output

What the numbered lines are telling you

1
The directory itself is created first

config.bak did not exist, so it became the copy. Had it existed, the copy would have landed inside it as config.bak/config.

2
-v narrates every file

Source on the left, destination on the right. Without -v a successful cp prints nothing at all.

3
-a went down into the subdirectory

Archive mode implies recursion, so the tls folder and the key inside it came along.

4
The copy kept its date and mode

Still rw------- and still dated the twelfth. A plain cp -r would have stamped it with today's time.

Switches

A switch changes how the command behaves. These are the ones used day to day; man cp lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.

-r
Recursive: copy a directory and everything inside it. -R is the same.
-a
Archive: recursive, and keep permissions, ownership, timestamps and links. The one to use for backups.
-i
Interactive: ask before overwriting a file that already exists.
-n
No-clobber: never overwrite an existing file.
-u
Update: copy only when the source is newer than the destination, or the destination is missing.
-p
Preserve the mode, ownership and timestamps of each file.
-v
Verbose: print each file as it is copied.

Variations worth knowing

The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.

cp -i notes.txt backup/

Asks before overwriting a file of the same name.

cp -u *.conf /etc/app/

Copies only files that are newer than the copy already there.

cp file{,.bak}

Brace expansion for "cp file file.bak" — the quickest backup before an edit.

cp -a /src/. /dest/

Copies the contents of a directory, hidden files included, into an existing one.

In cybersecurity

Copying a file can quietly change who owns it and who may read it, which matters most for exactly the files worth copying.

Ownership changes hands

Without -a, the copy belongs to whoever ran cp. A root-made copy of a user's file is now root's; a user's copy of a readable secret is now theirs.

Backups of secrets

A .bak of a key or config file is as sensitive as the original. Check its mode and where it sits — especially inside a web root.

Silent overwrite

cp replaces the destination without warning. Use -i interactively and -n in scripts that must not clobber.

Filed under

File management

Read alongside