7

On the one hand I have a lot of tar files created with gnu format, and on the other hand I have a tool that only supports pax (aka posix) format. I am looking for an easy way to convert the existing tar files to pax format - without extracting them to the file system and re-create the archives.

GNU tar supports both formats. However, I haven't found an easy way to the conversion.

How can I convert the existing gnu tar files to pax?

[I asked the same question on superuser.com, and a commenter recommended to migrate the question to unix.stackexchange.com.]

nosid
  • 173

3 Answers3

6

You can do this using 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 is the magic option. From the manpage:

@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.
3

The user Random832 wrote:

[...] create an empty tar file [...]
Disclaimer: I have not tested this script.

God bless you! You gave me ideas. I tested your script, but if someone creates a empty tar file, tar doesn't consider it a "posix tar" file. So I wrote a script that creates a "posix tar" file with something inside, and finally deletes it. I named it "gnu2posix", people can use it freely:

#!/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
Sys
  • 31
  • 2
2

Gnu tar has a "concatenate" option, but requires the destination archive to already exist due to the intended use case.

tar --format=posix -cvf converted.tar --files-from=/dev/null # create an empty tar file
tar --format=posix -Avf converted.tar original.tar

Disclaimer: I have not tested this script.

Random832
  • 10,666
  • From the documentation, I was under the impression this would create invalid tar files if the source and destination formats were different, but I haven't tested that. – ire_and_curses Sep 27 '12 at 20:59
  • WARNING this is known to create defective archives as GNU tar does not care about archive formats. – schily Jun 27 '18 at 20:17