
對於 NLP 項目,我需要一份 Project Gutenberg 庫的副本。現在,該專案允許下載他們的文件,特別是如果它是出於鏡像目的(我計劃最終設置一個),但對於我的工作,我只需要當前文件的特定子集。
來源目錄的組織方式如下:
|
| - 1 - |
| |- 1
| |- 2
| |...
| - 2
| .
| .
| .
| - 9
| - cache
| - retired
| ...
我唯一感興趣的目錄是編號的目錄,我唯一感興趣的文件類型是.txt
,我也不想要以-8.txt
或結尾的文件-h.txt
,但我現在願意容忍它們。
到目前為止我已經嘗試過:
--include "*/" --include "*.txt" --exclude "cache" --exclude "images" --exclude "retired" --exclude "pg" --exclude "*-8.txt" --exclude "*"
<- 這個仍然會拉入「快取」資料夾,因為它還包含一些.txt
文件--include "*/" --include "*.txt" -f'- *\-8.txt' -f'- *\-h.txt' -f'- cache/**' --exclude "cache" --exclude "images" --exclude "retired" --exclude "pg" --exclude "*"
<- 或多或少是同一件事
問題似乎是這樣的:
- 我需要排除一切,因為我需要的東西非常有限
- 我包括編號目錄,因為這就是我所需要的
- 包含
*.txt
會破壞先前的排除,因為其他目錄也包含文字檔案。
我該怎麼辦呢?
答案1
來自貼文 rsync - 排除除少數目錄之外的所有目錄,我引用的是 達裡爾·E·克拉克的回答:
一個簡單的過濾器就可以解決問題。使用正確的範例以先前的答案為基礎 - 明確包含父資料夾以及所有 (**) 子資料夾和檔案。然後排除其他一切。這是
filter.txt
:+ /include_this_dir/ + /include_this_dir/** + /include_that_dir/ + /include_that_dir/** - /** With the command line: rsync -av --dry-run --filter="merge filter.txt" source_dir/ dest_dir/
會導致:
sending incremental file list created directory dest_dir ./ include_that_dir/ include_that_dir/somefile.txt include_that_dir/subdir/ include_this_dir/ sent 202 bytes received 65 bytes 534.00 bytes/sec total size is 0 speedup is 0.00 (DRY RUN)
添加您的過濾器*.txt
。
換句話說:先包含,然後再排除所有。
答案2
您可以將編號目錄與 glob 明確配對[0-9]/
:
-f'+ [0-9]/' \
-f'- *-[8h].txt' \
-f'+ *.txt' \
-f'- *' \
如果數字高於 9,也許還可以添加-f'+ [0-9][0-9]/
或-f'+ [0-9]*/
就足夠了。