就我而言,我必须删除该文件,然后使用以下步骤添加新文件:
我的 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
,您需要从压缩文件(在本例中为 gzip)中提取 tar 文件,使用gunzip file.tar.gz
它将为您创建 tar 文件file.tar
。那么您将能够执行上述步骤。
最后,您应该再次压缩 tar 文件,使用gzip file.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