Linux: Save a list of files over SFTP to a text file

Problem

You want to get a list of files available on a remote site

There are many ways not to do this - ways that may work with FTP but don't work with FTP.
After trying a of "helpful" suggestions found on forums I stumbled on this - which works fine.

Solution

(this command assumes that you have RSA keys setup, hence passwords not required)

cd (localdir)

sftp -oPort=2222 hermitage@sftp.genusplc.com <<'EOF' > file.list
ls
EOF

file.list will have the list. You might need to clean up the first line.

Acknowledgement

One of the answers not marked as correct, and adapted for SFTP by me.
https://unix.stackexchange.com/questions/214961/save-a-list-of-files-over-sftp-to-a-text-file

Comments