Back up a config before editing
-a keeps the owner and permissions, so the backup can be put back as is.
The cp command copies files and directories, with overwrite prompts, preserved permissions and times, recursively.
cp app.conf app.conf.bak
a copy next to it, another name
cp app.conf /tmp/
into a directory, same name
cp a.txt b.txt docs/
several files into a directory
cp -r project project-copy
a whole directory
cp -a /etc/nginx ~/nginx-backup
keep permissions, owner, times
cp -i app.conf /etc/app.conf
ask before overwriting
cp -n app.conf /etc/app.conf
never overwrite an existing file
cp -u *.conf /srv/conf/
only if the source is newer
cp -v a.txt b.txt
print what gets copied
cp -r project/. backup/
a directory's contents, not itself
cp source destination makes a copy. If the destination is an existing directory,
the copy goes inside it under the same name. If the destination doesn't exist, a new
file with that name appears.
With no options cp copies files only and skips directories. Directories are copied
with -r (recursive) or -a (archive — also permissions, owner, times, and symlinks
as symlinks).
cp silently overwrites an existing destination file. When a mistake is expensive,
add -i or -n.
cp and mv share many options: -i, -n, -u, -v, -b, -t. Full list —
man cp.
| Option | What it does | Example |
|---|---|---|
-r |
Copy directories recursively, with everything inside (same as -R). | cp -r project project-copy |
-a |
Archive mode — recursive, keeping permissions, owner, times and links. | cp -a /etc/nginx ~/nginx-backup |
-i |
Prompt before overwriting an existing file. | cp -i app.conf /etc/app.conf |
-n |
Never overwrite existing files. | cp -n app.conf /etc/app.conf |
-u |
Copy only when the source is newer than the destination or it's missing. | cp -u *.conf /srv/conf/ |
-v |
Print each copy — 'a' -> 'b'. | cp -v a.txt b.txt |
-p |
Preserve mode, ownership and timestamps. | cp -p app.conf app.conf.bak |
-b |
Before overwriting, keep the old file with ~ appended to its name. | cp -b new.conf app.conf |
-t dir |
Destination directory as the first argument — handy with find and xargs. | cp -t /srv/www/ index.html style.css |
--parents |
Recreate the source path inside the destination directory. | cp --parents etc/app.conf /backup/ |
Nothing found. Try a shorter query.
-a keeps the owner and permissions, so the backup can be put back as is.
The same command gives different results depending on whether dst exists. Check
the result: ls dst.
cp works within one machine. Over the network use rsync or scp (see
ssh-client).
The source is a directory and -r is missing. Add -r or -a: cp -a src dst.
There's no source at that path. Check pwd and the name with ls; complete the
path with Tab.
You can't write to the destination directory. Check ls -ld dir; system
directories need sudo cp.
sudo cp creates new files as root. Keep the source's owner with -a, or fix the
owner: sudo chown student:student file — see chmod-chown.
Without -p or -a the copy gets the current time and permissions from the
umask. For backups use cp -a.
The destination filesystem is full. df -h path shows the free space — see
disk-usage.
cp new.conf /etc/app.conf replaces a working config without asking, and the old
text is gone. Make a backup first (cp -a … .bak), then replace — or use cp -i.
cp -r into an existing directoryIf the destination directory already exists, cp -r src dst puts the copy inside
it — dst/src. A script that expects "dst becomes the copy" will start nesting
directories when run a second time.
Found an inaccuracy? Report an error in the article