bash - 如果远程位置存在相同的现有文件夹名称,则将文件夹中的所有文件移动到新位置

标签 bash file ubuntu

寻找一个 bash 脚本:
情况如下:
我的备份目录驱动器上有 1000 个文件夹和子文件夹
让我们说......

  • /备份
  • /备份/文件夹A
  • /备份/文件夹A/文件夹AA
  • /备份/文件夹B
  • /备份/文件夹B/文件夹BB

  • 我在辅助位置有数十个类似的文件夹(其中包含文件),文件夹名称将与主备份驱动器中的文件夹或子文件夹之一匹配。

    我想将特定扩展类型的所有内容从我的辅助位置 $FolderName 移动到备份位置 + 匹配的子文件夹,前提是 $FolderName 完全匹配并从我的辅助位置删除文件夹!

    如果备份位置中没有对应的文件夹或子文件夹,则单独保留源文件夹和文件。

    期待得到一些帮助/指导。

    麦克风

    请求的附加信息。预期的输入和输出

    可以说我有以下内容:
    备份文件夹
    /backup/test/file.bak
    

    对于我的辅助文件夹位置:
    /secondarylocation/mike/test/hello/john.bak
    /secondarylocation/mike/test/hello/backup.zip
    

    我希望这是最终结果
    /backup/test/file.bak
    /backup/test/john.bak
    /backup/test/backup.zip
    

    和/secondarylocation/mike/test *以及子文件夹和文件已删除

    最佳答案

    使用带引号的文件夹和文件类型运行此脚本:

    ./merge.sh "backup" "secondarylocation/mike" "*.zip" "*.bak"
    
  • 替换 -iname-name如果要搜索后缀区分大小写
  • 替换 mv -fvmv -nv当您不想覆盖重复的文件名时
  • 添加 -mindepth 1持续find如果你想保留空文件夹test
  • merge.sh
    #!/bin/bash
    
    # read folders from positional parameters
    [ -d "$1" ] && targetf="$1" && shift
    [ -d "$1" ] && sourcef="$1" && shift
    
    if [ -z "$targetf" ] || [ -z "$sourcef" ]
      then
        echo -e "usage: ./merge.sh <targetfolder> <sourcefolder> [PATTERN]..."
        exit 1
    fi
    
    # add prefix -iname for each pattern
    while [ ${pattern:-1} -le $# ]
      do
        set -- "$@" "-iname \"$1\""
        shift
        pattern=$((${pattern:-1}+1))
    done
    
    # concatenate all prefix+patterns with -o and wrap in parentheses ()
    if (( $# > 1 ))
      then
        pattern="\( $1"
        while (( $# > 1 ))
          do
            pattern="$pattern -o $2"
            shift
        done
        pattern="$pattern \)"
      else
        pattern="$1"
    fi
    
    # move files from searchf to destf
    find "$targetf" -mindepth 1 -type d -print0 | sort -z | while IFS=$'\0' read -r -d $'\0' destf
      do
        find "$sourcef" -mindepth 1 -type d -name "${destf##*/}" -print0 | sort -z | while IFS=$'\0' read -r -d $'\0' searchf
          do
            if (( $# ))
              then
                # search with pattern
                eval find "\"$searchf\"" -depth -type f "$pattern" -exec mv -fv {} "\"$destf\"" \\\;
              else
                # all files
                find "$searchf" -depth -type f -exec mv -fv {} "$destf" \;
            fi
            # delete empty folders
            find "$searchf" -depth -type d -exec rmdir --ignore-fail-on-non-empty {} +
        done
    done
    
    exit 0
    

    这将合并 hello进入 test (收获果实,砍树)

    关于bash - 如果远程位置存在相同的现有文件夹名称,则将文件夹中的所有文件移动到新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62053283/

    相关文章:

    linux - Bash - : ls | grep 的替代品

    bash - 通过 ssh 将脚本和二进制数据通过管道传输到标准输入

    java - FreeMarker修改现有文件

    mysql - 连接到 mysql 数据库并执行查询并将结果导出到变量 - bash 脚本

    bash - bash 中的嵌套关​​联数组

    C# - 在二进制文件中搜索模式

    file - 文件中使用了多少字节?

    ubuntu - 调试时如何加载 gdbinit?

    ubuntu - CNTLM 不适用于 cURL

    linux - 关于 bash 中的函数