Wondering whether it's possible to specify a sort order when transferring files with scp.

For example: $ scp -r "user@host:/path/to/download/" .

[... files download in a seemingly random order ...]

Since perhaps this may be related to how scp functions, is there some way to configure ssh on the source(server)-side so that files are transfered, for example, in ascending filename order?

3 Answers 3


I whould solve the problem with tar and the pipe-transparency of ssh. An example can be seen below, the remote PC is named bar. Before the test, I created three empty files in /tmp/foo by executing ssh bar 'mkdir /tmp/foo ; touch /tmp/foo/{a,b,c}'.

$ ssh bar 'cd /tmp/foo ; ls | sort -r | tar -cT -' | tar -xv
$ ls -l a b c
-rw-r--r-- 1 dnet dnet 0 nov   29 17:07 a
-rw-r--r-- 1 dnet dnet 0 nov   29 17:07 b
-rw-r--r-- 1 dnet dnet 0 nov   29 17:07 c

The -T flag of tar makes it read filenames to pack from the next parameter, and - means the standard input. Now you just need to pipe the name of the files into it, which can come from ls (as in the example) or find for trickier tasks. With this setup, you can use sort to indicate the exact order the files will be transmitted through SSH (in this case, -r sorted files in reverse alphabetic order). The -v is only added to the final, unpacking tar, so that the order is visible.

  • Woah, great lower-level approach and presenting the broader picture so clearly. Thank you. Nov 29, 2012 at 17:35
  • How could such an approach be extended to transfer files individually instead of in an archive? Would a loop iterating over results from sort in the bar remote command somehow accomplish this? Nov 29, 2012 at 17:45
  • It might, but I don't see the problem with transferring it as an arcive. If you think about it, there's not much difference between this approach and scp -- both send metadata and contents for each file, they just use a different "protocol". I guess you could iterate over the results, but then you should take care of files with whitespace in their names.
    – dnet
    Nov 29, 2012 at 21:56
  • 2
    @dimadima, if you like the answer so much, perhaps you could accept the answer. May 16, 2015 at 20:05
  • This answer works, but one has to adjust it to the case when files have spaces in names.
    – Ruslan
    Jun 15, 2017 at 16:31

One can achieve file transfer with alphabetical order using rsync:

rsync -P -e ssh -r user@remote_host:/some_path local_path

P allows partial downloading, e sets the SSH protocol and r downloads recursively.


In case this helps someone, I provided a simple solution here:


Still waiting for a cleverer way to do it!

  • 1
    This is just a link. For a complete answer, provide the answer here and then provide the link for reference. Apr 20, 2014 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .