Truncating the first 100MB of a file in linux
NickName:CheeHow Ask DateTime:2013-08-06T13:09:04

Truncating the first 100MB of a file in linux

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 sparse file of the combined size.
  2. Copy 100Mb from the end of the second file to the end of the new file.
  3. Truncate 100Mb of the end of the second file
  4. Loop 2&3 till you finish the second file (With 2. modified to the correct place in the destination file).
  5. Do 2&3&4 but with the first file.

I would like to know if is there anyone there who are able to "truncate" a given file in linux? The truncation is by file size, for example if the file is 10GB, I would like to truncate the first 100MB of the file and leave the file with remaining 9.9GB. Anyone could help in this?

Thanks

Copyright Notice:Content Author:「CheeHow」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18072180/truncating-the-first-100mb-of-a-file-in-linux

Answers
Sunding Wei 2014-08-01T10:43:31

Answer, now this is reality with Linux kernel v3.15 (ext4/xfs)\nRead here\nhttp://man7.org/linux/man-pages/man2/fallocate.2.html\nTesting code\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <stdlib.h>\n#include <fcntl.h>\n\n#ifndef FALLOC_FL_COLLAPSE_RANGE\n#define FALLOC_FL_COLLAPSE_RANGE 0x08\n#endif\n\nint main(int argc, const char * argv[])\n{\n int ret;\n char * page = malloc(4096);\n int fd = open("test.txt", O_CREAT | O_TRUNC | O_RDWR, 0644);\n\n if (fd == -1) {\n free(page);\n return (-1);\n }\n\n // Page A\n printf("Write page A\\n");\n memset(page, 'A', 4096);\n write(fd, page, 4096);\n\n // Page B\n printf("Write page B\\n");\n memset(page, 'B', 4096);\n write(fd, page, 4096);\n\n // Remove page A\n ret = fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, 4096);\n printf("Page A should be removed, ret = %d\\n", ret);\n\n close(fd);\n free(page);\n\n return (0);\n}\n",


Joni 2013-08-06T05:51:02

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.\nYou 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/\n\nUpdate: 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.\nThere'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:\nfallocate -c -o 0 -l 100M yourfile\n\ndelete the first 1GB:\nfallocate -c -o 0 -l 1G yourfile\n",


More about “Truncating the first 100MB of a file in linux” related questions

Truncating the first 100MB of a file in linux

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: Allocate a sparse file of the combined size. Copy 100Mb...

Show Detail

Get latest 100MB Of text file in linux

How do I get the latest 100MB from a text log on Linux? Is there a tool for it, or could you point me on a script? I have no programming experience on Shell Scripting, Perl or Python, and I don't ...

Show Detail

C#: Archiving a File into Parts of 100MB

In my application, the user selects a big file (>100 mb) on their drive. I wish for the program to then take the file that was selected and chop it up into archived parts that are 100 mb or less. H...

Show Detail

Which linux official distribution are less than 100mb?

I need some Linux official distribution that are less than 100mb for pulling images from docker hub server. so far I am familiar with debian and busybox. any more official suggestions?

Show Detail

Keep count of occurrences in a truncating file

Let's say I have a file called data.log. Data constantly gets appended to it which can contain 'flag' and it gets truncated by an external script: [13/Jan/2015:11:11:53 +0000] curabitur flag la...

Show Detail

truncating a text file does not change the file

When a novice (like me) asks for reading/processing a text file in python he often gets answers like: with open("input.txt", 'r') as f: for line in f: #do your stuff Now I would like to

Show Detail

Truncating a file while it's being used (Linux)

I have a process that's writing a lot of data to stdout, which I'm redirecting to a log file. I'd like to limit the size of the file by occasionally copying the current file to a new name and trun...

Show Detail

Converting Decimal to Hex is truncating the first digit

I have a function to convert decimal to HEX but it's truncating the first character when returning the hex string. Why and how is it truncating the returned string? this is the char string I'm

Show Detail

How to truncate a file in FAT32 file system without zero padding in C?

I have 2TB HDD contains a single partition of FAT32 file system. While truncating a file to larger size say 100MB or 200MB using ftruncate(), it takes time of 5 to 10 seconds to do zero padding. Is...

Show Detail

How to Upload 100MB file?

I am designing a File Server application in ASP.NET. I have to upload a file of size 100MB and then save it into SQL Server 2008 database in varbinary(max). Saving file in DB is must. I am using

Show Detail