How do I copy a folder or file from remote to local host using scp
?
I use ssh
to log in to my server.
Then, I would like to copy the remote folder foo
to local /home/user/Desktop
.
How do I achieve this?
scp -r [email protected]:/path/to/foo /home/user/Desktop/
By not including the trailing '/' at the end of foo, you will copy the directory itself (including contents), rather than only the contents of the directory.
From man scp
(See online manual)
-r Recursively copy entire directories
-C
flag adds compression and the -c
flag lets you pass in other cipher types for better performance, like scp -c blowfish a@b:something .
as seen in dimuthu's answer
Commented
Jun 26, 2014 at 20:48
Desktop/foo
or will you have Desktop/allcontentsofFooGohere
scp seems to act weird sometimes to me it does one thing then another
scp -r [email protected]:/path/to/foo /home/user/Desktop/
you should end up with Desktop/foo
. With scp -r [email protected]:/path/to/foo/. /home/user/Desktop/
you will end up with the contents of foo
in Desktop
and all the sub-dirs of foo
strewn under Desktop
To use full power of scp you need to go through next steps:
Then, for example if you have this ~/.ssh/config:
Host test
User testuser
HostName test-site.example
Port 22022
Host prod
User produser
HostName production-site.example
Port 22022
you'll save yourself from password entry and simplify scp syntax like this:
scp -r prod:/path/foo /home/user/Desktop # copy to local
scp -r prod:/path/foo test:/tmp # copy from remote prod to remote test
More over, you will be able to use remote path-completion:
scp test:/var/log/ # press tab twice
Display all 151 possibilities? (y or n)
For enabling remote bash-completion you need to have bash-shell on both <source>
and <target>
hosts, and properly working bash-completion. For more information see related questions:
How to enable autocompletion for remote paths when using scp?
SCP filename tab completion
To copy all from Local Location to Remote Location (Upload)
scp -r /path/from/local username@hostname:/path/to/remote
To copy all from Remote Location to Local Location (Download)
scp -r username@hostname:/path/from/remote /path/to/local
Custom Port where xxxx
is custom port number
scp -r -P xxxx username@hostname:/path/from/remote /path/to/local
Copy on current directory from Remote to Local
scp -r username@hostname:/path/from/remote .
Help:
-r
Recursively copy all directories and files/
, Get full location/path by pwd
scp
will replace all existing fileshostname
will be hostname or IP address-P PortNumber
Note: Sometimes the custom port will not work due to the port not being allowed in the firewall, so make sure that custom port is allowed in the firewall for incoming and outgoing connection
What I always use is:
scp -r username@IP:/path/to/server/source/folder/ .
. (dot): it means current folder
. so copy from server and paste here only.
IP: can be an IP address like 125.55.41.311
or it can be host like ns1.mysite.example
.
.
refers to current directory!
Better to first compress catalog on remote server:
tar czfP backup.tar.gz /path/to/catalog
Secondly, download from remote:
scp [email protected]:/path/to/backup.tar.gz .
At the end, extract the files:
tar -xzvf backup.tar.gz
-C
flag, like scp -C a@b:bigfile .
.
Commented
Jun 26, 2014 at 20:43
Typical scenario,
scp -r -P port username@ip:/path-to-folder .
explained with an sample,
scp -r -P 27000 [email protected]:/tmp/hotel_dump .
where,
port = 27000
username = "abc" , remote server username
path-to-folder = tmp/hotel_dump
. = current local directory
And if you have one hell of a files to download from the remote location and if you don't much care about security, try changing the scp default encryption (Triple-DES) to something like 'blowfish'.
This will reduce file copying time drastically.
scp -c blowfish -r [email protected]:/path/to/foo /home/user/Desktop/
arcfour
in stead of blowfish
, but it has security flaws.
Commented
Jun 26, 2014 at 20:42
Go to Files on your unity toolbar
Press Ctrl + l and write [email protected]
The 192.168.1.103 is the host that you want to connect.
The here one example
In case you run into "Too many authentication failures", specify the exact SSH key you have added to your severs ssh server:
scp -r -i /path/to/local/key [email protected]:/path/to/folder /your/local/target/dir
The question was how to copy a folder from remote to local with scp
command.
$ scp -r userRemote@remoteIp:/path/remoteDir /path/localDir
But here is the better way for do it with sftp
- SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream.(wikipedia).
$ sftp user_remote@remote_ip
sftp> cd /path/to/remoteDir
sftp> get -r remoteDir
Fetching /path/to/remoteDir to localDir 100% 398 0.4KB/s 00:00
For help about sftp
command just type help
or ?
.
I don't know why but I was had to use local folder before source server directive . to make it work
scp -r . [email protected]:/usr/share/nginx/www/example.org/
root@
It is better to use rsync
than scp
- because of ease-of use, while both are available on most Linux platforms
Simple one liner :
rsync -aP myfiles/ [email protected]:server_dir/
a
to copy recursively, preserves symbolic links, special and device files, modification times, groups, owners, and permissions. It’s more commonly used than -r
and is the recommended flag to use.
P
to show progress
General form :
rsync -a ~/dir1 username@remote_host:destination_directory
With root
as user, and to a specific IP address.
rsync -aP myfiles/ [email protected]:server_dir/
You can refer to digitalocean guide for Rsync
rsync -ap . [email protected]:server_dir/
rsync -aPI source/ remote:destination/
-a
for recursive, -P
for progress, and -I
for overriding existing files. The command will show to the progress for every file, which is much clearer than scp
.
For Windows OS, we used this command.
pscp -r -P 22 hostname@IP:/path/to/Downloads ./
The premise of the question is incorrect. The idea is, once logged into ssh, how to move files from the logged-in machine back to the client that is logged in. However, scp is not aware of nor can it use the ssh connection. It is making its own connections. So the simple solution is create a new terminal window on the local workstation, and run scp that transfers files from the remote server to local machine. E.g., scp -i key user@remote:/remote-dir/remote-file /local-dir/local-file
By including -r
option in scp
command will solve the problem of /path-to-source/: not a regular file
but it will flood your terminal with all the progress while copying the files.
Using the -q
option along with -r
will resolve the issue without flooding your terminal with the progress updates during file copying.
These options enable recursive directory in quite mode.
snippet of command:
scp -rq /path-of-source/ /path-of-destination/
I invested few hours to find out that we have to run the scp
command on the system I want the copy.
So, suppose you have a VM in Azure and you want a particular file/folder from that VM to your localmachine (example a mac).
run this command on your local terminal: local terminal means normal terminal that you opens in your local system (for windows -> cmd, and for mac -> normal Ternimal that we open), not the remote terminal of the VM
To Download Folder:
scp -r adminCC@mystorage.<<remaining_url>>.azure.com:~/python Downloads/
To Download File:
scp adminCC@mystorage.<<remaining_url>>.azure.com:~/python/testfilenames.py Downloads/
Explaining the important parts:
adminCC
-> UserName for your VM (in my case username of my AzureVM, which I use to login to my VM using ternimal)mystorage.<<remaining_url>>.azure.com
-> this is the hostName. For Azure, your DNS name
is your hostName.~/python/testfilenames.py
-> path of the file in your server. ( this is how you can get the path -> open your VM in new remote terminal, type ll or ls -l
, and keep on navigating till you reach your file, use ~ before the complete path.)Downloads/
-> destination. like for mac. this is my Download folder.One logged into a remote machine, if you want to scp back to the client, you need to be running an SSH server on the client.
But really, once you are aware of the folder structure on Machine B, you should be able to "pull" the folder down.
OR...
Just open another terminal on the client..... and pull it down. You can have concurrent logons...
mc
:TAB
,cd sh://USER@HOST
, use the mc shortcuts,cd
out when done.mc
it's easier to use Left/Right on the menu > Shell link where you can type the alias you have in your~/.ssh/config
e.g.myhost:
> OK