Use scp to transfer a file from local directory X to remote directory Y
I took a look around at other questions here and at various “scp usage” tutorials on Internet, but I can’t sort out what’s wrong.
I’m using Linux Mint and I’m trying to figure out how scp works.
I’ve a file file.ext (on my computer) in directory /home/name/dir/
I connect to a remote machine using ssh, like:
ssh -p 2222 username@domain
it asks me the password and the shell displays:
username@domain ~ $
now, If I issue the command (before I ran ssh I was in the local directory /home/name/dir ):
scp -r -P 2222 file.ext username@domain
cp: cannot stat ‘file.ext’: No such file or directory
Same result if instead of file.ext I write the complete path
scp -r -P 2222 /home/name/dir/file.ext username@domain
Also, the server admin told me that I shall upload the file to my remote home directory (instead of root), like:
scp -r -P 2222 file.ext username@domain:~/
but when I do it and press “Enter” nothing happens, as If the shell was waiting for further input.
Summary of my problems:
- cp: no such file or directory
- shell “stuck” on ~/
If you’re running this scp command on the remote machine, it is looking for
file.ext as a “local” file, i.e. on the remote machine.
To copy a file from the remote machine to the local one, use
scp -P 2222 username@domain:dir/file.ext localdir (assuming that
file.ext is in
~/dir on the remote computer, as in your example). If you run
scp on the remote machine, reverse “local” and “remote”.
You need to run the
scp command from the local machine, not on the remote. You don’t need the
ssh at all:
dragonmnl@local $ scp -P 2222 file.ext username@domain:~/
You also don’t need the
-r Recursively copy entire directories.
If you are already logged into the remote machine and want to copy from your local, you need to make sure that your local machine is accessible via the internet and has
ssh set up. I don’t think this is what you are after but if it is, just run this from the remote:
username@domain $ scp dragonmnl@local:/path/to/file.ext ~/
If you happen to be on OSX…
When I tried running it with username@local:/file_directory…
I got the following error:
ssh: local: node name or service name not known
On my Mac I had to do the following to setup local
‘System Preferences’ –> ‘Internet & Networking’ –> ‘Sharing’ –> ENABLE ‘Remote Login’
This then provided the username@ip address.
I provide two methods of up/down load file between remote and local machine(I use Mac air):
In this case, I want to up/down
11.jpeg between local and remote:
Copy files on service to local dir: You must be in local bash terminal to conduct this command, not when you are in ssh!
scp username@domain:/home/xxx/xxx/11.jpeg /Users/username/Desktop/
copy files in local dir to remote service : also you must be in local bash terminal
scp /Users/username/Desktop/11.jpeg username@domain:/home/xxx/xxx
To achieve the same intention when you are logging in the SSH, you must first set “System Preferences>sharing>remote log in>all users(I am not sure if you must set for "all users", but it works in this situation)” the Mac will tell you"To log in to this computer remotely, type:
ssh username@xxxxxx, then type in the command below: scp username@domain:/home/xxx/xxx/11.jpeg username@xxxxxx:/Users/username/Desktop/**
This command above is for downloading file from remote to local when you are logging into ssh, Just change the two path when you want to upload file .
Solution to copy files from Linux client to Linux server
sshdis running on your server using:
ps aux | grep sshd
If not, install
sudo apt-get install openssh-server openssh-client
Restart your server and verify that
sshdis running using
ps aux | grep sshd
If you need you can configure your
but it’s not
necessary for simple configurations
verify that the ports are open using
nc -v -z 127.0.0.1 22
To copy files from a
Linuxclient to a
Linuxclient use scp as follows
scp ~/filename.md email@example.com:~/folder/filename.md
To verify that file has been copied use
ssh firstname.lastname@example.org provide your password.
Solution to copy files from Linux client to Windows
There are multiple clients for running SSH servers on Windows, such as:
Install one of the above servers and make sure that port 22 is open using
netsh advfirewall firewall add rule name="SSH Port 22" dir=in action=allow protocol=UDP localport=22 netsh advfirewall firewall add rule name="SSH Port 22" dir=out action=allow protocol=UDP localport=22
netsh advfirewall firewall add rule name="SSH Port 22" dir=in action=allow protocol=TCP localport=22 netsh advfirewall firewall add rule name="SSH Port 22" dir=out action=allow protocol=TCP localport=22
netsh advfirewall set allprofiles state off
Comparisons of these servers:
Bitvise SSH server
Provides almost close to native
Provides additional capabilities for customization, if you need
User customization for simple needs is 0. I got started on it within
2 minsute, after having spent a lot of time trying to get
To copy files using scp use:
scp ~/filename.md email@example.com:/C/Users/username/filename.md
Free for personal use, $99 for professional use. Because of so many
features and great integration, the price is well worth the money.
provides a Linux shell on Windows where simple Linux commands such
find, etc., work, similar to
you can access
powershelldirectly by typing
Unicode support is poor. Encoding is not correct. For example if you
try to start
Pythoninterpretor, it will crash. You will need to
change the encoding manually using
and then startup Python prompt
The great thing about MobaSSH is that it has a very simple
sshand integrated well. So will
work for simple use cases
To copy files from a Linux client to a Windows server running
scp ~/filename.md firstname.lastname@example.org:/cygdrive/c/Users/username/filename.md
This is because
MobaSSHis based on Cygwin and its drives are
mapped to the same way
cygwin-s drives are mapped.
Use VMs from Microsoft with OpenSSHv6.7 installed from
OpenSSHv6.7 is already installed.
It’s very buggy, and you will need to heavily customize it.
Command completion doesn’t work
It will leave blank spaces when you are moving cursor on the terminal
Python doesn’t work
Later versions of OpenSSH, may have improved on it. Further, MS seems to
make OpenSSH available via "Optional Features"
I had this same problem today on MAC.
Type the following command:
scp /local-file-path/ /-remote-location-path/
We can also use
scp -r source_folder destination_folder if not too many files in the folder.
If you want to transfer a folder, just
zip it first, we can
unzip it later on.
From your computer, run this:
$ scp your_path_to_the_file/the_file email@example.com:~/
root is your account, and
10.145.198.100 is the remote server’s IP address. We’re going to copy
~/ folder in the remote.
$ unzip the_zip_file.zip -d destination_folder
Just a small contribution, maybe it can help beginners like me: the path to the the remote directory should be written like this:
What I want to highlight here is that we should always add that