How to use cp command in SSH?

Hosting Solutions
4 min readApr 9, 2022

I’ve always been writing articles related o SSH commands. The reason behind this is very simple as I like to mange work with command line and continue learning with the wave. Even in my recent article article related to SSH, I’ve shared the procedure to transfer files in FTP via Linux using command line.

cp command is used to copy files or directories via SSH without making any changes in the original one. This is secure and can be done at remote server via SSH. So, today I’m sharing an article that help you to know more about cp command to copy files or directories via SSH.

The syntax used to copy files or directories in Linux is as follows:

cp [SOURCE] [DESTINATION]

Now, this syntax can be used in multiple ways as required to copy files or directories via SSH. Let us learn in detail.

The following steps would be similar before using cp command.

  • Login to SSH client or PuTty.
  • Get inside the directory from where you wish to copy the files or directories by using cd command.

#1. Copy a file with different name in current directory.

*Use ls command to get list of all the files present inside a particular directory.

Suppose I want to copy a file named any.html with the different name as new.html. Then the cp command will be used in the following way.

$ cp any.html new.html

*To see list of all the files with .html format use ls -l *.html.

#2. Copy single file to different directory.

Suppose I want to copy a file named new.html to a directory name any which is inside public_html. The cp command would be use in the following way.

$ cp filename path/of /destination/directory

$ cp new.html /home/public_html/any

To verify this get back to home directory by using cd../ command and then get inside any directory by using cd command then use find command with filename.

#3. Copy Multiple files to different directory.

Here, two things can be done. You can either copy multiple files of different file format or copy a selected format of files. Let us see in detail.

a. Copy multiple file of different file extension.

Suppose, I decided to copy index.html, index.php, new.html to different directory, then the syntax used is given below.

$ cp file1 file2 file3 path/of/destination/directory

$ cp index.html index.php new.html /home/public_html/any

b. Copy multiple files of same file extension.

Now, suppose I want to copy files only of .php file extension. So, the syntax to use cp command will be as follows.

$ cp *.ext path/of/destination/directory

$ cp *.php /home/public_html/any

#4. Copy all files of a directory to another directory.

To copy all the files present inside a particular directory, cp command will use as:

$ cp * path/of/destination/directory

#5. Copy a directory to another directory with all its files or sub-directories.

To copy the whole directory to another one follow the below syntax.

$ cp -R * path/of/destination/directory

#6. Copy file with interactive option.

By using this syntax, system will prompt you before overwriting file. That means, if you want to copy a file and another file with the same name already exists in that location then you will get prompt prior to replace the file with identical name. The syntax is as follows.

$ cp -i test.html new.html

Refer to the below image to see the result.

This means that file with new.html name is already present.

#7. Preserve copied file attributes.

The syntax given below will copy a file to the new location and preserve its attributes such as modification date and time, access time, file mode and access control list as well.

$ cp -p filename path/of/destination/directory

#8. Verbose Option

This allows to see the files as they are copied. The syntax for verbose options is given as.

$ cp -v filename path/of/destination/directory

And That’s it.

Hope the above given syntax help you learn more about cp command and make your working with SSH more interesting.

I’m using SSH to manage most of my work because it has been enabled in my hosting services of REDSERVERHOST without any extra charges. Even it is available in all the hosting plans of REDSERVERHOST.

Do share your suggestions so that we write more for you.

Happy Reading !!

Know About SSH

Basic SSH commands

Transfer Files in SSH

--

--

Hosting Solutions

Get all the solution related to web hosting and website management.