#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0

    remove contents of a file


    Can someone show how to remove the content of a file leaving the last 100 lines? The aim is to preserve the perimission for the file. Thanks.

    Oisaya
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Jersey
    Posts
    64
    Rep Power
    12
    Well, if it were me, I'd tail out the lines to a temp file, then zero out the original, and cat the temp contents back into the original, i.e.:

    FILE1=/path/to/current/file
    TEMPFILE=/tmp/newfile

    tail -100 > $TEMPFILE

    cat /dev/null > $FILE1

    cat $TEMPFILE >> $FILE1

    That should do it and keep the permissions set, since you're not deleting the file, just modifying it's contents. If you need any different number of lines, not 100, you can just substitute the number in the tail line above.

    I'm sure there's another cleaner way to do this, but the above should be a workable hack.

    -Gary

IMN logo majestic logo threadwatch logo seochat tools logo