代码之家  ›  专栏  ›  技术社区  ›  Jitesh Sojitra

重命名后将子目录文件移动到父目录的Shell脚本

  •  2
  • Jitesh Sojitra  · 技术社区  · 6 年前

    我想写一个外壳脚本,将文件放在父目录,以避免不必要的导航和更有意义的文件名。例如。

    test-2
        Chrome_68
            errors
                1.png
    test-5
        Chrome_68
            errors
                1.png
    test-10
        Chrome_68
            errors
                1.png
    test-11
        Chrome_68
            errors
                1.png
    

    预期:

    test-2
        test-2.png
    test-5
        test-5.png
    test-10
        test-10.png
    test-11
        test-11.png
    

    尝试的代码:

    testDir=/tmp/screenshots
    find $testDir -name *.png
    find $testDir -mindepth 1 -type f -print -exec mv {} . \;
    find . -type d -name Chrome_68 -exec rm -f {} \;
    find . -type d -name errors -exec rm -f {} \;
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Cyrus    6 年前

    使用prename:

    prename -n 's|(test-.*)/Chrome_68/errors/[0-9]+|$1/$1|' test-*/Chrome_68/errors/1.png
    

    输出:

    test-10/Chrome_68/errors/1.png renamed as test-10/test-10.png
    test-11/Chrome_68/errors/1.png renamed as test-11/test-11.png
    test-2/Chrome_68/errors/1.png renamed as test-2/test-2.png
    test-5/Chrome_68/errors/1.png renamed as test-5/test-5.png
    

    删除 -n rm -r test-*/Chrome_68/ .