Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.]

share|improve this question
add comment

3 Answers 3

up vote 5 down vote accepted

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.
share|improve this answer
    
why gnu tar can't do that simple and obvious thing? +1 –  Serge Sep 27 '12 at 5:13
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.