Linux

在 Linux/Unix 上複製文件範圍

  • April 21, 2021

背景

我已經徹底閱讀了這個問題:複製一系列欄位並嘗試了大部分答案。

我將數據導出為一個,.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

目標

我想將文件從 to 複製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

我想將文件從 to 複製smaller_aaausmaller_aadd目前目錄中的新目錄中。

以小增量進行,例如:

  • smaller_aaau先複製smaller_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])。

#!/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}")

您可以使用列表理解在一行中完成所有操作,但是對於這種情況,我認為上面的程式碼使邏輯更加可見

引用自:https://serverfault.com/questions/1061011