Saturday, 28 December 2019

rcp Command in Linux with examples

There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way and a LINUX beginner can use this command to copy files from one computer to another computer.

rcp Command, Linux Tutorial and Materials, Linux Study Materials

Here’s the syntax of rcp command:

// syntax of rcp command

rcp [-p] [-r] file name ... directory


Using rcp command


To simply use the rcp command, just provide the source and destination to rcp command with a colon used to separate the host and the data.

/* using rcp command
to send a file from local
host to remote host */

rcp /mydirectory/kt.txt kartik:one/kt.txt

/* the example
above is to send a file
not to receive a file
from remote host */

What actually happening in the above example is the file named kt.txt whose path is given as /mydirectory/kt.txt is getting transferred from this local path (/mydirectory) that is you can say from a local host to the remote system named kartik and the file on that system will be placed in the directory one(as path one/kt.txt is given).

Options for rcp command


◈ -r : This option is used when there is a need to cpy an entire directory.
◈ -p : This option allows the copy to have the modification times, access times, modes and ACLs if applicable as the original file.
◈ file name : refers to the name of the file.
◈ directory : refers to the name of directory

Examples of using rcp command


◈ Using rcp to receive a file from remote host to local host : In the above example we learnt how to use rcp command to send a file from local host to a remote host. We can use the same rcp command to receive a file from a remote host to a local host like shown below:

/*using rcp command
to receive a file from
a remote host */

rcp kartik:one/kt.txt .

/*the difference in the
syntax of receiving
is just of not using
the source path before
'kartik' i.e the name of
remote system */

The above will transfer a file named kt.txt in one directory from a remote host named kartik to the local host. The . (dot) used at the end is for placing the file kt.txt in the current directory of the local host, obviously you can provide a path of your own choice instead of a dot that is here representing the current directory.

◈ Using rcp with -p option : The rcp command like cp changes the modification time of the destination file to the late time. So, in order to retain the same modification time -p option is used.

//using rcp with -p option

rcp -p kartik:one/kt.txt

◈ Using rcp to copy directories : The rcp allows you to copy directories also when used with -r option.

rcp Command, Linux Tutorial and Materials, Linux Study Materials

/*using -r option
with rcp */

rcp -r localdir kartik:

The above will copy the entire directory localdir along with it sub directories to the HOME directory of remote host named kartik.

◈ Using rcp to copy two files together : this can be done simply just giving the names of two files together. For the sake of simplicity, we are using rcp for transferring the files from a local host to a remote host.

/*using rcp to copy
two files from local
host to remote host */

rcp kt.txt pt.txt kartik:/var/docs

The above will copy the files kt.txt and pt.txt from a local host (no path is specified cause in this case it is assumed that these files are placed in the current directory) to a remote host named kartik in /var/docs.

Note : The rcp command can only be used when both the computers have a .rhosts file in the user’s home directory.

Related Posts

0 comments:

Post a Comment