【问题标题】:find and copy so files while removing all but major version number查找并复制so文件,同时删除除主要版本号之外的所有文件
【发布时间】:2017-08-18 15:04:22
【问题描述】:

试图使用“查找”复制一堆共享对象。差不多了,但想删除除主要版本之外的所有版本号。 例如将 somesharedobject.so.30.0.4 复制到 somesharedobject.so.30

find . -maxdepth 1 -type f -name '*.so.*' -exec cp '{}' test/'{}' \;

我猜我将不得不通过管道连接到 xargs 和 sed,但只是遇到了心理障碍。

find . -maxdepth 1 -type f -name '*.so.*'|xargs -I '{}' cp '{}' test/'{}'

【问题讨论】:

    标签: linux shell


    【解决方案1】:

    我想我会选择这样的东西 find . -maxdepth 1 -type f -name '*.so.*' -exec cp '{}' test/'{}' \; for f in test/*.so.* ; do mv "$f" "${f%.*.*}" ; done 从我的测试看来工作正常

    【讨论】:

    • 不错。少量文件无关紧要,但是,通常 - 如果可行 - 您可以通过使用 + 而不是 \; 作为终止符来提高 -exec 的效率,后者(通常)通过 所有文件路径一次,但遗憾的是,只有{}是最后一个参数;在 Linux 上,您可以使用cp 的-t 选项来解决这个问题,但是:-exec cp -t test/ {} +。顺便说一句:虽然它肯定不会受到伤害,但在类似 POSIX 的 shell 中,您不需要引用 {}。
    【解决方案2】:

    我会写一个函数+脚本来简化工作

    #!/bin/bash
    specialised_copy(){
    version="${1##*so.}"
    # extract the version part alone in the above step
    cp "$1" "test/${1%%.so*}.so.${version%%.*}" 
    #cut the major version part from the version and use it for copy
    #note folder test should be relative to where the script is saved
    }
    export -f specialised_copy
    find . -maxdepth 1 -type f -name '*.so.*' -exec bash -c 'specialised_copy "$1"' _ {} \;
    

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多