Related: How do I remove the first 300 million lines from a 700 GB txt file on a system with 1 TB max disk space? on unix.SE points out that you can dd
in place (conv=notrunc) to copy the data earlier in the file before truncating, getting the job done with no extra disk space needed.
That's horrible as part of a repeated process to shift data from the start of one file into the end of another. But worth mentioning for other use-cases where the purpose of truncating the front is to actually bring a specific point in the file to the front, not just to free disk space.
I would like to truncate the first 100MB of the file and leave the file with remaining 9.9GB
That's the opposite of what the list of steps says to do, from the answer on How can you concatenate two huge files with very little spare disk space? which you say you're following. @Douglas Leeder suggested copying into the middle of a sparse file so you only need to truncate at the end, which is easy and portable with a POSIX ftruncate(2)
system call on the open fd you're using to read that file.
But if you want to avoid copying the first file, and just append the 2nd file to the end of the first, yes you do need to free data at the start of the 2nd file after you've read it. But note that you don't need to fully truncate it. You just need to free that space, e.g. by making the existing file sparse replacing that allocated space with a "hole".
The Linux-specific system call fallocate(2)
can do that with FALLOC_FL_PUNCH_HOLE
on FSes including XFS (since Linux 2.6.38), ext4 (since 3.0), BTRFS (since 3.7).
So it's available earlier than FALLOC_FL_COLLAPSE_RANGE
(Linux 3.15) which shortens the file instead of leaving a hole. Linux 3.15 is pretty old by now so hopefully that's irrelevant.
Punching holes in data after you read it (and get it safely written to the other file) is perhaps simpler than shifting data within the file, in terms of being sure of the semantics for file position of a file descriptor you're reading from, if it's open while you use FALLOC_FL_COLLAPSE_RANGE
.
The fallocate(1)
command-line tool is built around that system call, allowing you do to either of those things on systems that support them.