代码之家  ›  专栏  ›  技术社区  ›  Owen Morgan

使用RSync复制连续范围的文件

  •  6
  • Owen Morgan  · 技术社区  · 10 年前

    很抱歉,如果这没有意义,但我会尽力提供所需的所有信息!

    我想使用rsync将一系列顺序编号的文件从一个文件夹复制到另一个文件夹。

    我正在归档一个DCDM(它是一个电影),它包含60万个单独编号的顺序.tif图像文件(每个约10 MB)。

    我需要将其分解以正确归档到LTO6磁带上。我想使用rsync来准备文件夹,这样我的简单bash.sh文件就可以自动处理我想备份到磁带上的各种文件夹和文件。

    运行rsync时,我通常使用的命令是:

    sudo rsync -rvhW --progress --size only <src> <dest>
    

    我使用 sudo 如果需要,我总是先用 --dry-run

    我做任何事情(不排除错误)的唯一方法是使用 * 通配符。然而,这仅适用于具有设置模式的文件(例如。 01* 将仅移动范围中的文件 010000 - 019999 )我必须重复一遍 02 , 03 , 04

    我在网上找过,正在努力寻找一个可行的答案。

    这可能是不可能的,而且有600000个.tif文件,我不能为每个文件写一个排除!

    有没有想过(如果有的话)如何做到这一点?

    欧文。

    4 回复  |  直到 10 年前
        1
  •  4
  •   Community TTT    4 年前

    您可以使用 pattern matching :

    for file in [0-9]*; do
        # do something to $file name that starts with digit
    done
    

    或者,您可以启用 extglob 选项并循环所有仅包含数字的文件名。这可以消除任何以数字开头但在第一个字符之后包含非数字的潜在不需要的文件。

    shopt -s extglob
    for file in +([0-9]); do
        # do something to $file name that contains only digits
    done
    
    • +([0-9]) 扩展为一个或多个数字

    更新:

    根据您最近评论中的文件名模式:

    shopt -s extglob
    for file in legendary_dcdm_3d+([0-9]).tif; do
        # do something to $file
    done
    
        2
  •  2
  •   5gon12eder    10 年前

    Globing是shell的一个功能,它可以将通配符扩展到匹配的文件名列表中。你已经在问题中使用了它。

    对于以下解释,我将假设我们位于包含以下文件的目录中:

    $ ls -l
    
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 file.txt
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 funny_cat.jpg
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 report_2013-1.pdf
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 report_2013-2.pdf
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 report_2013-3.pdf
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 report_2013-4.pdf
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 report_2014-1.pdf
    -rw-r----- 1 5gon12eder staff 0 Sep  8 17:26 report_2014-2.pdf
    

    最简单的情况是匹配所有文件。以下是穷人的 ls .

    $ echo *
    
    file.txt funny_cat.jpg report_2013-1.pdf report_2013-2.pdf report_2013-3.pdf report_2013-4.pdf report_2014-1.pdf report_2014-2.pdf
    

    如果我们想匹配2013年的所有报告,我们可以缩小匹配范围:

    $ echo report_2013-*.pdf
    
    report_2013-1.pdf report_2013-2.pdf report_2013-3.pdf report_2013-4.pdf
    

    例如,我们可以忽略 .pdf 部分,但我希望尽可能具体。

    您已经想出了一个解决方案,可以使用它来选择一系列编号文件。例如,我们可以按季匹配报告:

    $ for q in 1 2 3 4; do echo "$q. quater: " report_*-$q.pdf; done
    
    1. quater:  report_2013-1.pdf report_2014-1.pdf
    2. quater:  report_2013-2.pdf report_2014-2.pdf
    3. quater:  report_2013-3.pdf
    4. quater:  report_2013-4.pdf
    

    如果我们懒得打字 1 2 3 4 ,我们可以使用 $(seq 4) 相反这将调用程序 seq 带参数 4 并替换其输出( 1 2 3 4 在这种情况下)。

    现在回到你的问题:如果你想要10次方的块大小,你应该能够扩展上面的例子来满足你的需求。

        3
  •  0
  •   mulllhausen    9 年前

    我知道这个老问题,但有人可能会觉得这个有用。上述扩展范围的示例也适用于 rsync 例如,从目录中复制以a、b和c开头但不以d和e开头的文件 /tmp/from_here 到目录 /tmp/to_here :

    $ rsync -avv /tmp/from_here/[a-c]* /tmp/to_here
    sending incremental file list
    delta-transmission disabled for local transfer or --whole-file
    alice/
    bob/
    cedric/
    total: matches=0  hash_hits=0  false_alarms=0 data=0
    
    sent 89 bytes  received 24 bytes  226.00 bytes/sec
    total size is 0  speedup is 0.00
    
        4
  •  0
  •   Jamie Metzger    8 年前

    如果要写入LTO6磁带,则应考虑在命令中包含“--inplace”。Inplace用于写入线性文件系统,如LTO