14

I use SCP regularly to move files between servers and I've heard rumoured that you can use tab-completion on the remote server if you're using public keys, but so far I have had no luck.

What I'm trying to accomplish is the following:

scp -r remote.ip.address:/remote/dir/folder<TAB> /local/destiation/

An ls on the remote server of /dir/ would reveal 3 folders:

/remote/dir/folder_1
/remote/dir/folder_2
/remote/dir/folder_3

I'd like for SCP to reveal which folders match (if any), just like normal path completion when navigating the local filesystem. Tab completion does work on the local paths in the scp command line, but I've noticed that when the path for the remote machine matches the local (ie: "~/"), it completes/suggests files/folders from the local filesystem rather than the remote.

Running the following works for password-less login:

`ssh remote.ip.address`

...so I know the keys are setup correctly. Any ideas on how to get this working?

rkthkr
  • 8,618
  • 28
  • 38

4 Answers4

17

Which shell are you using? If you are using bash you may need to enable "advanced" completions in bash...

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

In your ~/.bashrc

On OSX you can do:

$ sudo port install bash-completion

To your ~/.profile add:

if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi
rkthkr
  • 8,618
  • 28
  • 38
  • I'm using OSX as a workstation, that file doesn't exist. However, I'm assuming that with any distrubtion I need to install the bash-completion package to get this to work? –  Jun 16 '09 at 18:27
  • I will update my answer.. – rkthkr Jun 16 '09 at 18:28
  • I just finished going through those steps myself, all works well. Thanks mate! –  Jun 16 '09 at 18:33
  • No problem, happy to help! – rkthkr Jun 16 '09 at 18:33
  • 1
    Of course, the port install part for OS X only works, if you have installed the MacPorts package (www.macports.org). And I guess there is package available if you should use fink instead of MacPorts. – Sven Jun 16 '09 at 18:43
  • Are there any downsides to enabling the "advanced" completions? – Bloke Feb 23 '16 at 10:06
2

If anyone is wanting to do this for Mac OSX Mountain Lion with rkthkr's method, they have to install Mac Ports (http://www.macports.org/install.php) first, run the command he mentioned:

$ sudo port install bash-completion

then configure their terminal app per these instructions found here: https://trac.macports.org/wiki/howto/bash-completion

Shawn Conn
  • 125
  • 6
  • When you have enough reputation to leave comments, additions to existing answers usually work better with those. (if you aren't going to edit the answers) – Andrew B Apr 06 '13 at 19:59
  • Thanks. That's what I wanted to do but, as you mentioned, I didn't have enough rep to do so. – Shawn Conn Apr 17 '13 at 03:52
0

Another, maybe simpler solution is using sshfs.

This is a file system client based on the SSH File Transfer Protocol. Since most SSH servers already support this protocol it is very easy to set up: i.e. on the server side there's nothing to do. On the client side mounting the filesystem is as easy as logging into the server with ssh.

It's a fuse based file system which links your remote folder to a local one. Under the hood, scp is used, but you can operate the cp command just like copying files on your local machine. Thus, tab-completion works automatically! sshfs is available for most distributions, e.g. do sudo apt-get install sshfs (this also loads the fuse kernel module)

Start like this:

$ sudo mkdir /mnt/server1
$ sudo chown local-username /mnt/server1
$ sshfs remote-username@server1.example.com:/home/remote-username /mnt/server1
$ ls /mnt/server1
 .. <bunch of files> ...
$ cp /mnt/server1/.bash<tab><tab>
.bash_history  .bash_logout   .bashrc
$ cp /mnt/server1/.bashrc .
Sebastian
  • 189
  • 6
0

I strongly recommend using yafc.

It supports filename completion and has some other useful features as well (eg recursive get/put/fxp/ls/rm)

jamespo
  • 1,698
  • 12
  • 12