Log inSign up

Copying files and folders on a Linux server with cp

Copy a file with cp source target and a whole folder with cp -a source/ target/, which walks the tree and keeps owners, permissions, timestamps and links. Add -i to be asked before anything is overwritten and -v to see each file as it is copied. For a quick safety copy before editing a config, sudo cp -a /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig is all you need.

Topic: Linux command line · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

How does the basic cp syntax work?

The cp command takes one or more sources and a single destination, and the last argument is always the destination. When the destination is an existing directory, each source lands inside it under its own name; when it is a file name, the copy gets that name. cp is part of GNU coreutils, so it is already present on every Ubuntu, Debian, Rocky Linux and AlmaLinux image you can deploy, and nothing needs to be installed.

cp --version | head -n 1
cp notes.txt notes-copy.txt
cp notes.txt /home/deploy/archive/
cp report.csv summary.csv /home/deploy/archive/

Ubuntu 24.04 ships coreutils 9.4, Debian 12 ships 9.1 and Rocky or Alma 9 still carry 8.32. The everyday options below behave the same on all of them; the few differences are pointed out where they matter. If you are new to the shell, log in first with the first SSH session tutorial and work as a normal user with sudo.

How do I copy a whole directory?

Plain cp refuses to copy a directory and prints -r not specified; omitting directory. Give it -r (recursive) to walk the tree, or better, -a (archive), which is recursive and also keeps modes, ownership, timestamps and symbolic links as links.

cp -r project project-copy
cp -a /var/www/app /var/www/app-staging

The trailing slash and the dot matter more than most people expect. If the target directory does not exist yet, cp -a app backup creates backup as a copy of app. If backup already exists, the same command creates backup/app instead. To pour only the contents of one folder into another existing folder, use the dot form:

mkdir -p /srv/release
cp -a build/. /srv/release/

The build/. source means "everything inside build, hidden files included", which a shell glob such as build/* would miss because * skips names starting with a dot.

Which flags keep ownership and permissions?

Use -a whenever the copy should look exactly like the original, for example a web root, a home directory or a config folder. It is shorthand for -dR --preserve=all. If you only want to keep mode and timestamps without ownership, -p does that for single files.

  • -p: keep mode, owner and timestamps (owner only when you run as root).
  • --preserve=mode,timestamps: choose the attributes one by one.
  • --no-preserve=ownership: the copy belongs to you even when the source belongs to someone else.
  • -L: follow symbolic links and copy the files they point to.
  • -P or -d: copy links as links (part of -a).

Ownership can only be kept by root, so a normal user copying files owned by www-data ends up as the owner of the copy. Run the command with sudo when the owner must stay the same, and read the file permissions guide if the result looks wrong.

How do I stop cp from overwriting files by accident?

By default cp overwrites an existing target silently, so add a guard flag whenever the destination may already hold data. -i asks before each overwrite, -n never overwrites, and -u only copies when the source is newer than the target or the target is missing.

cp -i settings.php /var/www/app/
cp -n *.jpg /srv/media/
cp -u -v docs/*.md /srv/docs/

On coreutils 9.3 and later (Ubuntu 24.04) you can also write --update=none, which spells out the same "skip existing" behaviour as -n in a form that scripts read more clearly. Rocky and Alma 9 do not know that long form, so keep -n in scripts that must run on both families.

Another safe habit is a numbered backup of whatever is replaced. --backup=numbered renames the old target to name.~1~, name.~2~ and so on before writing the new file:

sudo cp --backup=numbered new-site.conf /etc/nginx/sites-available/site.conf
ls /etc/nginx/sites-available/

How do I back up a config file before editing it?

Make a dated copy next to the original, edit, test, and keep the copy until the change has proven itself. The date in the name tells you later which version came from when.

sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.$(date +%F)
sudo nano /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl reload ssh

On Rocky and Alma the service is named sshd, so the last line becomes sudo systemctl reload sshd. If the test fails, put the original back with sudo cp -a /etc/ssh/sshd_config.2026-12-03 /etc/ssh/sshd_config. Be careful with drop-in folders such as /etc/nginx/conf.d/ or /etc/ssh/sshd_config.d/: many services load every file ending in .conf, so a copy called old.conf left there is read too. Put such backups in your home directory or give them a suffix the service ignores.

Can I copy many files at once or by pattern?

Yes. The shell expands patterns before cp runs, and cp accepts any number of sources as long as the destination is a directory. With -t you name the target first, which is handy when the list of sources comes from another command.

cp *.log /home/deploy/old-logs/
cp report-2026-1[0-2]*.pdf /srv/archive/
cp -t /srv/archive/ a.txt b.txt c.txt
find . -name '*.conf' -newer stamp -exec cp -t /root/conf-backup/ {} +

The last line copies only config files changed after the file stamp was touched. The find guide explains the tests, and xargs and pipes covers longer chains. To copy files to another machine instead of another folder, use scp or rsync; cp only works inside one system.

How do I verify that a copy is complete?

Compare sizes and checksums, or let diff walk both trees. A missing or changed file shows up immediately.

du -sh /var/www/app /var/www/app-staging
diff -r /var/www/app /var/www/app-staging && echo identical
sha256sum big-dump.sql /srv/backup/big-dump.sql

When diff prints nothing and the echo runs, both trees hold the same content. For large copies, check free space first with df -h; a disk that fills halfway through leaves a partial copy and an error such as No space left on device. The disk usage guide shows how to find what is taking the room.

Why does cp fail, and how do I fix it?

Most cp errors come from permissions, missing directories or paths with spaces.

  • Permission denied: you cannot read the source or write the target. Check with ls -l, then use sudo or fix ownership.
  • No such file or directory: the target folder does not exist. Create it with mkdir -p first.
  • Spaces in names: quote them, as in cp "Annual Report.pdf" /srv/docs/.
  • Names starting with a dash: end options with --, as in cp -- -draft.txt drafts/.
  • Copy is huge: you may have followed a link into a big tree with -L; use -a instead.

For very large trees or copies you want to resume after an interruption, rsync with -a --progress is the better tool, since it skips files that are already complete.

Questions about Copy files and directories with cp

What is the difference between cp -r and cp -a?

Both copy directories recursively. -a also keeps permissions, ownership, timestamps and symbolic links, so the copy matches the original. Use -r only when you want new default attributes.

Does cp copy hidden files?

cp copies whatever you name. cp -a dir/. target/ includes dotfiles, while cp dir/* target/ skips them because the shell glob ignores names that start with a dot.

How can I see progress while copying?

Add -v to print each file name. For a byte counter on a large copy, run rsync -a --info=progress2 source/ target/ instead.

Can cp copy to another server?

No, cp works within one system. Use scp or rsync over SSH to send files to another host, for example when moving to a bigger plan.

Why is my copy owned by me and not by the original user?

Only root can create files owned by another account. Run the copy with sudo cp -a, or change the owner afterwards with sudo chown -R.