Linux shell: repack a bz2 archive into xz

4

2

How can I repack .bz2 archive to .xz format, if I want to save datestamp of file and its chmod-chown settings?

I can use any standard tools and xz, lzma, p7zip

osgx

Posted 2011-08-13T01:21:15.527

Reputation: 5 419

Answers

2

You need to break this into two parts

1) Convert from bz2 to xz, which billc.cn answered

2) Fix the ownership, permissions and timestamp of the xz file to match the bz2.

chown "`stat --printf '%u:%g' file.bz2`" file.xz
chmod "`stat -c '%a' file.bz2`" file.xz
touch -a -d "`stat -c '%x' file.bz2`" file.xz
touch -m -d "`stat -c '%y' file.bz2`" file.xz

brightlancer

Posted 2011-08-13T01:21:15.527

Reputation: 946

5

You can simply use bzcat to remove the bzip2 compression but keep the tar format and then pipe the results into xz:

bzcat file.tar.bz | xz > file.tar.xz

You can add your compression options to xz as usual, but just leave out the file name so the standard input is used.

billc.cn

Posted 2011-08-13T01:21:15.527

Reputation: 6 821

and how " to save datestamp of file and its chmod-chown settings?" – osgx – 2011-08-13T08:28:04.443

The file meta info are stored in the tar file and is not handled by the compression algorithm. If you don't touch the tar, then nothing will be changed. If you compressed a file without tar, you have already lost all the meta info anyways. – billc.cn – 2011-08-13T23:46:29.960

sorry, I did not ask about "tar.bz2" but about "bz2". Tar was added in your answer by you. I want to save stamp of the File.bz2 (File.xz should have the same stamp). – osgx – 2011-08-14T08:25:48.870

In which case the question has nothing to do with bzip or xz because these formats do NOT store timestamps in the archive file. When you decompress, the timestamp of the compressed file will be used for the output. To set the timestamp of a file, you can use touch. ls has extra options to display the complete {creation, access, modification} times of your original file. – billc.cn – 2011-08-15T22:28:32.317

Not the inside a file, but of the archive itself. I want to repack a lot of bzip2 backups with xz and timestamp of archive is valuable information in my case. – osgx – 2011-08-15T22:29:59.223

Then you can use ls (or stat)to get the dates from the bzip2's and use touch to set them on the new xz's. You'll probably have to write a shell script for this. – billc.cn – 2011-08-15T22:36:11.797

it is hard to parse ls output because it is in different format. I have really a lot files and can't convert by hand; oldest files are 4+ years ago, newer are several weeks. – osgx – 2011-08-15T22:39:39.380

1In this case use stat -c %X to get the raw values. Change X for Y and Z, to get modification and change time. See the man page for stat. – billc.cn – 2011-08-15T22:42:37.250