1

I log into sftp:

sftp user@server

Then I run:

ls -lt

I expect files to be sorted by date.

sftp>  ls -lt *.csv
-rw-------    0 76547986 200       5073032 Mar 14 08:42 file1.csv
-rw-------    0 76547986 200       5073032 Mar 15 08:41 file2.csv
-rw-------    0 76547986 200       5073032 Mar 16 08:41 file3.csv
-rw-------    0 76547986 200       5072802 Mar 17 08:42 file4.csv
-rw-------    0 76547986 200       1117736 Sep 30 14:44 file5.csv
-rw-------    0 76547986 200       1120419 Oct  2 08:45 file6.csv
-rw-------    0 76547986 200       1119763 Oct  1 08:43 file7.csv
-rw-------    0 76547986 200       5073258 Mar 13 08:41 file8.csv

But they are not.

Here is the version of OpenSSH

OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g  1 Mar 2016

Any idea why date sorting for ls in sftp is not working?

NOTE: ls -lt works in bash on the sftp server just not through sftp prompt. Maybe a bug in sftp?

Martin Prikryl
  • 7,756
  • 2
  • 39
  • 73
  • 1
    Sorting seems to work on my systems when I don't have a glob (`ls -lt`) but when * add a glob (`ls -lt *.log`) it breaks. – Zoredache Mar 17 '17 at 19:46
  • 1
    It seems to work ok without the `*`. – user9517 Mar 17 '17 at 19:46
  • 4
    I am not great with C, but the function for handling ls with a glob (`do_globbed_ls`) is completely different from the function for handling an ls without a glob (`do_ls_dir`). The glob function doesn't seem to have a sort. https://github.com/openssh/openssh-portable/blob/master/sftp.c#L1528 – Zoredache Mar 17 '17 at 19:56
  • 1
    One easy work-around is to `sshfs` and use normal utilities (bash `ls -lt` and many more). Despite its name, `sshfs` doesn't require full-blown `ssh` access; it is reality intended to abstract the deficient `sftp` away. – kubanczyk Mar 17 '17 at 21:26

1 Answers1

7

If the ls argument includes a mask (and the mask does not match a single folder only), all sorting flags are ignored.

Note a lack of LS_*_SORT flags use in the do_globbed_ls function.

Contrary to non-globbed do_ls_dir.


It seems like a bug to me. Hence I have posted a bug report:
sftp ls command ignores sorting flags for globbed listing

A fix is included since OpenSSH 7.6.

Martin Prikryl
  • 7,756
  • 2
  • 39
  • 73