LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-29-2008, 05:07 AM   #1
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Rep: Reputation: 15
Thumbs up 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.
 
Old 10-29-2008, 05:18 AM   #2
mrrangerman
Member
 
Registered: Oct 2007
Location: MI
Distribution: Debian Slackware
Posts: 528

Rep: Reputation: 50
man tar
man rm
 
Old 10-29-2008, 05:18 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.

Last edited by druuna; 10-29-2008 at 05:21 AM.
 
Old 10-29-2008, 07:14 AM   #4
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,
I did not get you completly. Can you please say me in detail.

Thanks,
Prakash.
 
Old 10-29-2008, 07:37 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 10-29-2008, 08:18 AM   #6
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,

Thanks. I will definitely look at it.

Prakash.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i decompress this tar.tar file? hmmm sounds new.. tar.tar.. help ;) kublador Linux - Software 14 10-25-2016 03:48 AM
gave wrong syntax for tar as tar -cvzf file file.tgz how to recover the file gautham Linux - General 4 04-13-2005 04:15 AM
how to make a tar.bz2 file bruse Linux - Newbie 5 03-30-2005 02:42 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration