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.
cpusername in all examples with your actual cPanel username.There are a few different ways to utilise the cp command to copy file(s), below are some of the most common examples.
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.
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.
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.
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.
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.
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.
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.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting