andyortlieb andyortlieb - 1 year ago 85
Linux Question

How to remove X bytes from the end of a large file without reading the whole file?

In Linux, I have a rather large file with some extraneous information tacked on to the end of it. Let's say for example I know there are 314 bytes of extraneous data at the end of a 1.6GB file.

Of course it is very easy and efficient to add more data to the end of a file, but what can I do to remove it without having to copy the first portion of that file into another (or overwrite said file)?


I'm seeing some good advice on doing this in C. I was hoping to script it from the commandline, but failing that I would be more inclined to doing it in python than C.

I see that python has a truncate method on its file object but it seems to be demolishing my file no matter how i use it--I should be able to figure this one out, but of course answers are more than welcome still.

Answer Source

use the function truncate

int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length); 

truncate takes the file name
ftruncate takes an open file descriptor

both of these set the file length to length so it either truncates or elongates (in the latter case, the rest of the file will be filled with NULL/ZERO)

truncate (linux shell command) will work also


truncate -s integer <filename>  

-s number specify the new file length. If the new length is smaller than the current filelength data is lost. If the new length is greater the file is padded with 0. You can specify a magnitude character to ease large numbers:
b or B size is bytes.
k size is 1000 bytes.
K size is 1024 bytes.
m size is 10^6 bytes.
M size is 1024^2 bytes.
g size is 10^9 bytes.
G size is 1024^3 bytes.


To shrink a file to 10 bytes:

truncate -s 10 /tmp/foo

To enlarge or shrink a file to 345 Megabytes:

truncate -s 345M /tmp/foo


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download