Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

(OT) Linux/Perl Tar System Command

by ccericks (Initiate)
on Aug 04, 2003 at 17:41 UTC ( #280743=perlquestion: print w/replies, xml ) Need Help??

ccericks has asked for the wisdom of the Perl Monks concerning the following question:

This might be a question more suited to Server/Linux Guru types but I thought possibly some programmers using Perl might have run into this problem.

Anyway, on a Redhat Linux server I am trying to run a cron script weekly to tar up miscellaneous files which accumulate in a specific directory. When I use this code,

system("tar -cf tarred_orders.$year$month$mday.tar temp");
I get an error message because the temp directory which I am attempting to tar up isnít in the directory where the script is running.

Then when I try to put a path in the command to show the script where the temp directory is,

system("tar -cf tarred_orders.$year$month$mday.tar /project/server_nam +e/web/catalog/orders/temp");
the script finds the files and tars them up. However, when I untar the tar file, the whole directory structure, starting at the root, is created in the directory where the script is running, so my tarred files end up here:
/project/server_name/bin/cron/project/server_name/web/catalog/orders/t +emp/.
Does anyone know how to make a system call using Perl on a Linux server so that when the tar file is opened, it will simply place the files in the directory where the tar file was located without creating the directory structure?


update (broquaint): added formatting

update (broquaint): title change (was Linux/Perl Tar System Command)

Replies are listed 'Best First'.
Re: (OT) Linux/Perl Tar System Command
by halley (Prior) on Aug 04, 2003 at 17:49 UTC
    Not a Perl question at all. man tar

    GNU tar paths are relative by default. If you want to extract from the root, cd to the root before you extract. Of course, you should probably know what you're doing before you go extracting from root.

    Update: Qualified. The question did say Linux, and non-GNU tar is rare on Linux.

    [ e d @ h a l l e y . c c ]

      [2:11pm] 8 [/usr/local/bin]:server% tar -cvf /tmp/lie.tar /var/tmp/has +h a /var/tmp/hash 5K [2:11pm] 9 [/usr/local/bin]:server% cd /tmp [2:11pm] 10 [/tmp]:server% tar -xvf lie.tar tar: blocksize = 12 x /var/tmp/hash, 4286 bytes, 9 tape blocks
      For example this was run on solaris. It is very possible to have fully qualified (non relitive) paths with tar. and _most_ unix flavors do this by default if you specify the absolute path while creating the archive (without the chdir option). In fact most versions of the tar the comes with Solaris make it impossible to extract relitive when the files are stored absolute -- your two choices are really to use gnu tar (which lets you ignore absolute) or chroot to the location besides root that you wanted to untar to.

Re: (OT) Linux/Perl Tar System Command
by sgifford (Prior) on Aug 04, 2003 at 20:45 UTC
    You simply want to chdir to the directory first:
    chdir("/project/server_name/web/catalog/orders") or die "Couldn't CD to orders directory: $!\n"; system("tar -cf tarred_orders.$year$month$mday.tar temp") == 0 or die "Error running tar: exit status $?\n";
Re: (OT) Linux/Perl Tar System Command
by Anonymous Monk on Aug 04, 2003 at 17:56 UTC
    Yes, anyone who knows how to RTFM. Learn how to RTFM by reading How to RTFM.
    $ tree /a asdf +---asdf | \---basdf | \---rasdf \---basdf \---rasdf $ tar -zxv -f asdf.tar.gz -CReadTheF-ingManual tar: ReadTheF-ingManual: Cannot chdir: No such file or directory tar: Error is not recoverable: exiting now $ tar -zxv -f asdf.tar.gz -CReadTheF-ingManual asdf/ asdf/asdf/ asdf/asdf/basdf/ asdf/asdf/basdf/rasdf/ asdf/basdf/ asdf/basdf/rasdf/ $ tree /a READTHEF-INGMANUAL $ READTHEF-INGMANUAL \---asdf +---asdf | \---basdf | \---rasdf \---basdf \---rasdf
    Even without knowing how to 'man tar', you could've still solved your issue by reading 'perldoc perlfunc'.
      Note:I do NOT disagree with the above post.

      I just read the RTFM-oriented post above, and was surprised to see that it was from an anonymous monk. The content and tone of the message really screams experienced monk. So, im wondering if the poster actually has an account here. The reason i'm saying this is because a message like that is probably better received from an established user than from an unknown lurker.

        Actually, if the RTFM message was not posted as an AM message, I would expect it to get downvoted. Hard.

        While the actual message in the post is somewhat correct, the tone and the the illustrating examples are downright rude and demeaning. The RTFM post has a very high risk of getting downvoted, and I guess that was the reason why it was posted as an AM message.

        No matter how frustrated one might get at what might be dumb questions or ignorant users, there is absolutely no reason to become unchivalrous. Better for that monk (if he/she indeed has an account) to have not posted that message at all.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://280743]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-05-16 21:58 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (63 votes). Check out past polls.