Whenever I try to SCP files (in bash), they end up in a seemingly random(?) order.
I've found a simple but not-very-elegant way of keeping a desired order, described below. Is there a clever way of doing it?
Edit: deleted my early solution from here, cleaned, adapted using other suggestions, and added as an answer below.
To send files from a local machine (e.g. your laptop) to a remote (e.g. your calculation server), you can use Merlin2011's clever solution:
scp
command, assuming you have an access key for the remote server:scp -r $(ls -rt) [email protected]:/where/you/want/them/.
Note: if you don't have a public access key it may be better to do something similar using tar
, then send the tar file, i.e. tar -zcvf files.tar.gz $(ls -rt)
, and then send that tar file on its own using scp
.
But to do it the other way around you might not be able to run the scp
command directly from the remote server to send files to, say, your laptop. Instead, you may need to, let's say bring files into your laptop. My brute-force solution is:
cd
into the folder you want to copy files from.ls -rt > ../filenames.txt
pwd
. Now do go up: cd ..
cat filenames.txt | awk '{print "path/to/files/" $0}' > delete_me.txt
tr '\n' ' ' < delete_me.txt > filenames.txt
scp -r [email protected]:"$(cat filenames.txt)" .
Similarly, this assumes you have a private access key, otherwise it's much simpler to tar
the file in the remote, and bring that.