truncate a file

Posted on 2006-05-01
Last Modified: 2010-04-22
I had asked this question previously but I closed out the question prematurely:

"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."

I put togther a program that uses the truncate() function which I though was working the way I wanted to, but it turns out it won't truncate a file while another program has that file open. I even tried suspending the first process, truncating the file, and then resuming the suspended process. The file size was reduced, but after I resumed the process, the file was restored to the size it had before I suspended the process (argh).

Any input much appreciated,

Question by:opike
    LVL 84

    Assisted Solution

    head file > temp
    mv temp file

    but any other writes that the process does to the file before closing it will be lost

    LVL 43

    Assisted Solution

    I guess in Your case it's impossible. Thats, becouse the original application haven't opened the file in APPEND mode (that's why it restores to the original size after truncate)...
    In such case, the orig application (after resuming) writes to outside the file (at original position), which is fine under UNIX systems (the file grows and it's propably sparse file) :(
    LVL 51

    Assisted Solution

    > "Is there a way to truncate a file while it is open and being written to? ..
    as long as the application has the file open, you can truncate it as ozo suggested, but that truncation will only be "visible" at user level, the filesystem still occupies the whole place.
    So it's impossible to truncate the size of file from another process while it is in use.
    LVL 61

    Assisted Solution

    > "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."

    If opening process only appends file then
    : > file
    (null command truncating and outputing to file)
    will help

    If program writes to file position you have to create a script that makes program reopen file after truncation.
    LVL 43

    Expert Comment

    ahoffmann: unfortunatelly You wrong(at least concerning linux).
    If the another process has the file open in O_APPEND mode, everything would work fine. Otherwise, on next write, the filesystem will create sparse file.
    But anyway, after truncating, the file is truncated (also in filesystem meaning)
    LVL 51

    Expert Comment

    ravenpl, sometimes we learn new things every day, thanks!
    LVL 27

    Accepted Solution

    opike, what OS/version are you using?
    All above is correct, you CANNOT truncate open file externally.

    Sometimes there are no direct ways of doing things (yes, it's your case), but some workarounds (dirty hacks) are possible :-)

    I know, truncating of file, open by another process (it doesn't matter if it was opened with APPEND or WRITE mode) is _possible_.
    You only need to fix some fields in one kernel table and their copies in userspace of running process.
    With kernel space you may write your own syscall handler (yes, it's possible) :-)
    With user process space you may also use new syscall or it might be possible to use ptrace() syscall.
    If you are ready to go with all these difficulties (read: this truncation is _so_ importent for you), we will proceed.

    Have a nice weekend.

    LVL 3

    Assisted Solution

    A way to "truncate" a file could be the following: cat /dev/null > <file>

    I hope it helps.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. Please see for the updated article. It is avail…
    Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    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…

    733 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

    21 Experts available now in Live!

    Get 1:1 Help Now