SFTP with curl - A Cheat Sheet
Did you know you can use curl as a minimal SFTP client? In this post we will guide you through the various curl commands for
uploading
,
downloading
, and
listing
files on a SFTP server, with ready to use snippets you can copy/paste in your terminal.
List files via curl
~/$
curl -k sftp://127.0.0.1
drwxr-xr-x 2 mickael mickael 4096 Jun 25 2021 Videos
drwx------ 2 mickael mickael 4096 Mar 5 00:00 .ssh
drwxr-xr-x 3 mickael mickael 4096 Jul 3 2021 Music
drwxrwxr-x 6 mickael mickael 4096 Jan 26 00:00 filestash
-rw------- 1 mickael mickael 55 Jun 26 2021 .authinfo
drwxr-xr-x 3 root root 4096 Jun 25 2021 ..
-rw------- 1 mickael mickael 100306 Apr 1 00:54 .bash_history
drwxr-xr-x 2 mickael mickael 20480 Mar 3 08:40 Pictures
drwxr-xr-x 3 mickael mickael 16384 Apr 1 09:17 Downloads
drwxr-x--- 46 mickael mickael 4096 Apr 1 00:54 .
-rw-r--r-- 1 mickael mickael 4153 Jul 25 2021 .bashrc
drwxr-xr-x 11 mickael mickael 4096 Feb 6 18:59 Documents
drwx------ 7 mickael mickael 4096 Mar 30 16:19 .emacs.d
-rw-rw-r-- 1 mickael mickael 3433 Jun 25 2021 .tmux.conf
drwxr-xr-x 2 mickael mickael 4096 Nov 30 19:42 Desktop
~/$
curl -k sftp://127.0.0.1/~/Downloads
-rw-rw-r-- 1 mickael mickael 12 Apr 1 15:28 foobar.txt
User authentication
You have a few ways to authenticate a user.
A first technique is the default private key authentication which uses your private key available from
~/.ssh/id_rsa
:
~/$
curl -k sftp://
[email protected]
The other method is by using your user defined password directly in curl, this is how it works:
~/$
curl -k --user root sftp://127.0.0.1
Enter host password for user 'root':
Upload a file via curl
~/$
echo "lorem ipsum" > foobar.txt
~/$
curl -T foobar.txt -k sftp://127.0.0.1/~/Downloads/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6 0 0 100 6 0 9 --:--:-- --:--:-- --:--:-- 9
100 6 0 0 100 6 0 9 --:--:-- --:--:-- --:--:-- 9
Download a file via curl
~/$
curl -k sftp://127.0.0.1/~/Downloads/foobar.txt
lorem ipsum
or download the file directly in your local machine:
~/$
curl -O -k sftp://127.0.0.1/~/Downloads/foobar.txt
~/$
cat foobar.txt
lorem ipsum