How to Copy a Local file to a Remote System with the SCP Command

April 10, 2024 / How-to Guide

This article will illustrate how to copy a local file to a remote system with the SCP command.

Follow the steps:

  1. Execute the following command to copy a file from a local to a remote system-
    scp file.txt [email protected]:/remote/directory
  2. If you want to copy a file named file.txt to a remote server, follow these steps-
    1. Replace “file.txt” with the name of your file.
    2. Use “remote_username” as the user on the remote server.
    3. “10.10.0.2” is the server’s IP address.
    4. “/remote/directory” is the path to where you want to copy the file.
    5. If no remote directory is specified, the file will be copied to the user’s home directory. After entering the command, you will be asked for the user password, and then the file transfer will begin.
      [email protected]’s password:
      file.txt                             100%    0     0.0KB/s   00:00
  3. If you do not include the filename in the destination, the file will be copied with its original name. To save it with a different name, you must specify the new filename.
    scp file.txt [email protected]:/remote/directory/newfilename.txt
  4. If the SSH on the remote host uses a port other than the default 22, you can indicate the port by using the -P argument.
    scp -P 2322 file.txt [email protected]:/remote/directory
  5. To copy a directory and all its files, simply use the scp command with the -r flag, which recursively copies the entire directory along with its contents.
    scp -r /local/directory [email protected]:/remote/directory
  6. When utilising wildcards like * or? to match particular files or directories, it’s important to prevent shell expansion by enclosing the path containing wildcard characters in quotes (“ ”) or single quotes (‘ ’). In the following instance, we are copying all .txt files from the local Projects directory to the Projects directory on the remote server.
    scp “~Projects/*.txt” [email protected]:/home/user/Projects/
  7. If you wish to maintain the file metadata, which includes details like ownership and creation date, pass the -p option. For instance, to copy file.txt to the remote directory while preserving the metadata, use the following command-
    scp -p file.txt [email protected]:/remote/directory/

In this manner, you can copy a local file to a remote system with the SCP command. If you run into any difficulty, our support experts will help you out. They are available for you 24X7.

Spread the love