简体   繁体   中英

Removing the first bytes in a file

Is there a way to remove the first few bytes of a file (truncating it from the front) without using programs like tail or without having to read the whole contents of the file?

That is because the file size might be hundreds of MBs or even GBs, so dealing with reading and re-writing the contents of the file would be very unefficient.

What I am looking for is, possibly, a solution that allows me to offset the beginning of the file (in the file allocation table) so that the first few bytes are just skipped.

On Windows there is a SetEndOfFile function, which allows me to truncate the file simply by limiting the end of the file, reducing the file size.

Is there anything like a StartOfFile function which would allow me to make the file start a few bytes after its current start position?

Any other solution would also be welcome, as long as it does not require reading / rewriting the whole contents of the file.

It depends a little on filesystem type, but mostly the answer is no. File allocation tables are pointers to particular blocks or inodes - contiguous chunks of 'file data'.

They support being cut short - technically you read the whole block, but then stop when you hit an 'end of file' marker. But there's not really any mechanism for 'skip-until'. To delete the first few bytes, you can either overwrite them, or you have to rewrite and re-align the whole file.

Something like dd can do this fairly efficiently.

These links may be relevant:

https://unix.stackexchange.com/questions/13907/delete-the-first-n-bytes-of-files Remove number of bytes from beginning of file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM