簡體   English   中英

更新壓縮 tar 中的單個文件

[英]Updating a single file in a compressed tar

給定一個壓縮存檔文件,例如application.tar.gz ,其中有一個文件夾application/x/y/z.jar ,我希望能夠使用我最新版本的z.jar並更新/刷新用它存檔。

除了以下內容之外,還有其他方法可以做到這一點嗎?

tar -xzf application.tar.gz
cp ~/myupdatedfolder/z.jar application/x/y
tar -czf application application.tar.gz

我知道 tar 中的-u開關可能有助於避免解壓整個內容,但我不確定如何准確使用它。

嗯,我找到了答案。

您不能將tar -u與壓縮檔案一起使用。 所以我使用的解決方案如下。 請注意,我z.jarz.jar文件移動到了我在當前目錄中創建的名為application/x/y的文件夾中。

gzip -d application.tar.gz
tar -uf application.tar application/x/y/z.jar
gzip application.tar

當我執行tar -tf application.tar (更新之后,gzip 之前)時,它正確顯示。

如果您要更新的文件是文本文件。 然后你可以直接使用vim編輯器打開包含文件的tarball並打開它,就像使用vim編輯器打開文件夾一樣。 然后修改文件並保存並退出。

但是,如果文件是二進制文件。 我不知道解決方案。

就我而言,我必須刪除該文件,然后按照以下步驟添加新文件:

我的 tar 文件

file.tar
└── foo.json
└── bar.json
└── dir
    └── zoo.json

我只想修改/更新foo.json文件而不提取和重新創建整個 tar 文件file.tar ,以下是命令:

tar -x -f file.tar foo.json # extract only foo.json file to my current location
# now modify the file foo.json as you want ...
tar --delete -f file.tar foo.json # delete the foo.json file from the file.tar
tar -uf file.tar foo.json # add the specific file foo.json to file.tar

壓縮文件:

如果是壓縮文件,例如file.tar.gz ,則需要使用gunzip file.tar.gz從壓縮文件(在此示例中為 gzip)中提取 tar 文件,該文件將為您創建 tar 文件file.tar . 那么你就可以完成上面的步驟了。

最后,您應該使用gzip file.tar再次壓縮 tar 文件,這將為您創建名為file.tar.gz壓縮文件

子目錄:

為了處理子目錄,您還必須在文件系統中保持相同的結構:

tar -x -f file.tar dir/zoo.json
# now modify the file dir/zoo.json as you want ...
tar --delete -f file.tar dir/zoo.jsonfile.tar
tar -uf file.tar dir/zoo.json

查看文件結構:

通過使用less命令,你可以查看文件的結構:

less file.tar

drwxr-xr-x root/root         0 2020-10-18 11:43 foo.json
drwxr-xr-x root/root         0 2020-10-18 11:43 bar.json
drwxr-xr-x root/root         0 2020-10-18 11:43 dir/zoo.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM