Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scp files in a certain order using ls

Tags:

scp

ls

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.

like image 549
Vicks Avatar asked Apr 14 '14 18:04

Vicks


People also ask

Can you use wildcards with scp?

2) Unfortunatly, scp will not support wild cards as like rcp.

How do I recursively scp files?

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents. You'll be prompted for your password on the source system ( deathstar.com ). The command won't work unless you enter the correct password.

How do I scp multiple files in Unix?

As Jiri mentioned, you can use scp -r user@host:/some/remote/path /some/local/path to copy files recursively. This assumes that there's a single directory containing all of the files you want to transfer (and nothing else). This the only answer that actually answers the question.

How do I scp from one directory to another?

scp -r username@IP:/path/to/server/source/folder/ . . (dot): it means current folder . so copy from server and paste here only.


1 Answers

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:

  1. Go into the folder in your local machine where you want to copy files from.
  2. Execute the 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:

  1. In the remote server, cd into the folder you want to copy files from.
  2. Create a list of the files in the order you want. For example, for reverse order of creation (most recent copied last):
    ls -rt > ../filenames.txt
  3. Now you need to add the path to each file name. Before you go up to the directory where the list is, print the path using pwd. Now do go up: cd ..
  4. You now need to add this path to each file name in the list. There are many ways to do this, here's one using awk:
    cat filenames.txt | awk '{print "path/to/files/" $0}' > delete_me.txt
  5. You need the filenames to be in the same line, separated by a space, so change newlines to spaces:
    tr '\n' ' ' < delete_me.txt > filenames.txt
  6. Get filenames.txt to the local server, and put it in the folder where you want to copy the files into.
  7. The scp run would be:
    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.

like image 153
Vicks Avatar answered Oct 21 '22 05:10

Vicks