rcp Command in Linux with examples
Last Updated :
16 Oct, 2024
When working in a Linux environment, there often comes a time when you need to transfer files from one computer to another. While more secure options like scp or rsync exist, the rcp (Remote Copy Protocol) command offers a simple and efficient way to copy files between systems, especially for beginners.
Here we will look into the rcp command, its syntax, options, and examples of how you can use it to transfer files between hosts.
Syntax
rcp [-p] [-r] file name ... directory
Where,
- -p preserves the modification times, access times, modes, and ACLs (if applicable) of the original file.
- -r allows the recursive copying of directories.
- source_file is the file or directory you want to copy.
- destination_directory is the target location where the files will be copied.
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).
Common Options for the rcp Command
Option |
Description |
-r |
Used to copy an entire directory, including its contents and subdirectories. |
-p |
Preserves the original file’s modification times, access times, modes, and ACLs (if applicable) during copy. |
file name |
Refers to the name of the file that you want to copy. |
directory |
Refers to the name of the directory where the file(s) should be copied. |
Examples of using rcp command
Example 1: 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.
Example 2: 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
Example 3: Using rcp to copy directories
The rcp allows you to copy directories also when used with -r option.
/*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.
Example 4: 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”.
Conclusion
The rcp command is a simple and easy-to-use tool for copying files and directories between local and remote systems. While it lacks the security features of modern alternatives like scp and rsync, it remains a helpful utility for Linux beginners working in a trusted network environment. With options like -p to preserve file attributes and -r to copy directories recursively, rcp provides a straightforward solution for file transfers.
Similar Reads
How to Change User Password in Linux | passwd Command
Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will
8 min read
Paste command in Linux with examples
Paste command is one of the useful commands in Unix or Linux operating system. It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or put dash ("-"
6 min read
pidof Command in Linux with Examples
pidof command is used to find out the process IDs of a specific running program. It is basically an identification number that is automatically assigned to each process when it is created. Syntax: pidof [options] program1 program2 ... programNWorking with Pidof Command 1. To find pid of any process
2 min read
How to Check Network Connectivity in Linux | ping Command
Ensuring a stable and reliable internet connection is crucial for seamless navigation and efficient communication in the world of Linux. The "ping" command is a powerful tool that allows users to check the status of their internet connection and diagnose network-related issues. In this article, we w
7 min read
Pinky command in Linux with Examples
Pinky command is a user information lookup command which gives details of all the users logged in. This tool is generally used by system administrators. It is similar to the finger tool but in many Linux, Systems pinky comes pre-installed while finger doesn't, so this command is useful in case you d
2 min read
pmap command in Linux with Examples
The pmap command in Linux is a powerful utility used to display the memory map of a process. A memory map provides insight into how memory is allocated and distributed within a running process. This can be incredibly useful for developers and system administrators when debugging memory issues, optim
4 min read
halt, poweroff and reboot Commands in Linux
In Linux systems, the halt, poweroff, and reboot commands are essential tools for controlling the system's hardware by stopping the CPU, shutting down the system, or restarting it. These commands are typically restricted to superusers, as they involve critical actions that impact the system hardware
4 min read
printf command in Linux with Examples
The 'printf' command in Linux is a versatile tool used to display formatted text, numbers, or other data types directly in the terminal. Like the 'printf' function in programming languages like C, the Linux printf command allows users to format output with great precision, making it ideal for script
4 min read
How to List Running Processes in Linux | ps Command
As we all know Linux is a multitasking and multi-user system. So, it allows multiple processes to operate simultaneously without interfering with each other. Process is one of the important fundamental concepts of the Linux OS. A process is an executing instance of a program that carries out differe
9 min read
How to Display Current Working Directory in Linux | pwd Command
The 'pwd,' which stands for "print working directory." In this article, we will delve into the 'pwd' command, exploring its functionality, usage, and various examples. It prints the path of the working directory, starting from the root. pwd is shell built-in command(pwd) or an actual binary(/bin/pwd
4 min read