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?
Best Answer
I whould solve the problem with
tar
and the pipe-transparency ofssh
. An example can be seen below, the remote PC is namedbar
. Before the test, I created three empty files in/tmp/foo
by executingssh bar 'mkdir /tmp/foo ; touch /tmp/foo/{a,b,c}'
.The
-T
flag oftar
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 fromls
(as in the example) orfind
for trickier tasks. With this setup, you can usesort
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, unpackingtar
, so that the order is visible.