【问题标题】:gsutil rename files as they are being copied from different directoriesgsutil 重命名文件,因为它们是从不同目录复制的
【发布时间】:2018-08-04 00:02:11
【问题描述】:

我在一个存储桶中有三个文件夹,每个文件夹都包含test_result_0.xml

  • gs://test-lab-12345-67890/2018-02-23_18:48:45.403202_urtc/Nexus6P-27-en_US-portrait/test_result_0.xml
  • gs://test-lab-12345-67890/2018-02-23_18:48:45.403202_urtc/Nexus7-21-en_US-portrait/test_result_0.xml
  • gs://test-lab-12345-67890/2018-02-23_18:48:45.403202_urtc/Nexus6P-23-en_US-portrait/test_result_0.xml

现在我希望将它们复制到本地文件夹中,并将它们重命名为:

  • test_result_0.xml

  • test_result_1.xml

  • test_result_2.xml

使用 bash 脚本最好的方法是什么?到目前为止我有这个,但它不起作用:(

for i in $("`gsutil ls gs://test-lab-12345-67890 | tail -1`*/*.xml"); do gsutil -m cp -r -U $i ~/Documents done

【问题讨论】:

  • 一些观察。首先,消息顶部列出的路径都是相同的。其次,在 $("...") 构造中,shell 将构造 "..." 字符串,然后尝试执行它。也许从一个 gsutil cp -r 开始到本地目录并在本地进行重命名。
  • 谢谢 - 我明白了,我会发布答案

标签: google-cloud-platform google-cloud-storage gcloud gsutil google-cloud-sdk


【解决方案1】:

我尝试使用AWK 改进您的解决方案。 看看你是否更喜欢这种解决方案,不知道你是否认为它更“优雅”:

gsutil ls -r gs://test-lab-12345-67890/**/test_result_* | awk -F"/" {'system("gsutil cp "$0" ~/localpath/"$6)'}

在我的测试环境中,使用类似于您的文件和文件夹结构。

P.S.我没有使用您在解决方案中使用的许多 gsutils 标志来提高可读性,但如果您认为需要,可以添加它们。

【讨论】:

    【解决方案2】:

    用这样的方法解决它:

    counter=0
    for path in $(gsutil ls -r gs://test-lab-12345-67890/**/*.xml | tail -3); do
    	counter=$((counter+1))
    	gsutil -m cp -r -U $path ~/localpath
    	mv ~/localpath/test_result_0.xml ~/localpath/test_result_$counter.xml
    done

    不是最漂亮的代码,但它现在可以工作.. 除非有人可以为我提供更好的方法吗? :)

    编辑:原来在文档中(我错过了)你可以指定:

    • --results-bucket=RESULTS_BUCKET]
    • --results-dir=RESULTS_DIR

    这样就完全缓解了我遇到的问题,因为我现在可以提前指定文件夹名称了!!我会保留旧代码以供参考。

    https://cloud.google.com/sdk/gcloud/reference/alpha/firebase/test/android/run

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-09
      • 2018-08-17
      • 2020-01-23
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多