Given host A / user a and host B / user b, and ssh from A to B: a@A$ ssh b@B. Suppose that in B there is a file called hello.txt. As we know, if I log into B and look for this file, I can use tab to autocomplete the file name:

a@A$ ssh b@B
b@B$ ls h

where at the end of the second line I typed 'tab'.

Now suppose that I am logged into A and I want to copy with scp hello.txt from B to A, but I do not remember the full file name, I only remember that the file starts with 'h'. I would like to do

a@A$ scp b@B:h

then press 'tab', and see the list of files in B that start with 'h', just like in the example above. Is this possible?


Here is how I tried to do this by following pLumo's suggested answer:

    $ ssh b@B
    Last login: xxx
    b@B:~$ ls
    b@B:~$ exit
    Shared connection to B closed.
    $ cat ~/.ssh/config
      Host B

          Hostname B.xxxx.xx

          User b

          ControlMaster auto    
           ControlPath ~/.ssh/control:%h:%p:%r

    $ scp b@B:my_file.dat .
    my_file.dat                                                                                                                                   100%    4     0.0KB/s   00:00  
    $ssh -fN b@B  
    $ scp b@B:my_ 

where at the end of the last line I pressed TAB, but nothing happens. 
  • Yes, this is possible. zsh for example has support for scp autocompletion – Panki Jul 24 '19 at 8:12

Tab completion works well for scp if you have one of these:

First option:

Reuse connections, add this to your ~/.ssh/config:

Host B
    User b
    ControlMaster auto
    ControlPath ~/.ssh/control:%h:%p:%r

For tab completion to work, you need to have a connection already open. You can start one in the background if you don't have one already open:

ssh -fN B

This even works via JumpHost.

Second option:

Use Public/Pricate Keys, scp will automatically use these. Downside is, scp will need to login each time which might be a bit slower than reusing already established connections.

  • I have tried the first option, and it does not work. If I understand correctly, I must add to ~/.ssh/config 'Host A...' where A is the host from which I am trying to connect, not the one to which I am trying to connect, right? In this case A would be my local computer, so I suspect that I should write 'Host local'. – Juan Jul 30 '19 at 22:47
  • Also, it is not clear how to practically implement the second option. – Juan Jul 31 '19 at 11:24
  • Sorry, I got A/B wrong. You have that config on local host (A) for the foreign host (B). – pLumo Jul 31 '19 at 11:41
  • Thank you. This still does not work: I added the lines that you suggested to ~/.ssh/config, made $ssh -fN B, then $scp b@B:file and pressed TAB, but no autocompletion. – Juan Jul 31 '19 at 12:51
  • and file... exists in B's users home? – pLumo Jul 31 '19 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.