143

I ssh-ed into my webhost's directory, and tar-ed the webapp to download. When I try to mv to ~/mydirectory/backups or /home/mydirectory/backups, it defines the "home" as my root on the webhost that I'm ssh'ed into.

How do I mv in ssh to a local drive while still being inside the webhost's system?

1
  • What OS are you connecting from?
    – Zoredache
    Jun 29, 2012 at 0:15

7 Answers 7

198

First things first: ssh is a way to remotely login to another computer. The shell (command line) you get after you ssh is (pretty much) the same as if you had opened a xterm in the remote machine. If offers no such way to move files.

However, the fact that the remote computer accepts ssh connections gives you some options to exchange files:

Use scp To copy from your local computer to the remote, type, in the local computer:

scp /tmp/file [email protected]:/home/name/dir

(where /tmp/file can be replaced with any local file and /home/name/dir with any remote directory)

To copy from the remote computer to the local one, type, in the local computer:

scp [email protected]:/home/name/dir/file /tmp

Use sshfs This is a little more advanced but much, much nicer (when the internet connection of both computers is good. If not, stick to scp)

You can "link" a directory from the remote computer to an (empty) directory of the local computer. Say you "link" the /some/remote/dir from the remote computer to /home/youruser/remotecomp in your computer. If there is a file /some/remote/dir/file in the remote computer, you can see it on /home/youruser/remotecomp/file. You can copy and mv as usual, and you can even alter remote files and dirs.

Note however, that when the connection ends, /home/youruser/remotecomp becomes an empty dir again, and you only keep in the local computer the files you copied to other directories

To achieve this:

  1. install sshfs:

sudo apt-get install sshfs

  1. create a empty dir

mkdir /home/youruser/remotecomp

  1. "link" the two directories (the right term is mount)

sshfs [email protected]:/some/remote/dir /home/youruser/remotecomp

  1. Enjoy

  2. "unlink" the dirs

fusermount -u /home/youruser/remotecomp

If the local computer runs windows You can find versions of scp for windows. See, e.g, winscp

Rsync rsync is a utility to copy files that can:

  • resume transfers
  • redo a transfer, but only send the files that changed.

To copy a single file, you can use rsync -P -e ssh /tmp/file [email protected]:/home/name/dir

To keep a directory in sync, sending only the needed files, you can use rsync -avzh /home/yourname/dir_name [email protected]:/var/temp/

There are also many other options, including deleting files in the remote dir if they no longer exist in the local dir.

This option is a bit harder. For example, you can mess up with trailing slashes (note that, in the last command, dir_name had no trailing slash, but /var/temp/ had). So it is useful, but requires a bit of testing and/or man rsync

As always, commands are run in the local computer, not the remote one

5
  • 1
    Is there a way to copy the file from the remote machine to the local machine via "terminal on the remote machine"? In your case, you used the terminal on the local machine. I'm talking about a situation where local machine is windows and remote is ubuntu and I'm logged to the remote machine via ssh.
    – CKM
    Mar 23, 2017 at 8:13
  • Usually not. As far as I know, copying via "terminal in remote machine" means that you need an SSH server in the "local machine" (I purposefully skipped that during my answer, but "the fact that the remote computer accepts ssh connections" is meant to say "the fact that the remote computer is running an ssh server")
    – josinalvo
    May 30, 2017 at 14:30
  • However, you can find "scp clients" for windows. See, e.g, winscp.net/eng/download.php
    – josinalvo
    May 30, 2017 at 14:31
  • 1
    I love the sshfs option Mar 2, 2018 at 22:52
  • 1
    If you're transferring a large file, use rsync over ssh with rsync -P -e ssh /tmp/file [email protected]:/home/name/dir. This is because if a file transfer fails half way through, scp will just delete the file. On the other hand, if you use rsync with the -P option, it'll keep an incomplete file on the remote machine and when you restart the transfer, it'll make sure that what's already on the remote machine matches the file on your local machine and then continue where the transfer left off.
    – user677955
    Nov 2, 2018 at 23:10
17

You can either use scp or rsync. In your local system:

scp remoteuser@remotehost:/remote/dir/file /local/dir/

But, since you mentioned backup, I assume that it would be incremental and you'll need to do it every now and then. So, rsync is a better choice for incremental backup. On your local shell:

rsync -avz -e ssh remoteuser@remotehost:/remote/dir /local/dir/

see rsync(1)and scp(1) man pages for options.

10

You could use scp secure copy.

From you local shell:

scp -C username@webhost:/path/of-the/tar.archive /home/mydirectory/backups/

This example will copy via ssh from /path/of-the/tar.archive of your webhost to /home/mydirectory/backups/

Extra options:

-C: Enables compression

6

I had the same problem. Here's the easy solution:-

  1. Open nautilus (the file explorer)
  2. Click on + Other Locations at bottom of left panel.
  3. At the bottom there's a strip Connect to server. Write the ssh address in the input box such as ssh://111.222.333.444/.
  4. Enter your user name and password in the dialogue box that will open.
  5. Upon successful authentication you will be at home of remote location. Feel free to move file here and there.
1
  • Great answer! Simple and closest to an answer to the actual question, i.e. How do you mv from within the ssh remote computer. The answer is you can't, you do it from the local computer. Or this! You do it graphically from your explorer so that at least to the user it indeed seems like you can move files directly from remote to local.
    – Kvothe
    Jul 4, 2018 at 14:49
2

The other answers recommend rsync or scp, both of which require you to know the location of the file you want to copy on the remote machine.

If you instead want to be able to poke around on the remote machine, like you can with ssh, you want to run sftp. Logging into the server is very similar to ssh, but once you get in, type help to get the list of commands - it lets you move yourself around on both the local and remote machines, and transfer files back and forth easily.

2

It's strange to that it's impossible from inside ssh. In a regular terminal, one could send/receive files using zmodem commands.

This helped to google up zssh: http://zssh.sourceforge.net/. One will have to run it as a wrapper to original ssh.

0

I realise this is old, but still relevant.

A good solution is to use fish:// via dolphin (from KDE's Plasma), put the ssh server details like this in the file-location box (ie address bar) of dolphin file manager -- you might have to change the default settings to reveal the location bar:

fish://[email protected]:2200

where username is your username on the remote computer, example.com is the domain of the computer (or IP address, like 202.123.4.2) and after the colon is the port number (default is 22, but the remote server may be set with a different port number; and should be).

You'll get asked for the password (unless you've set up an SSH key; which is a good idea) and when accepted you'll have a tab in dolphin that you can use like any other tab. Moving files from one dolphin window or tab is done by drag-dropping the files as you would if they were all local. IME this is a really easy way to do things.

I also have used krusader for the same sort of thing, except it has told to help you synchronise file changes between different folders (locally or remotely).

This blog post has images and some extra info on using fish.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .