1

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
  hello.txt

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?

Edit:

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

    $ ssh b@B
    Last login: xxx
    b@B:~$ ls
    my_file.dat
    b@B:~$ exit
    logout
    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. 
1
  • Yes, this is possible. zsh for example has support for scp autocompletion Commented Jul 24, 2019 at 8:12

1 Answer 1

1

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.

6
  • 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'. Commented Jul 30, 2019 at 22:47
  • Also, it is not clear how to practically implement the second option. Commented Jul 31, 2019 at 11:24
  • Sorry, I got A/B wrong. You have that config on local host (A) for the foreign host (B). Commented Jul 31, 2019 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. Commented Jul 31, 2019 at 12:51
  • and file... exists in B's users home? Commented Jul 31, 2019 at 13:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.