GreatGather
GreatGather

Reputation: 881

create and read tar.bz2 files in perl

I have been trying to use

system("tar -jcvf archive_name.tar.bz2 $my_file")

but I get an error

archive_name.tar.bz2: Cannot open: No such file or directory tar: Error is not recoverable: exiting now tar: Child returned status 2

Is it not possible to create .tar.bz2 using this method in perl? I would prefer not to use a module but will if it is absolutely necessary

Upvotes: 0

Views: 1385

Answers (1)

David W.
David W.

Reputation: 107040

First of all, you're not doing it in Perl. You're spawning a separate process that runs the command.

If you want to do this in Perl, you would need to use the Archive::Tar module and possibly IO::Compress::Bzip2 module too.

First thing is to see if you can run this command straight from the command line:

$ tar -jcvf archive_name.tar.bz2 $my_file

If you can't run the tar command from the command line, it's highly unlikely to be able to run it from inside Perl using the system command.

The way you ran the command, with the command and all of the arguments in a single strung, causes Perl to run the command from a shell (usually /bin/sh). If you run the command with the arguments in a list, Perl will run the command directly without a shell:

my $error = system qw( tar -jcvf archive_name.tar.bz2 $my_file );

Always check the output of the system command and also the value of $?. The system command documentation has more information about processing errors.

Upvotes: 1

Related Questions