Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 1
    Does cating the file actually have any advantage to just appending though ? It would seem as though it would take longer as it has to fork two processes every loop and also move the entire contents multiple times. Commented Mar 11, 2016 at 15:14
  • 1
    @123 speed. The cat approach is much, much faster. It only makes sense for creating huge files but that created a 545M file in 10 seconds on my machine. The same while loop with echo "test line" >> file created a 96K file in the same amount of time. Commented Mar 11, 2016 at 17:15
  • I guess the thing with the "cat" approach is that it grows exponentially. On starting the second iteration, 'newfile' already has 1 line and 'file' has 2, and when it is done, 'newfile' is now 3 lines and 'file' is 5. Next, 'newfile' will be 8 and 'file' will be 13. Next (21, 34), etc. Commented Jul 18, 2019 at 7:35
  • downside: it may take more disk space (>= 1.5 * desired_size) than target file size while it is creating the file. Commented Jul 18, 2019 at 7:41
  • btw. If you have truncate around, you can truncate -s 1G to create the file in first place. unix.stackexchange.com/a/269184/85549. You could replace it by a head -c $DESIRED_SIZE, possibly within the while loop. Commented Jul 18, 2019 at 7:43