Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

cleaning out a file

by Simplicus (Monk)
on Apr 28, 2000 at 00:54 UTC ( [id://9470]=perlquestion: print w/replies, xml ) Need Help??

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

I am out on the road, and only have a laptop (w/out Perl) so I can't test this out....
Can I erase the contents of a file this way (I don't want to unlink the file, I just want to "clean it out.":
... s/./\004/; ...
Where \004 is ^D?
Simplicus

Replies are listed 'Best First'.
Re: cleaning out a file
by turnstep (Parson) on Apr 28, 2000 at 01:24 UTC
    Nope.
    But you could do this:
    open(X, ">$filename");
    or even:
    perl -e "open(X, '>'.shift);" SYSTEM.DA0
    which would shrink SYSTEM.DA0 to 0 bytes....
      Yes, I think this is the safest way. close FH if open FH, ">".$filename But overwrite mode might do an unlink/link combo. (meaning that the file $filename might be deleted, then recreated as a empty file). So this could be the same asunlink $filename; open ">$filename"; I am not sure. Anybody actually know if there is a difference? The file inode number should still be the same though for either method.
      Depending on what you are doing this is probably good enough.
RE: cleaning out a file
by Anonymous Monk on Apr 28, 2000 at 11:24 UTC
    system("cat /dev/null>$file");

    also not very portable

Re: cleaning out a file
by blackwolf (Sexton) on Apr 28, 2000 at 20:50 UTC
    What about plain ol' ...
        truncate 'filename', 0;
    
Re: cleaning out a file
by clash (Novice) on Dec 01, 2000 at 23:53 UTC
    why dont u just unlink it and create it again?
Re: cleaning out a file
by kirbyk (Friar) on Apr 28, 2000 at 02:38 UTC
    Another easy way to do it would be:
    unlink($filename); system("touch $filename");
    touch is a standard unix command that updates the last-modified timestamp of a file. If the file doesn't exists, it creates it as a zero-byte file. It's a handy little thing to know, for testing purposes.

    -- Kirby

      Actually, 'touch' does a lot more than that. Most people only use it for its default behavior, but it can change the access and modification times to anything you want - which can be occassionally very useful. For the record, using a system("touch") is not very portable, especially when you can do the same thing in perl easy enough.
RE: cleaning out a file
by radixzer0 (Beadle) on Apr 28, 2000 at 02:33 UTC
    Hate to introduce a non-perl item ;) but a bash one-liner is:
    : > filename

    Use the tools you've got is what i always say...
    This also preserves all the file permissions and attributes...
    Assuming you're on Unix of course (or bash on some other platform).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found