一方面,我有很多用以下命令创建的 tar 文件牛羚格式,另一方面我有一个工具只支持帕克斯(又名POSIX) 格式。我正在寻找一种简单的方法将现有的 tar 文件转换为 pax 格式 - 无需将它们提取到文件系统并重新创建档案。
GNU tar 支持这两种格式。但是,我还没有找到简单的转换方法。
如何转换现有的牛羚tar 文件到帕克斯?
[我在 superuser.com 上问了同样的问题,评论者建议将问题迁移到 unix.stackexchange.com。]
答案1
您可以使用以下方法执行此操作bsdtar:
ire@localhost: bsdtar -cvf pax.tar --format=pax @gnu.tar
ire@localhost:file gnu.tar
gnu.tar: POSIX tar archive (GNU)
ire@localhost:file pax.tar
pax.tar: POSIX tar archive
@archive
是神奇的选择。来自联机帮助页:
@archive
(c and r mode only) The specified archive is opened and the
entries in it will be appended to the current archive. As a sim-
ple example,
tar -c -f - newfile @original.tar
writes a new archive to standard output containing a file newfile
and all of the entries from original.tar. In contrast,
tar -c -f - newfile original.tar
creates a new archive with only two entries. Similarly,
tar -czf - --format pax @-
reads an archive from standard input (whose format will be deter-
mined automatically) and converts it into a gzip-compressed pax-
format archive on stdout. In this way, tar can be used to con-
vert archives from one format to another.
答案2
用户 Random832 写道:
[...] 创建一个空的 tar 文件 [...]
免责声明:我尚未测试此脚本。
上帝祝福你!你给了我想法。我测试了你的脚本,但如果有人创建了一个空的 tar 文件,tar 不会将其视为“posix tar”文件。所以我编写了一个脚本,创建一个“posix tar”文件,其中包含一些内容,最后将其删除。我将它命名为“gnu2posix”,人们可以自由使用它:
#!/bin/bash
set -o nounset
### // Convert a tar file, from the "gnu tar" format to the "posix tar" one (which in Windows, for example, allows seeing correctly all of the utf-8 characters of the names of the files)
NAME_PROGRAM=$(basename "$0")
alert() {
echo "$@" >&2 ;
}
alert_about_usage() {
echo "The usage of this program is: $NAME_PROGRAM FILE_TO_CONVERT RESULTING_FILE" >&2
}
if [[ $# != 2 ]]; then
alert "ERROR: the program \"$NAME_PROGRAM\" needs two arguments, but it has received: $#."
alert_about_usage
exit 1;
fi
file_to_convert="$1"
if [[ ! -f "$file_to_convert" ]]; then
error "ERROR: the program \"$NAME_PROGRAM\" can't access any file with this path: \"$file_to_convert\"."
alert_about_usage
exit 1;
fi
resulting_file="$2"
# // Create a file with something inside, in this case, the "." folder (without its contents). This way, a real "posix tar" is created
tar --format=posix -cf "$resulting_file" . --no-recursion
# // Add "$file_to_convert", finally getting a "posix tar" file
tar -Avf "$resulting_file" "$file_to_convert"
# // Just in case, delete the "." folder from the file
tar -f "$resulting_file" --delete "."
# // End of file
答案3
Gnu tar 有一个“连接”选项,但由于预期的用例,要求目标存档已经存在。
tar --format=posix -cvf converted.tar --files-from=/dev/null # create an empty tar file
tar --format=posix -Avf converted.tar original.tar
免责声明:我没有测试过这个脚本。