truncate a file

Posted on 2006-05-01
Last Modified: 2011-10-03
Is there a way to truncate a file while it is open and being written to? I'm using an application that hasn't been set up with log file rolling and I'm trying to manange it externally.

Question by:opike
    LVL 45

    Expert Comment

    Hi opike,

    > Is there a way to truncate a file while it is open and being written to?

    Can try

    echo > filename

    LVL 23

    Accepted Solution

    Have you tried ftruncate?  Excerpt from the man page:

         #include <unistd.h>

         int ftruncate(int fd, off_t length);

         ftruncate() causes the file referenced by fd to be truncated or
         extended to length bytes in size.  If the file previously was
         larger than this size, the extra data is lost. If the file was smaller
         than this size, it will be extended as if by writing bytes with the value
         zero.  With ftruncate(), the file must be open for writing.

    Author Comment

    truncate did the trick. thanks.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
    Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now