Truncating the first 100MB of a file in linux Truncating the first 100MB of a file in linux linux linux

Truncating the first 100MB of a file in linux


Answer, now this is reality with Linux kernel v3.15 (ext4/xfs)

Read herehttp://man7.org/linux/man-pages/man2/fallocate.2.html

Testing code

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#ifndef FALLOC_FL_COLLAPSE_RANGE#define FALLOC_FL_COLLAPSE_RANGE        0x08#endifint main(int argc, const char * argv[]){    int ret;    char * page = malloc(4096);    int fd = open("test.txt", O_CREAT | O_TRUNC | O_RDWR, 0644);    if (fd == -1) {        free(page);        return (-1);    }    // Page A    printf("Write page A\n");    memset(page, 'A', 4096);    write(fd, page, 4096);    // Page B    printf("Write page B\n");    memset(page, 'B', 4096);    write(fd, page, 4096);    // Remove page A    ret = fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, 4096);    printf("Page A should be removed, ret = %d\n", ret);    close(fd);    free(page);    return (0);}


Chopping off the beginning of a file is not possible with most file systems and there's no general API to do it; for example the truncate function only modifies the ending of a file.

You may be able to do it with some file systems though. For example the ext4 file system recently got an ioctl that you may find useful: http://lwn.net/Articles/556136/


Update: About a year after this answer was written, support for removing blocks from beginning and middle of files on ext4 and xfs file systems was added to the fallocate function, by way of the FALLOC_FL_COLLAPSE_RANGE mode. It's more convenient than using the low level iotcl's yourself.

There's also a command line utility with the same name as the C function. Assuming your file is on a supported file system, this will delete the first 100MB:

fallocate -c -o 0 -l 100M yourfile

delete the first 1GB:

fallocate -c -o 0 -l 1G yourfile


Please read a good Linux programming book, e.g. Advanced Linux Programming.

You need to use Linux kernel syscalls, see syscalls(2)

In particular truncate(2) (both for truncation, and for extending a sparse file on file systems supporting it), and stat(2) to notably get the file size.

There is no (portable, or filesystem neutral) way to remove bytes from the start (or in the middle) of a file, you can truncate a file only at its end.