Banner

If you're tired of worrying about viruses, want more control over how your computer works or tired of paying for software that should be free it's time to dump Microsoft and switch to Linux.

Need to chat live with me for tips and tricks with your computer or crafts? Click the LIVE CHAT menu link above. Check out the all new accessibility feature. Look for this icon in the bottom left corner

How to Transfer Files (Local and Remote) in Linux

How to Transfer Files (Local and Remote) in Linux


Transferring files in Linux is a common task that every system administrator should know, especially when transferring files between local or remote systems across networks.

Linux offers two popular tools for this purpose: SCP (Secure Copy) and Rsync. Both provide a secure and convenient way to transfer files between local or remote machines.

In this article, we’ll explore how to use the SCP and Rsync commands to transfer files, covering both local and remote file transfers.

1. Understanding scp (Secure Copy Protocol) in Linux

The scp command is a command-line program used to securely copy files and directories between two hosts using SSH (Secure Shell), which means the data is transferred in an encrypted manner during file transfer over the internet.

The basic general syntax for using scp is as follows:

Copy Code
scp [options] source_file destination

Here is the explanation of the command:

  • source_file: The file or directory you want to copy.
  • destination: The location where you want to copy the file or directory.

Copying Files Locally in Linux

If you want to copy a file from one directory to another on the same machine, you can use:

Copy Code
scp ~/document.txt /tmp

Copying Files to a Remote Server

To copy files from your local machine to a remote server, you’ll need to mention the remote server’s IP address and the destination path.

Copy Code
scp ~/document.txt username@192.168.1.100:/home/username

Copying Files from a Remote Server

To copy a file from a remote server to your local machine, simply reverse the source and destination:

Copy Code
scp username@192.168.1.100:/home/username/document.txt /tmp

To copy an entire directory, use the -r option (which stands for recursive):

Copy Code
scp -r ~/tecmint /tmp

2. Understanding rsync in Linux

The rsync command is another most widely used file transfer command-line tool that is used for transferring files and directories between the source and the destination, which is commonly used for synchronizing directories.

The basic syntax for using rsync is:

Copy Code
rsync [options] source destination

Transfer Files Locally

To transfer files or directories locally, you can use rsync with the -a option to enable archive mode, which preserves permissions, ownership, and timestamps.
The -v option stands for verbose, which shows the progress of the file transfer.

Copy Code
rsync -av ~/myfolder /backup

Transfer Files to a Remote Server

To transfer files to a remote server, rsync uses SSH by default, here you need to specify the remote server IP address and location:

Copy Code
rsync -av ~/myfolder username@192.168.1.100:/home/username

The above command will transfer myfolder to the remote server, and if you run it again, rsync will only copy the files that have changed.

Transfer Files from a Remote Server

To transfer files from a remote server to your local machine, use:

Copy Code
rsync -av username@192.168.1.100:/home/username/myfolder /backup

Synchronizing Directories with Rsync

One of the key strengths of rsync is its ability to synchronize directories, which means you can keep two directories in sync, whether they are on the same machine or on different machines.

For example, to synchronize a local directory with a remote directory:

Copy Code
rsync -av /local/directory/ username@remote_host:/remote/directory/

To synchronize a remote directory with a local directory, you would reverse the source and destination in the rsync command.

Copy Code
rsync -av username@remote_host:/remote/directory/ /local/directory/

If a transfer is interrupted due to a network failure or other reasons, you can resume it using the --partial option, which allows to resume transferring files from where it left off:

Copy Code
rsync -av --partial /path/to/source username@remote_host:/path/to/destination

Conclusion

Transferring files in Linux is a straightforward task with tools like scp and rsync. While scp is simple and secure, rsync offers more advanced features like synchronization and efficiency.

Understanding how to use these tools effectively will help you manage your files and data across local and remote systems.

No comments:

Post a Comment