在我的情况下,我必须执行以下步骤:删除该文件,然后添加新文件:
我的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
,你需要从压缩文件中提取tar文件(在这个例子中是gzip),使用gunzip file.tar.gz
命令,它将为你创建一个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.json
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