How to copy files and directories with the cp command

By Angus Published 20 February 2025 Updated 4 March 2026 3 min read

The cp command copies files and directories on Linux systems. You use it to duplicate configuration files, back up website content or move data between directories without removing the original.

This guide covers common cp operations with practical examples. You will copy single files, entire directories and preserve file attributes during the copy process.

Before you begin

  • You need SSH access to your server.
  • Replace cpusername in all examples with your actual cPanel username.

Using copy (cp)

There are a few different ways to utilise the cp command to copy file(s), below are some of the most common examples.

Copy a single file

The basic cp syntax copies one file to a new location. This creates a duplicate while leaving the original file untouched.

cp /home/cpusername/public_html/index.html /home/cpusername/backup/

This command copies index.html from your public web directory to a backup folder. The original file remains in public_html.

Copy multiple files at once

You can copy several files to the same destination directory in one command. List each source file before specifying the target directory.

cp /home/cpusername/public_html/index.html /home/cpusername/public_html/about-us.html /home/cpusername/backup/

This copies both index.html and about-us.html to the backup directory. The last path in the command must always be the destination.

Copy an entire directory

The -r flag enables recursive copying. This copies a directory and all its contents, including subdirectories and files.

cp -r /home/cpusername/public_html /home/cpusername/backup/

This command copies your entire public_html directory structure to the backup location. Without -r, the command would fail because cp cannot copy directories by default.

Preserve file attributes

The -p flag preserves original file attributes during copying. This maintains timestamps, ownership and permissions on the copied files.

cp -p /home/cpusername/public_html/index.html /home/cpusername/backup/

Preserving attributes keeps backup copies identical to their originals. This prevents permission issues when restoring files later.

Prompt before overwriting

The -i flag adds interactive prompts. You must confirm before cp overwrites any existing files at the destination.

cp -i /home/cpusername/public_html/index.html /home/cpusername/backup/

If index.html already exists in the backup directory, the command pauses and asks for confirmation. This prevents accidental data loss.

Combine multiple flags

You can combine flags to apply multiple options in one command. This creates more controlled and safer copy operations.

cp -rpi /home/cpusername/public_html /home/cpusername/backup/

This command copies the entire directory recursively, preserves all attributes and prompts before overwriting. Combining -r, -p and -i gives you a safe backup process.

Wrapping up

You now know how to copy files and directories using the cp command. You can duplicate single files, copy entire directory structures and preserve file attributes during the process. Interactive prompts protect against accidental overwrites.

For copying files between servers, the rsync command offers better performance and additional features. The GNU coreutils manual documents all available cp flags and options.

Need more power?

Get scalable resources with our VPS hosting with root access and optional software.

Get VPS Hosting

Starting something new?

Perfect for websites and small businesses unlimited bandwidth with cPanel hosting.

Get cPanel Hosting