LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make a tar file using perl (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-a-tar-file-using-perl-679723/)

prakash.akumalla 10-29-2008 04:07 AM

How to make a tar file using perl
 
Hai,

I have written a script when run will create a txt file. Now I want to make a tar file for that text file, and delete the text file created after running the script.

Finally when I give the command 'ls' after running the script, I should find only a tar file, which when extracted should give a text file.

Please help me in doing this.

Thanks,
Prakash.

mrrangerman 10-29-2008 04:18 AM

man tar
man rm

druuna 10-29-2008 04:18 AM

Hi,

This is one way of doing just that (what follows is the relevant part from a larger perl script):
Code:

  # anything to archive
  if ( $needArchiving =~ "T") {
    epochToDate ($highMtime);

    # get basic filename
    my ($baseName, $path, $suffix) = fileparse($targetLog,'\..*');

    # tar target(s)
    $targetName = $archDirName . "/" . $baseName . "." . $hrTime . ".gz.tar";
    @externalProg = ($TAR, "cf -", @archFiles, ">", $targetName );
    system("@externalProg") == 0 or die "$TAR: execute failed!. ($?)\n\n";

    # remove tarred source file(s)
    @externalProg = ($RM, "-f", @archFiles );
    system("@externalProg") == 0 or die "$RM: execute failed!. ($?)\n\n";
    $usedArchive = basename($targetName);

  }

As you can see I used the system("@prog") way of executing external commands (seen from perl). Some of the variables are set in the beginning or in the rest of the script ($TAR for example: my $TAR = "/bin/tar";)

Take a look at perl system function for details on how to use it, the general layout of this function is as follows:
Quote:

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"
Hope this helps.

prakash.akumalla 10-29-2008 06:14 AM

Hi,
I did not get you completly. Can you please say me in detail.

Thanks,
Prakash.

druuna 10-29-2008 06:37 AM

Hi again,

Which part(s) didn't you get?

Maybe this page will help: Perl 5.10.0 documentation - ... > Functions > system

or this: Learning Perl - Chapter 14. Process Management.

Hope this gets you going again.

prakash.akumalla 10-29-2008 07:18 AM

Hi,

Thanks. I will definitely look at it.

Prakash.


All times are GMT -5. The time now is 10:50 AM.