Skip to content
Bots!
Bots!
  • About
    • Myself
    • আমার দোয়া
  • Bookmarks
    • Bookmarks
    • My OCI Bookmarks
    • Useful Proxmox Commands & Links
    • Learning Nano
    • Useful Sites
    • Useful Virtualbox Command
    • Useful MySQL Command
    • Useful Linux Command
    • BTT-CAS
  • Resources
    • Webinar on Cloud Adoption for Project Managers
  • Photos
  • Videos
  • Downloads
Bots!

SCP Command Syntax

Rumi, March 14, 2019

Before going into how to use the scp command, let’s start by reviewing the basic syntax. The scp utility expressions take the following form:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

OPTION – scp options such as cipher, ssh configuration, ssh port, limit, recursive copy ..etc
[user@]SRC_HOST:]file1 – Source file.
[user@]DEST_HOST:]file2 – Destination file
Local file should be specified using an absolute or relative path while remote file names should include a user and host specification.

scp provides a number of options that control every aspect of its behavior. The most widely used options are:

-P Specifies the remote host ssh port.
-p Preserves files modification and access times.
-q Use this option if you want to suppress the progress meter and non-error messages.
-C. This option will force scp to compresses the data as it is sent to the destination machine.
-r This option will tell scp to recursively copy directories.
Before you Begin
The scp command relies on ssh for data transfer, so it requires an ssh key or password to authenticate on the remote systems.

The colon(:) is how scp distinguish between a local and a remote locations.

To be able to copy files you must have at least read permissions on the source file and write permission on the target system.

Be careful when copying files that share the same name and location on both systems, scp will overwrite files without a warning.

When transferring large files it is recommended to run the scp command inside a screen or tmux session.

Copy Files and Directories Between Two Systems with SCP
Copy a Local File to a Remote System with the scp Command
To copy a file from a local to remote system run the following command:

scp file.txt remote_username@10.10.0.2:/remote/directory

Here, file.txt is the name of the file we want to copy, remote_username is the user on the remote server, 10.10.0.2 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to, if you don’t specify a remote directory, the file will be copied to the remote user home directory.

You will be prompted to enter the user password and the transfer process will start.

remote_username@10.10.0.2's password:
file.txt 100% 0 0.0KB/s 00:00

Omitting the filename from the destination location copies the file with the original name. If you want to save the file under a different name you need to specify a new name:

scp file.txt remote_username@10.10.0.2:/remote/directory/newfilename.txt

If SSH on the remote host is listening on a port other than the default 22 then you can specify the port using the -P argument:

ssh -P 2322 file.txt remote_username@10.10.0.2:/remote/directory

The command to copy a directory is much like as when copying files. The only difference is that you need to use the -r flag for recursive.

To copy a directory from a local to remote system use the -r option:

scp -r /local/directory remote_username@10.10.0.2:/remote/directory

Copy a Remote File to a Local System using the scp Command
To copy a file from a remote to local system, use the remote location as source and local location as destination.

For example to copy a file named file.txt from a remote server with ip 10.10.0.2 run the following command:

scp remote_username@10.10.0.2:/remote/file.txt /local/directory

If you haven’t set a passwordless SSH login to the remote machine, you will be asked to enter the user password.

Copy a File Between Two Remote Systems using the scp Command
Unlike rsync, when using scp you don’t have to login to one of the server to transfer files from one to another remote machine.

The following command will copy the file /files/file.txt from the remote host host1.com to the directory /files on the remote host host2.com.

scp user1@host1.com:/files/file.txt user2@host2.com:/files

You will be prompted to enter the passwords for both remote accounts. The data will be transfer directly from one remote host to the other.

To route the traffic through the machine on which the command is issued use the -3 option:

scp -3 user1@host1.com:/files/file.txt user2@host2.com:/files

Src: https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/

Collected Articles SSH

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Myself…

Hi, I am Hasan T. Emdad Rumi, an IT Project Manager & Consultant, Virtualization & Cloud Savvyfrom Dhaka, Bangladesh. I have prior experience in managing numerous local and international projects in the area of Telco VAS & NMC, National Data Center & PKI Naitonal Root and CA Infrastructure. Also engaged with several Offshore Software Development Team.

Worked with Orascom Telecom-Banglalink, Network Elites as VAS partner, BTRC, BTT (Turkey) , Mango Teleservices Limited and Access to Informaiton (A2I-UNDP)

Currently working at Oracle Corporation as Principal Technology Solution and Cloud Architect.

You can reach me [h.t.emdad at gmail.com] and I will be delighted to exchange my views.

Tags

Apache Bind Cacti CentOS CentOS 6 CentOS 7 Debain Debian Debian 10 Debian 11 Debian 12 DKIM Docker endian icinga iptables Jitsi LAMP Letsencrypt Linux Munin MySQL Nagios Nextcloud NFS nginx pfsense php Postfix powerdns Proxmox RDP squid SSH SSL Ubuntu Ubuntu 16 Ubuntu 18 Ubuntu 20 Varnish virtualbox vpn Webmin XCP-NG zimbra

Topics

Recent Posts

  • Install Jitsi on Ubuntu 22.04 / 22.10 April 30, 2025
  • Key Lessons in life April 26, 2025
  • Create Proxmox Backup Server (PBS) on Debian 12 April 19, 2025
  • Add Physical Drive in Proxmox VM Guest April 19, 2025
  • Mount a drive permanently with fstab in Linux April 16, 2025
  • Proxmox 1:1 NAT routing March 30, 2025
  • Installation steps of WSL – Windows Subsystem for Linux March 8, 2025
  • Enabling Nested Virtualization In Proxmox March 8, 2025
  • How to Modify/Change console/SSH login banner for Proxmox Virtual Environment (Proxmox VE / PVE) March 3, 2025
  • Install Proxmox Backup Server on Debian 12 February 12, 2025

Archives

Top Posts & Pages

  • Install Jitsi on Ubuntu 22.04 / 22.10
©2025 Bots! | WordPress Theme by SuperbThemes