Truncating the first 100MB of a file in linux

后端 未结 8 2113
余生分开走
余生分开走 2020-12-13 10:11

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
8条回答
  •  一生所求
    2020-12-13 10:33

    This is a pretty old question by now, but here is my take on it. Excluding the requirement for it to be done with limited space available, I would use something similar to the following to truncate the first 100mb of a file:

    $ tail --bytes=$(expr $(wc -c < logfile.log) - 104857600) logfile.log > logfile.log.tmp
    $ mv logfile.log.tmp logfile.log
    

    Explanation:

    • This outputs the last nn bytes of the file (tail --bytes).
    • The number of bytes in the file to output is calculated as the size of the file (wc -c < logfile.log) minus 100Mb (expr $( ... ) - 104857600). This would leave us with 100Mb less than the size of the file to take the tail of (eg. 9.9Gb)
    • This is then output to a temp file and then moved back to the original file name to leave the truncated file.

提交回复
热议问题