Bash
如何遞歸地使用特定名稱的 tar+gzip 文件夾?
我有一個帶有多個嵌套文件夾的大文件結構。我想使用特定名稱對每個文件夾進行 tar+gzip 壓縮並刪除原始文件夾。
在這個例子中,我希望擁有的腳本會檢測到每個以
.abc
tar+gzip 結尾的文件夾,然後刪除原始文件夾並留下 .gz。Main Folder/ Project 1/ Prj1FolderA.abc/ ...lots of tiny files and maybe subfolders... Prj1FolderB.abc/ ...lots of tiny files and maybe subfolders... Prj1FolderC.abc/ ...lots of tiny files and maybe subfolders... Project 2/ Prj2FolderA.abc/ ...lots of tiny files and maybe subfolders... Prj2FolderB.abc/ ...lots of tiny files and maybe subfolders... Prj2FolderC.abc/ ...lots of tiny files and maybe subfolders... Project 3/ Prj3FolderA.abc/ ...lots of tiny files and maybe subfolders... Prj3FolderB.abc/ ...lots of tiny files and maybe subfolders... Prj3FolderC.abc/ ...lots of tiny files and maybe subfolders... Project 4/ Another Folder Inside/ Prj4FolderA.abc/ ...lots of tiny files and maybe subfolders... Prj4FolderB.abc/ ...lots of tiny files and maybe subfolders... Prj4FolderC.abc/ ...lots of tiny files and maybe subfolders...
執行此腳本後,我希望該文件夾如下所示:
Main Folder/ Project 1/ Prj1FolderA.abc.tar.gz Prj1FolderB.abc.tar.gz Prj1FolderC.abc.tar.gz Project 2/ Prj2FolderA.abc.tar.gz Prj2FolderB.abc.tar.gz Prj2FolderC.abc.tar.gz Project 3/ Prj3FolderA.abc.tar.gz Prj3FolderB.abc.tar.gz Prj3FolderC.abc.tar.gz Project 4/ Another Folder Inside/ Prj4FolderA.abc.tar.gz Prj4FolderB.abc.tar.gz Prj4FolderC.abc.tar.gz
這只是一個範例,文件夾的名稱可能與上面不完全相同,它們也不是完全 2 層深。中間可能有多個文件夾。
我假設使用
find
這樣做,但我不太確定如何將正確的資訊傳遞到tar
並刪除原始文件夾。謝謝!
執行這個
Main Folder
:find . -type d -name '*.abc' -exec sh -c ' for d; do tar -C "$d" -zcf "$d.tar.gz" . && rm -r "$d"; done ' findsh {} +
雖然我測試了它,但我建議你也測試它。放一個
echo
beforerm
,所以如果出現任何問題,您不會失去數據。內部循環執行
tar
並且rm
(僅當tar
成功時)為找到的匹配 ‘*.abc’ glob 模式的子目錄。