UNIX

[ Solved – 6 Answers ] BASH – SSH easily copy file to local system

If you are logged in to a system via SSH, is there a way to copy a file back to my local system without firing up another terminal or screen session and doing scp or something similar or without doing SSH from the remote system back to the local system?

  • You can mount the remote file system on your local machine with sshfs:
Java Code
mkdir -p /mnt/sshfs

[email protected]:~# sshfs 192.168.1.2:/ /mnt/sshfs
[email protected]:~# umount /mnt/sshfs

  • Another (IMO) way would be:

Bash Code
# to remote host
cat localfile.conf | ssh [email protected] 'cat -> /tmp/file.conf'

# from remote host
ssh [email protected] 'cat /tmp/remotefile.conf' > /tmp/file.conf

Or if you prefer something GUI-like, try Midnight Commander. They call the feature Shell-Link. Most distros have em in their package systems as mc.

  • If the file is small, you can type it out and copy-paste from the terminal output.
  • If the file contains non-printable characters, use an encoding such as base64.
Java Code
remote.example.net$ base64 <myfile
(copy the output)

local.example.net$ base64 -d >myfile
(copy the output)
Ctrl+D

More conveniently, if you have X forwarding active, copy the file on the remote machine and paste it locally.

You can pipe data in and out of xclip or xsel. If you want to preserve the file name and metadata, copy-paste an archive.

Bash Code
remote.example.net$ tar -czf - myfile | xsel
local.example.net$ xsel | tar -xzf -

  • You can either use scp or rsync.

In your local system:

Bash Code
scp [email protected]:/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.

READ  How To Install Docker on Ubuntu 16.04

On your local shell:

Bash Code
rsync -avz -e ssh [email protected]:/remote/dir /local/dir/

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

  • You could use scp secure copy.

From you local shell:

Bash Code
scp -C [email protected]:/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

  • The best efficient solution is to use xclip-copyfile and xclip-pastefile.
  • On the server, you use xclip-copyfile to copy one or more files.
  • These files are then available on your local server. There, you can use xclip-pastefile.
  • This bypasses the need to use scp or have a local ssh server. you use this with cygwin for instance.
  • The only problem is that this requires installing xclip if you don’t already have it and this works with binary files too.

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

X