使用ls以一定顺序排列scp文件 [英] scp files in a certain order using ls

查看:381
本文介绍了使用ls以一定顺序排列scp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试以bash格式访问SCP文件时,它们最终都以看似随机(?)的顺序结束.

我发现了一种保持所需顺序的简单但并非十分优雅的方法,如下所述.有聪明的方法吗?

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?

编辑:从此处删除了我的早期解决方案,进行了清理,使用其他建议进行了改编,并在下面添加为答案.

deleted my early solution from here, cleaned, adapted using other suggestions, and added as an answer below.

推荐答案

要将文件从本地计算机(例如您的笔记本电脑)发送到远程计算机(例如您的计算服务器),您可以使用Merlin2011的聪明解决方案:

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. 进入您要从中复制文件的本地计算机中的文件夹.
  2. 执行scp命令,假设您具有远程服务器的访问密钥:
    scp -r $(ls -rt) user@foo.bar:/where/you/want/them/.
  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) user@foo.bar:/where/you/want/them/.

注意:如果您没有公共访问密钥,最好使用tar做类似的事情,然后发送tar文件(即tar -zcvf files.tar.gz $(ls -rt)),然后发送该文件tar文件可以单独使用scp.

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.

但是相反,您可能无法直接从远程服务器运行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. 在远程服务器中,cd进入要从中复制文件的文件夹.
  2. 以所需顺序创建文件列表.例如,对于相反的创建顺序(最近复制的最后一个):
    ls -rt > ../filenames.txt
  3. 现在,您需要将路径添加到每个文件名.在转到列表所在的目录之前,请使用pwd打印路径.现在开始:cd ..
  4. 您现在需要将此路径添加到列表中的每个文件名.有很多方法可以做到这一点,这是使用awk的一种方法:
    cat filenames.txt | awk '{print "path/to/files/" $0}' > delete_me.txt
  5. 您需要将文件名放在同一行中,并用空格分隔,因此将换行符更改为空格:
    tr '\n' ' ' < delete_me.txt > filenames.txt
  6. 将filenames.txt获取到本地服务器,并将其放在要将文件复制到的文件夹中.
  7. scp运行为:
    scp -r user@foo.bar:"$(cat filenames.txt)" .
  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 user@foo.bar:"$(cat filenames.txt)" .

类似地,这假设您有一个私有访问密钥,否则,将文件tar放在远程文件中并携带它要简单得多.

Similarly, this assumes you have a private access key, otherwise it's much simpler to tar the file in the remote, and bring that.

这篇关于使用ls以一定顺序排列scp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆