How to copy file from local to remote server in linux

  • Post last modified:December 31, 2020
  • Reading time:4 mins read

If you are looking something to transfer the file using SSH then you are at right article We will cover How to copy a file from local server to a remote server or remote server to local.

This can be easily performed through SCP(Secure Copy). An SCP command utility helps us to transfer file and directories in a secure way to transfer between remote server to local and vice versa.

The reason why we said secure, when you transfer file using SCP, data get encrypted with cipher key between in both system.

How to Use SCP command?

Above we have explained to you what is SCP command now we will see how to use SCP command before that you should know SCP Command Syntax.

scp [OPTION] [SRC-FILE] [[email protected]]:/[REMOTE_LOCATION]

This was the basic command Syntax now we will explain you syntax

  • OPTION – such as cipher, ssh_config, identity_file, ssh port, limit, recursive copy …etc.
  • [SRC-FILE] – File Source
  • [[email protected]]

To get more relatable we will show you demo how to transfer file from a remote server to local server.

How to Copy Files and Directories using SCP between local to the remote system

We will Demonstrate both file and Directory copy from remote and local server.

File transfer using SCP

In this demo guide we will show you how to copy file from local server to remote system using the below command:

$ scp file [email protected]:/home/shen
File transfer from local to remote
File transfer from local to remote

Remote Server

File Successfully received to remote
File Successfully received to remote

This is the first demo to transfer a file to the remote location, Now we will see how this command work First we have use SCP command without any options after that pass the file location and filename In my case “file” is present at current directory then pass the remote address [email protected] with the: colon, In last you need pass directory location where you need to save.

Directories transfer using SCP

On top, we able to transfer file successfully to the remote server now will see how to transfer directories

To transfer directories using the following below command:

$ scp -r SCPDEMO [email protected]:/home/shen
Transfer Directory from local to remote server
Transfer Directory from local to remote server

Remote Server

Verify File received on remote server
Verify File received on remote server

We are able to copy directories or folder from remote server to local server. If you have noticed the command this time we have used options -r to transfer directory.

Whenever you want to transfer directory or folder then use the options -r to with SCP command, Otherwise, you will not able to transfer directory with the error “SCPDEMO: not a regular file”.

Read this:- How to save the command output in Linux Terminal?

How to Copy Files and Directories using using SCP between remote to local system

We will Demonstrate both file and Directory copy from the remote and local server.

File transfer using SCP

In this demo guide we will show you how to copy a file from a remote server to the local system using the below command:

$ scp [email protected]:/home/shen/notes . 
File able to get from remote server to local
File able to get from remote server to local

Now, I’ll explain to you how this command work. First, we have use SCP command without any options after that type remote username and hostname in my case my remote details[email protected] after that use : (colon) this will identify shen is local or remote user after that you need to pass the location from where you are file is located, and the last we have use . (Point) to store the file in the local system.

Directories transfer using SCP

Above we have seen how to transfer file now we will show you how to copy directory from remote server to local system using the below command:

$ scp -r [email protected]:/home/shen/SCPDEMO ./
Directory able to get from remote server to local
Directory able to get from remote server to local

We are able to copy directories or folder from remote server to local server. If you have noticed the command this time we have used options -r to transfer directory.

If you don’t use the parameter -r then you will not able to copy the file and you will get this kind of error “scp: /home/shen/SCPDEMO: not a regular file”.

Few Points to note

If you are not able to transfer file and your getting some error check the few things like

  • OpenSSH is installed in both systems.
  • Port no 22 is allowed in the firewall
  • Read and Write is allowed

That’s it to transfer the file using SCP command utility, If you are facing any issue while following guide then let me know in the comment section.

Like what you read? Please share it with others.
  • 1
    Share
guest
0 Comments
Inline Feedbacks
View all comments