Copy File From Remote To Local Using Scp

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: January 31, 2024

The short answer

To copy and download a file from a remote server to your local machine, you can use the scp (secure copy) in the following way:

Bash
$ scp <user>@<host>:<source> <destination>

Where:

  • user is your username on the remote server.
  • host is the IP address or the hostname of the remote server.
  • source is the path of the file you want to copy on the remote server.
  • destination is the destination path of the file you want to copy on your local machine.

When executing this command, you will be prompted to enter the password of the specified user account on the remote server.

For example, the following command will attempt to copy the index.js file located in the /home/john/app directory on the remote server to your home directory on your local machine using the johndoe account.

Bash
$ scp johndoe@127.0.0.1:/home/johndoe/app/index.js ~/index.js

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering copy file from remote server to local machine in the AI Command Search will suggest you the correct scp command, which you can then quickly insert into your shell by doing CMD+ENTER.

scp multiple files at once

To copy multiple files located in the same directory on the remote server at once, you can either use a wildcard pattern:

Bash
$ scp <user>@<host>:/path/to/directory/* /path/to/local/directory

Or a brace expansion:

Bash
$ scp <user>@<host>:/path/to/directory/{file_1,file_2} 
 /path/to/local/directory

scp an entire folder from remote to local

To copy an entire folder and all of its content including subdirectories, you can use the -r flag (short for recursive):

Bash
$ scp -r <user>@<host>:/path/to/directory /path/to/local/directory

Note that when using this command, the source directory will be copied from the remote server within the specified destination directory on the local machine.

For example:

Bash
$ scp -r johndoe@127.0.0.1:/home/johndoe/app ~/Projects
 $ ls ~/Projects
 app

scp to your local machine while being connected to a remote machine via ssh

Although it is unusual, the scp command can be used to copy files from a remote server to the local machine whilst being connected to the remote server.

To do so, you must first connect to the remote server using the ssh command:

Bash
$ ssh <user>@<host>

Where:

  • user is your username on the remote server.
  • host is the IP address or the hostname of the remote server.

Which will prompt you to enter the password of the specified user account on the remote server.

Once connected, you can use the following syntax:

Bash
$ scp /path/to/file ${SSH_CLIENT%% *}:/path/to/local/file

Where:

  • ${SSH\_CLIENT%% \} will be replaced by the IP address of the client connected to the remote server (i.e. your machine) using a parameter substitution symbolized by the ${} expression.

Note that for this to work, you will need to run an SSH server on your local machine and set up an SSH account in order to allow remote connections.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.