SCP recursive file transfer order

Solution 1:

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
c
b
a
$ 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.

Solution 2:

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.