Truncating the first 100MB of a file in linux

后端 未结 8 2102
余生分开走
余生分开走 2020-12-13 10:11

I am referring to How can you concatenate two huge files with very little spare disk space?

I\'m in the midst of implementing the following:

  1. Allocate a
相关标签:
8条回答
  • 2020-12-13 10:53

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

    Read here http://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
    #endif
    
    int 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);
    }
    
    0 讨论(0)
  • 2020-12-13 10:55

    If you can work with ASCII lines and not bytes, then removing the first n lines of a file is easy. For example to remove the first 100 lines:

    sed -i 1,100d /path/to/file
    
    0 讨论(0)
提交回复
热议问题