scp Command in Linux

Topics Covered

Overview

The SCP command in Linux is a potent tool that enables users to securely copy files and directories between two locations. Derived from 'secure copy', the scp command in Linux utilizes SSH (Secure Shell) for data transfer, ensuring both security and reliability. The command is instrumental in scenarios where you need to transfer files between different machines in a network, or even different directories on the same machine.

Syntax of scp Command in Linux

The basic syntax of the scp command in Linux is as follows:

Where:

  • [options]: These are optional flags that modify the behavior of the scp command.
  • [user@]source_host:source_file: This specifies the source file location. If a user and host are provided, the command will attempt to copy the file from the specified host. If omitted, the command will look for the source file on the local machine.
  • [user@]destination_host:destination_file: This specifies the destination file location. If a user and host are provided, the command will attempt to copy the file to the specified host. If omitted, the command will copy the file to the local machine.

Options in scp Command in Linux

  1. -p: Preserves the modification and access times, as well as the permissions of the source file.

    For example -

    This command will copy the source file to the destination, preserving the file's permissions and timestamps.

  2. -r: Recursively copies entire directories.

    For example -

    This command will copy the entire source directory and its contents to the destination.

  3. -v: Displays debugging messages. Useful for troubleshooting.

    For example -

    This command will display detailed information about the scp process, useful for diagnosing problems.

Example Usages

  • Copying a file from a remote host to the local machine:

    Output:

    Explanation: This command copies 'file.txt' from the remote host to the current directory on the local machine.

  • Copying a directory from the local machine to a remote host:

    Explanation: This command copies the entire 'local_directory' and its contents to 'destination_directory' on the remote host.

Tips

  • When copying files to or from a remote host, ensure you have the necessary permissions to read or write to the specified locations.

  • Use the -C option to enable compression, which can significantly speed up file transfers over slower network connections.

  • Use the -q option to run the scp command in quiet mode, suppressing progress meter and warning messages.

Advanced Use Cases of scp Command in Linux

  • Copying a file between two remote hosts from a local host:

    Explanation: This command copies 'file.txt' from 'remote_host1' to 'remote_host2'. The operation is initiated from the local host.

  • Limiting the bandwidth used by scp:

    Explanation: This command limits the bandwidth consumed by the scp command to 400 Kbit/s, reducing the impact on other network operations.

  • Using a different ssh port:

    Explanation: This command uses port 2222 for the ssh connection instead of the default port 22.

Conclusion

  • The scp command in Linux is a powerful tool for secure file and directory transfer.

  • The scp command utilizes SSH for data transfer, offering security and reliability.

  • The scp command provides several options to control the copying process, including preserving file attributes, recursive copy, and bandwidth limitation.