How to use the SSH cp command

Learning how to use the SSH cp command is one of the most essential Linux skills. With it, you can copy files and directories from one location to another directly from the terminal. It is fast, reliable, and beginner-friendly, which makes it perfect for managing files on your server. In this article, we will show you how to use the SSH cp command with clear, practical examples.

How to use the SSH cp command

In short, cp copies a source file or folder to a destination you choose. To get started, connect to your account over SSH, then run the examples below to copy files safely and efficiently.

Copy a file

First, to copy a single file, name the source and then the destination:

cp source.txt destination.txt
cp file.txt /home/user/backup/

Copy a directory

Next, to copy a folder and everything inside it, add the -r (recursive) flag:

cp -r mydir /home/user/backup/

Copy multiple files at once

Additionally, you can list several files before the destination directory to copy them all in one step:

cp file1.txt file2.txt file3.txt /home/user/docs/

Useful cp options

Finally, a few flags make the SSH cp command safer and clearer. Use -i to confirm before overwriting, -v to see what is copied, and -p to preserve permissions and timestamps:

cp -i file.txt /home/user/backup/
cp -v file.txt /home/user/backup/
cp -p file.txt /home/user/backup/

Because cp can overwrite files without warning, the -i flag is a smart habit when you work with important data. You can also combine flags, for example cp -rv, to copy a directory while watching each file as it moves. If you need to copy files between two different servers instead of within one, use the related scp command, which works over the same SSH connection.

Need more help?

Master the basic Linux commands with examples on a SSH shared hosting account, or get full root control with a JetHost VPS. Both include free SSH access and full Linux terminal support, so you can run the SSH cp command directly on your server today.