scp Command for Secure File and Folder Transfer (original) (raw)

Last Updated : 2 May, 2026

The scp (Secure Copy Protocol) command is used to securely transfer files and directories between systems over a network. It works over SSH, which means all transferred data is encrypted. It is commonly used by system administrators to move backup files, fetch logs, copy configuration files, and transfer data between servers safely and quickly.

Example: Copy File from Local to Remote

To copy a file named file.txt from your local machine to a remote server

**Command:

scp file.txt username@192.168.1.2:/home/username/

Syntax

scp [options] [[user@]host1:]source_file_or_directory ... [[user@]host2:]destination

Commonly Used SCP Commands in Linux

1. Securely Copying Files from Local to Remote Machine

**Syntax:

scp [file_name] remoteuser@remotehost:/remote/directory

Here,

**Example:

Suppose we want to copy a file named test.txt from your local system to a remote machine with the following details:

**Command:

scp test.txt jayesh@10.143.90.2:/home/jayesh

copied file from local system to remote system

copied file from local system to remote system

File that we have copied

File that we have copied

2. Securely Copying Files From Remote Machine to Local Machine

To copy a file from a remote machine to your local system, you just reverse the source and destination, let's check out the syntax below:

**Syntax:

scp user@remotehost:/home/user/file_name

Here,

**Example:

Suppose the remote system details are as follows:

To copy this file from the remote machine to your current local directory, run:

**Command:

scp jayesh@10.143.90.2:/home/jayesh/test1.txt .

**Output:

copied file from remote system to local system using scp

copied file from remote system to local system using scp

**Note: If . is not specified, SCP will fail because destination is mandatory.

test1.txt successfully copied

test1.txt successfully copied

Important SCP Command Options

1. -P (Specify Custom SSH Port)

It is used to Securely Copy File to a Remote Machine on a Non-Standard SSH Port and specify the port to connect on the remote host. It is useful when our SSH server is listening on a non-standard port.

**Syntax:

scp -P port source_file user@hostname:destination_file

**Example:

Suppose you want to copy a file named test2.txt from your local system to a remote machine with the following details:

**Command:

scp -P 2222 test2.txt jayesh@10.143.90.2:/home/jayesh/

**Output:

copying file from local system to remote using -P option in scp

copying file from local system to remote using -P option in scp

test2.txt successfully copied

test2.txt successfully copied

2. -p (Preserve File Metadata)

This option is used when we want the original metadata of the file that has been transferred. Basically, it preserves modification time, access time, and modes from the original file.

**Syntax:

scp -p source_file user@hostname:destination_file

**Example:

Suppose you want to copy a file named test3.txt from your local system to a remote machine with the following details:

**Command:

scp -p test3.txt jayesh@10.143.90.2:/home/jayesh/

**Output:

copying file from local system to remote using -p option in scp

copying file from local system to remote using -p option in scp

3. -q (Quiet Mode)

It Securely Copy File with Quiet Mode - Disabling Progress Meter .This option hides the progress of the file transfer on the terminal.

**Syntax:

scp -q source_file user@hostname:destination_file

**Example:

Suppose you want to copy a file named test4.txt from your local system to a remote machine with the following details:

**Command:

scp -q test4.txt jayesh@10.143.90.2:/home/jayesh/

**Output:

As we can see there is no progress shown

As we can see there is no progress shown

test4.txt successfully copied

test4.txt successfully copied

4. -r (Copy Directory Recursively)

This option is used when we want to copy an entire directory and its contents. Which basically means copying entire directory recursively.

**Syntax:

scp -r Directory_name user@hostname:destination_file

**Example:

Suppose you want to copy a directory named new from your local system to a remote machine with the following details:

**Command:

scp -r new jayesh@10.143.90.2:/home/jayesh/new1/

**Output:

copying entire directory and its file recursively using `-r` in scp

copying entire directory and its file recursively using `-r` in scp

new directory successfully copied.

new directory successfully copied.

Other Secure File Transfer Alternatives

While SCP is useful for direct secure file transfers, there are other powerful tools available in Linux depending on your requirements.

1. SFTP (SSH File Transfer Protocol)

SFTP is a secure file transfer tool that works over SSH and provides an interactive session for managing remote files. It allows users to browse directories, upload and download files, rename files, and manage folders directly from the terminal. SFTP also supports resuming interrupted transfers, making it reliable for unstable connections.

**Example:

Connect to a remote system:

**Command:

sftp user@remotehost

After connecting, you can use:

put file_name
get file_name
ls
cd directory_name

Rsync (Remote Synchronization)

Rsync is a powerful file synchronization tool used for copying and syncing files or directories between systems. It transfers only the changed portions of files (delta transfer), which makes it efficient for backups and repeated transfers. Rsync also supports compression and resume functionality.

**Example:

Sync a local file to a remote system:

**Command:

rsync -avz file user@remotehost:/destination

Improve the Security of SCP File Transfer

You must follow these practices to ensure that you're performing a secure file transfer using the SCP:

Troubleshooting While Copying Files in Linux: SCP Command

While performing this action, there are certain challenges that you might face, let's address them for better clarity:

1. Permission Denied Error

If such issue persists, ensure that all the permissions to read/write to both source and destination (or directories) are correct. Here's how you can check it:

transferring

2. Issue While Transferring Files

Check if the source file exists and is accessible, also ensure to have sufficient storage disk space on the remote server so that the files can be stored.

3. Connection Timeout

Ensure that the remote server is online and reachable and in case of custom SSH port, always make sure that you're using the correct port. You may verify it by using the -P option.