在 Linux/Unix 上复制文件范围

在 Linux/Unix 上复制文件范围

背景

我已经仔细阅读过这个问题:复制一系列文件并尝试了大部分答案。

我将数据导出到单个文件中.json,然后将该文件拆分.json为多个较小的序列化文件,每个文件有 50k 行。

$ls

smaller_aaaa  smaller_aaak  smaller_aaau  smaller_aabe  smaller_aabo  smaller_aaby  smaller_aaci  smaller_aacs  smaller_aadc  smaller_aadm
smaller_aaab  smaller_aaal  smaller_aaav  smaller_aabf  smaller_aabp  smaller_aabz  smaller_aacj  smaller_aact  smaller_aadd  smaller_aadn
smaller_aaac  smaller_aaam  smaller_aaaw  smaller_aabg  smaller_aabq  smaller_aaca  smaller_aack  smaller_aacu  smaller_aade  smaller_aado
smaller_aaad  smaller_aaan  smaller_aaax  smaller_aabh  smaller_aabr  smaller_aacb  smaller_aacl  smaller_aacv  smaller_aadf  smaller_aadp
smaller_aaae  smaller_aaao  smaller_aaay  smaller_aabi  smaller_aabs  smaller_aacc  smaller_aacm  smaller_aacw  smaller_aadg  smaller_aadq
smaller_aaaf  smaller_aaap  smaller_aaaz  smaller_aabj  smaller_aabt  smaller_aacd  smaller_aacn  smaller_aacx  smaller_aadh  smaller_aadr
smaller_aaag  smaller_aaaq  smaller_aaba  smaller_aabk  smaller_aabu  smaller_aace  smaller_aaco  smaller_aacy  smaller_aadi  smaller_aads
smaller_aaah  smaller_aaar  smaller_aabb  smaller_aabl  smaller_aabv  smaller_aacf  smaller_aacp  smaller_aacz  smaller_aadj  smaller_aadt
smaller_aaai  smaller_aaas  smaller_aabc  smaller_aabm  smaller_aabw  smaller_aacg  smaller_aacq  smaller_aada  smaller_aadk
smaller_aaaj  smaller_aaat  smaller_aabd  smaller_aabn  smaller_aabx  smaller_aach  smaller_aacr  smaller_aadb  smaller_aadl

目标

我想将文件从复制smaller_aaausmaller_aadd当前目录中的新目录中。

我尝试过的:

这些的变体

cp smaller_aa{au..dd} ./GlobalBuckets

我也尝试使用 bash 脚本:

#!/bin/bash
for file in {au..dd};do cp smaller_aa$file ~/Downloads/SPLIT/GlobalBuckets;done;
bash script

两种方法均存在错误:

cp: cannot stat 'smaller_aa{au..dd}': No such file or directory

答案1

我想将文件从复制smaller_aaausmaller_aadd当前目录中的新目录中。

以小的增量进行,例如:

  • 复制smaller_aaausmaller_aaaz第一个,然后
  • 复制smaller_aadasmaller_aadd,然后
  • 复制smaller_aabasmaller_aacz

应该是这样的:

cp smaller_aaa[u-z] smaller_aad[a-d] smaller_aa[b-c]* /path/to/destination

方括号通配符仅支持整数范围([107-203])或字符范围([a-d], [A-Ca-c])。

答案2

#!/usr/bin/python3

import os
import shutil

files = os.listdir()
for file in files:
    if file[:10] == "smaller_aa" and len(file) > 12:
        if file[8:12] >= "aaau" and file[8:12] <= "aadd":
            shutil.copyfile(file, f"/path/to/target/dir/{file}")

您可以使用列表推导式在一行中完成所有操作,但对于这种情况,我认为上面的代码使逻辑更加清晰

相关内容