Is it possible in Linux (and/or on other Unix) 'shrink' file from beginning? I'd like to use it for persistent queue (no existing implementation suits my needs). From end of file I guess it's possible with truncate().
shrink (truncate) file from beginning on linux
4.6k views Asked by woky At
3
There are 3 answers
0
On
If you are using ext4, xfs or some other modern file system, since Linux Kernel 3.15 you can use:
#include <fcntl.h>
int fallocate(int fd, int mode, off_t offset, off_t len);
with the FALLOC_FL_COLLAPSE_RANGE
flag.
http://manpages.ubuntu.com/manpages/disco/en/man2/fallocate.2.html
Yes, you can use
cut
ortail
to remove portions of a file.cut -b 17- input_file
tail -c +17 input_file
This will output the contents of input_file starting at the 17th byte, effectively removing the first 16 bytes of the file. Note that the
cut
example will also add a newline to the output.