Linux – SCP filename tab completion

command-line-interfacelinuxmac-osxscpssh

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?

Best Answer

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