![如何在保留目錄結構的同時將檔案從多個子目錄複製到另一個目錄?](https://rvso.com/image/1072581/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%BF%9D%E7%95%99%E7%9B%AE%E9%8C%84%E7%B5%90%E6%A7%8B%E7%9A%84%E5%90%8C%E6%99%82%E5%B0%87%E6%AA%94%E6%A1%88%E5%BE%9E%E5%A4%9A%E5%80%8B%E5%AD%90%E7%9B%AE%E9%8C%84%E8%A4%87%E8%A3%BD%E5%88%B0%E5%8F%A6%E4%B8%80%E5%80%8B%E7%9B%AE%E9%8C%84%EF%BC%9F.png)
我有一個包含許多類似子目錄的目錄,每個子目錄都有一些我感興趣的檔案和一些我不感興趣的檔案。
我想複製:
FROM:兩個特定檔案:每個子目錄中的 file1.txt file2.json
TO:不同地點
但在該位置建立子目錄
例如如果我有
base
|_folder_00000001
| |_-file1.txt
| |_ file2.json
| |_some_other_things_I_dont_need
| |_some_other_folder_I_dont_need
|
|_folder_00000002
| |_-file1.txt
| |_ file2.json
| |_some_other_things_I_dont_need
| |_some_other_folder_I_dont_need
|
|_folder_00000003
| |_-file1.txt
| |_ file2.json
| |_some_other_things_I_dont_need
| |_some_other_folder_I_dont_need
|
|_folder_00000004
| |_-file1.txt
| |_ file2.json
| |_some_other_things_I_dont_need
| |_some_other_folder_I_dont_need
|
我想將其複製到另一個位置,但排除“我不需要的其他一些東西和資料夾”
target
|_folder_00000001
| |_-file1.txt
| |_ file2.json
|
|
|_folder_00000002
| |_-file1.txt
| |_ file2.json
| |
|_folder_00000003
| |_-file1.txt
| |_ file2.json
| |
|_folder_00000004
| |_-file1.txt
| |_ file2.json
|
我如何從 bash 終端執行此操作?
答案1
您可以cp
使用--parents
:
cd base
cp -v --parents */file1.txt */file2.json ../target/
答案2
您要複製的文件是否有某種模式?
從範例來看,如果我們將“-file1.txt”和“file2.json”作為要複製到新目錄結構的文件,可以這樣做,我應該認為:
rsync -av --include="*/" --include="*-file1.txt" --include="*file2.json" --exclude="*" /src/dir/ /dest/dir/
我希望這有幫助。